Conversation
3 tasks
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
lu-zero
force-pushed
the
luca/structured-output-thinking
branch
from
September 11, 2026 08:27
cdc72c9 to
0282e79
Compare
…nking)
opencode implements structured output (`format: { type: "json_schema" }`)
by injecting a `StructuredOutput` tool and forcing `tool_choice: "required"`.
Anthropic and several reasoning providers reject thinking + forced tool use
with `400 "Thinking may not be enabled when tool_choice forces tool use."`,
so structured output either 400s or the model can't reason.
Route `json_schema` structured output through the AI SDK's native structured
output for providers that support it (Anthropic via `@ai-sdk/anthropic`'s
`structuredOutputMode: "outputFormat"`), exactly as the Anthropic SDK does it
(`output_format`, not a forced tool). The forced-tool path is kept as a
fallback for non-native providers, so their behavior is unchanged (strict
superset).
- llm.ts: add `structuredOutput` to `StreamInput`; on native-capable providers
pass `output: Output.object(jsonSchema(...))` and merge
`{ anthropic: { structuredOutputMode: "outputFormat" } }` into provider
options; defensively downgrade a forced `tool_choice` to `auto` on the native
path so the two can never collide. Add `supportsNativeStructuredOutput`.
- prompt.ts: skip the `StructuredOutput` tool, the coercion system prompt, and
the forced `tool_choice` on the native path; thread `structuredOutput`
through `handle.process`; parse the assistant text into `message.structured`,
degrading to the existing `StructuredOutputError` on parse failure.
- test: structured output on a thinking-enabled Anthropic model requests
`output_config.format` and does NOT force `tool_choice`.
Refs anomalyco#15226. Alternative to anomalyco#29565 (which relaxes the forced
tool_choice required->auto for thinking surfaces); this is the native
output_format approach.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lu-zero
force-pushed
the
luca/structured-output-thinking
branch
from
September 16, 2026 07:55
0282e79 to
f8eb445
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Fixes #15226. Reroll of #32637, closed by automated cleanup rather than review, rebased onto
dev.Type of change
What does this PR do?
format: json_schemaforcestool_choice: "required", which Anthropic rejects when thinking is on (400 "Thinking may not be enabled when tool_choice forces tool use."). Still live ondevatpackages/opencode/src/session/prompt.ts:1285:For Anthropic this routes through the provider's native
output_formatinstead of the forced tool. The model returns the JSON as text, parsed intomessage.structured. Not a forced tool, so it coexists with thinking. Other providers keep the existing path, so their behaviour is unchanged.session/llm.tsstructuredOutputtoStreamInput; on native-capable providers passoutput: Output.object(jsonSchema(...))and merge{ anthropic: { structuredOutputMode: "outputFormat" } }into provider options; downgrade a forcedtool_choicetoautoon the native path so the two cannot collide. AddsupportsNativeStructuredOutput.session/prompt.tsStructuredOutputtool, the coercion system prompt, and the forcedtool_choiceon the native path; threadstructuredOutputthroughhandle.process; parse assistant text intomessage.structured, degrading to the existingStructuredOutputErroron parse failure.test/session/llm.test.tsoutput_config.formatand does not forcetool_choice.#29565 and #28621 addressed this by relaxing the forced
tool_choice; both closed unmerged.How did you verify your code works?
bun turbo typecheckbun test test/session/llm.test.tsopencode servevs live Anthropic API, thinking +format: json_schemamessage.structuredpopulatedScreenshots / recordings
N/A — provider request-shape change.
Checklist