Skip to content

feat(ai): constrain structured replies on the openai and claude providers - #2550

Merged
dan-petty merged 2 commits into
mainfrom
feat/1419-structured-reply-constraint
Oct 28, 2026
Merged

dan-petty merged 2 commits into
mainfrom
feat/1419-structured-reply-constraint

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

chat_structured sends the reply's JSON Schema in the system message to every provider. Only the gateway and ollama providers also got it as a constraint on the reply. The openai, claude and copilot providers were prompt-only, so their replies could drift from the schema and cost a reflection retry, or fail the item after the last retry. chat_structured serves intake, refine and two research calls.

Each provider now gets the constraint that the pydantic-ai profile of its model supports, in the form that pydantic-ai's NativeOutput sends, through the official SDK of the provider:

  • openai: response_format (type: json_schema). openai_model_profile gives the profile, its OpenAIJsonSchemaTransformer adapts the schema, and strict is true only when the schema fits OpenAI's strict mode (is_strict_compatible). The four schemas that chat_structured gets today do not fit it, so they go with strict: false.
  • claude: output_config.format (type: json_schema) when anthropic_model_profile says that the model supports JSON-schema output. AnthropicJsonSchemaTransformer adapts the schema with the Anthropic SDK's transform_schema. The format goes beside the effort of adaptive thinking. A model that the profile does not name gets no format.
  • copilot: no constraint. No profile or probe shows that the Copilot API honours one, and an unsupported response_format could turn a request that succeeds today into a 400 error.
  • gateway and ollama: unchanged.

ReplySchema (ai/client/network.py) owns the two provider forms. chat_structured keeps its repair, validation and reflection retry, because the OpenAI form is not strict for the current schemas and the Anthropic transform moves the constraints that the API does not compile into descriptions. The design records the rejected alternatives: running chat_structured on pydantic-ai's Agent, a forced tool call for Claude, the SDKs' parse helpers, and strict: true for every OpenAI schema.

Closes #1419

Change: openspec/changes/archive/2026-10-28-1419-structured-reply-constraint/

…ders

`chat_structured` sent the reply's schema as a constraint only to the
gateway and ollama providers. The openai, claude and copilot providers
were prompt-only, so their replies could drift from the schema and cost
a reflection retry.

- The openai provider sends the `response_format` that pydantic-ai's
  `NativeOutput` sends for its model: `openai_model_profile` and its
  `OpenAIJsonSchemaTransformer` adapt the schema, and `strict` is true
  only when the schema fits OpenAI's strict mode.
- The claude provider sends `output_config.format` when
  `anthropic_model_profile` says that the model supports JSON-schema
  output. `AnthropicJsonSchemaTransformer` adapts the schema, and the
  format goes beside the effort of adaptive thinking.
- The copilot provider sends no constraint: no profile or probe shows
  that the Copilot API honours one.
@dan-petty

Copy link
Copy Markdown
Owner Author

Test evidence:

  • uv run devops ci fail-to-pass --base origin/main: 3 failed on base, 20 passed in tests/test_ai_client_structured.py, 0 errored. The three failures are the new constraints: test_each_provider_is_sent_the_schema_and_only_its_own_constraint (the openai body had no response_format), and test_claude_gets_the_output_format_its_models_profile_supports for claude-sonnet-4-6 without and with adaptive thinking (no format in output_config). The claude-example-1 and copilot cases already sent no constraint on base, as intended.
  • After the rebase onto 002d6450: tests/test_ai_client_structured.py, tests/test_thinking_providers.py and tests/test_ai_chat_reply_stream.py: 46 passed.
  • Before the rebase: the 27 AI client, gateway, research, intake and refine test files: 649 passed.
  • Full uv run devops ci before the rebase: every gate passed. The test step took 8 m 40 s on a shared host, over the 5-minute budget; its slowest tests are the architecture and docs invariants, which perf(ci): the startup-import tests start 56 interpreters and take 204 s of the suite's 2,423 s #2369, perf(ci): each test shard collects only its own test files #2371 and perf(ci): measure coverage and branch cost #2373 cover.

Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).

@dan-petty
dan-petty marked this pull request as ready for review October 28, 2026 21:50
@dan-petty
dan-petty merged commit 4f7390a into main Oct 28, 2026
17 checks passed
@dan-petty
dan-petty deleted the feat/1419-structured-reply-constraint branch October 28, 2026 21:51
dan-petty pushed a commit that referenced this pull request Oct 29, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.12.0](v0.11.3...v0.12.0)
(2026-10-29)


### Features

* **ai:** constrain structured replies on the openai and claude
providers ([#2550](#2550))
([4f7390a](4f7390a))
* **argo:** retire the Helm release records of the charts that Argo CD
renders ([#2568](#2568))
([cabfcd4](cabfcd4))
* **k8s:** devops k8s tunnel keeps the cloudflared forward to
k8s.<domain> running
([#2567](#2567))
([f9493fa](f9493fa)),
closes [#1641](#1641)
* **monitoring:** alert when the otel collector fails to export spans,
metric points or log records
([#2565](#2565))
([4fc8ff7](4fc8ff7))
* **telemetry:** the roadmap Service counts its GitHub writes, budget
holds, Firecrawl searches and own deliveries, and caps refine's comments
on one item
([#2556](#2556))
([9819877](9819877)),
closes [#2475](#2475)


### Bug Fixes

* **git:** report a worktree that a run in parallel removed as removed,
not a failed reclaim
([#2557](#2557))
([a556984](a556984)),
closes [#2408](#2408)
* **k8s:** the devops perimeter opens only the collector's OTLP/HTTP
port, 4318
([#2549](#2549))
([d156ce7](d156ce7))
* **sandbox:** devops sandbox logs reads each line with its stream from
a real engine
([#2546](#2546))
([e7216d5](e7216d5)),
closes [#2520](#2520)
* **service:** the webhook route refuses an unsigned or oversized
delivery before it reads the body, and a rate limit guards it
([#2553](#2553))
([40efcd4](40efcd4)),
closes [#2476](#2476)


### Performance Improvements

* **ai:** price a request with one load of the settings
([#2555](#2555))
([5d86538](5d86538)),
closes [#1420](#1420)


### Code Refactoring

* **config:** a default that backs a settings field is read only through
the settings
([#2561](#2561))
([0702d13](0702d13))
* **exceptions:** each exception class declares its error_code and
exit_code ([#2558](#2558))
([410dece](410dece))
* **review:** render the review report from one input, outside the
orchestrator
([#2563](#2563))
([9e73e6d](9e73e6d)),
closes [#1008](#1008)
* **security:** src has no unreviewed Bandit findings, and the gate
scans src at low severity
([#2560](#2560))
([45261dd](45261dd)),
closes [#1538](#1538)
* **security:** the scanners stop looking for mocks, and their tests run
on the scanner fake
([#2551](#2551))
([2b7f37a](2b7f37a))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(ai): constrain structured replies on the claude, openai and copilot providers

1 participant