feat: use native Output.object() for structured output#18450
Open
potlee wants to merge 2 commits intoanomalyco:devfrom
Open
feat: use native Output.object() for structured output#18450potlee wants to merge 2 commits intoanomalyco:devfrom
potlee wants to merge 2 commits intoanomalyco:devfrom
Conversation
…ool-based approach Use the AI SDK's experimental_output with Output.object() to leverage providers' native JSON mode (response_format) instead of a synthetic StructuredOutput tool with forced toolChoice.
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
Closes #
Type of change
What does this PR do?
Switches structured output from a custom
StructuredOutputtool to the AI SDK's nativeOutput.object()viaexperimental_output.Previously, when
format.type === "json_schema"was set, the code would:StructuredOutputtooltoolChoice: "required"so the model had to call itThis was fragile — it consumed a tool slot, required prompt engineering to make the model cooperate, and bypassed the provider's native JSON mode support.
Now it passes
experimental_output: Output.object({ schema })tostreamText(), which setsresponseFormat: { type: "json", schema }on the underlying provider call. Providers that support native JSON schema will use their built-in mechanisms, AI SDK handles the fallback to using a tool when structured output is not supported. The model outputs JSON text directly, the processor parses it, and tools still work alongside structured output for multi-step workflows.Net removal of ~300 lines of tool scaffolding, system prompt injection, and manual plumbing.
How did you verify your code works?
bun typecheckpasses with 0 errors (frompackages/opencode)bun test test/session/structured-output.test.ts— 13 pass, 0 failbun test test/session/structured-output-integration.test.ts— 1 pass, 4 skipped (require API key), 0 failScreenshots / recordings
N/A — no UI changes.
Checklist