#18450 · @potlee · opened Mar 20, 2026 at 11:37 PM UTC · last updated Mar 21, 2026 at 12:52 AM UTC
feat: use native Output.object() for structured output
Score breakdown
Impact
Clarity
Urgency
Ease Of Review
Guidelines
Readiness
Size
Trust
Traction
Summary
This PR refactors the structured output mechanism to leverage the AI SDK's native Output.object(), replacing ~300 lines of custom and fragile code. It enhances system reliability and better integrates with provider-native JSON modes.
Description
Issue for this PR
Closes #
Type of change
- [ ] Bug fix
- [ ] New feature
- [x] Refactor / code improvement
- [ ] Documentation
What does this PR do?
Switches structured output from a custom StructuredOutput tool to the AI SDK's native Output.object() via experimental_output.
Previously, when format.type === "json_schema" was set, the code would:
- Inject a synthetic
StructuredOutputtool - Add a system prompt telling the model to call that tool
- Force
toolChoice: "required"so the model had to call it - Capture the tool args as the structured output
This 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 }) to streamText(), which sets responseFormat: { 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 fail
Screenshots / recordings
N/A — no UI changes.
Checklist
- [x] I have tested my changes locally
- [x] I have not included unrelated changes in this PR
Linked Issues
None.
Comments
No comments.
Changed Files
packages/opencode/src/session/llm.ts
+6−0packages/opencode/src/session/processor.ts
+10−0packages/opencode/src/session/prompt.ts
+5−66packages/opencode/test/session/structured-output.test.ts
+0−230