opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat: support claude agent SDK-style structured outputs in the OpenCode SDK

Open K-Mistele opened this issue 5 days ago • 3 comments

What does this PR do?

This PR adds support for structured generation in the OpenCode SDK (v2) by implementing an injected StructuredOutput tool with an arbitrary user-defined JSON schema.

This implementation approach follows Claude Code's approach as indicated by debugging and proxy analysis.

When set, toolChoice is forced to "required" and the agent loop is exited once the input to the tool call passes validation against the user-supplied JSON Schema.

Retries are supported, and retry count is customizable (important for smaller models or providers which have poor support for structured generation!).

Prompts and descriptions are based on those used in the Claude Agent SDK

You can learn more about structured outputs in the Claude Agent SDK here

Closes #5639

How did you verify your code works?

This PR contains both unit tests and integration tests.

K-Mistele avatar Jan 13 '26 08:01 K-Mistele