Conversation
|
The following comment was made by an LLM, it may be inaccurate: Based on my search results, I found one related PR that is mentioned in the PR description itself: Related PR (not a duplicate):
Additionally, there's:
PR #32637 appears to be a targeted fix for the specific issue (#15226) where Anthropic's native |
42b8d47 to
2828ca1
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>
2828ca1 to
ab5ad27
Compare
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
|
I think this PR is still relevant.. |
|
I'm happy to reroll it but it needs reviewers... |
|
Rerolled as #44634 — rebased onto current |
Issue for this PR
Closes #15226
Type of change
What does this PR do?
format: json_schemaforcestool_choice: "required", which Anthropic rejects with thinking on (400 "Thinking may not be enabled when tool_choice forces tool use.").For Anthropic, this uses the provider's native
output_formatinstead (output: Output.object({schema})+structuredOutputMode: "outputFormat"), skipping the forced tool. The model returns the JSON as text, parsed intomessage.structured. Not a forced tool, so it coexists with thinking. Non-Anthropic providers keep the existing path.See also #29565, that workarounds for non-anthropic providers.
How did you verify your code works?
bun run typecheckclean;bun test test/session/llm.test.ts→ 27 pass (added a test asserting a thinking Anthropic model sendsoutput_config.formatand no forcedtool_choice).opencode servefrom source against the live Anthropic API with thinking enabled +format: json_schema: no 400, reasoning emitted,message.structuredpopulated.Screenshots / recordings
N/A — provider request-shape change.
Checklist