Skip to content

Conversation

@K-Mistele
Copy link

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 and others added 7 commits January 12, 2026 20:09
Add outputFormat option to session.prompt() for requesting structured JSON
output. When type is 'json_schema', injects a StructuredOutput tool that
validates model output against the provided schema.

- Add OutputFormat schema types (text, json_schema) to message-v2.ts
- Add structured_output field to AssistantMessage
- Add StructuredOutputError for validation failures
- Implement createStructuredOutputTool helper in prompt.ts
- Integrate structured output into agent loop with retry support
- Regenerate OpenAPI spec with new types
- Add unit tests for schema validation
Regenerate SDK types to include outputFormat and structured_output fields.
- Fix loop exit condition to check processor.message.finish instead of
  result === "stop" (processor.process() returns "continue" on normal exit)
- Add system prompt instruction when json_schema mode is enabled to ensure
  model calls the StructuredOutput tool
- Add integration tests for structured output functionality
- Fix test Session.messages call to use object parameter format
Document the outputFormat feature for requesting structured JSON output:
- Basic usage example with JSON schema
- Output format types (text, json_schema)
- Schema configuration options (type, schema, retryCount)
- Error handling for StructuredOutputError
- Best practices for using structured output
When structured output is requested, the model must call the
StructuredOutput tool instead of responding with plain text.

Changes:
- Add toolChoice parameter to LLM.StreamInput
- Pass toolChoice: "required" to streamText when outputFormat is json_schema
- This forces the model to call a tool, ensuring structured output is captured
Merge upstream changes while preserving structured output feature:
- Keep tools deprecation notice from upstream
- Keep bypassAgentCheck parameter from upstream
- Keep variant field on user messages from upstream
- Preserve outputFormat and StructuredOutput tool injection

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@K-Mistele
Copy link
Author

cc @thdxr :)

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Support for Structured Outputs in the OpenCode SDK

1 participant