Skip to content

Use Anthropic native output_config.format for structured outputs on supported models#385

Draft
dpdiliberto wants to merge 1 commit intomainfrom
deven/anthropic-native-structured-outputs
Draft

Use Anthropic native output_config.format for structured outputs on supported models#385
dpdiliberto wants to merge 1 commit intomainfrom
deven/anthropic-native-structured-outputs

Conversation

@dpdiliberto
Copy link
Contributor

Summary

This PR updates the proxy to use Anthropic's native output_config.format API for structured outputs on supported models, ensuring proper validation of required fields when strict: true is configured (https://platform.claude.com/docs/en/build-with-claude/structured-outputs).

Problem

Currently, structured outputs are converted to a tool-based approach for all Anthropic models. This doesn't properly enforce required fields from JSON schemas, even when strict: true was set, causing required fields to be omitted from responses.

Solution

For models that support native structured outputs (Claude Haiku 4.5, Sonnet 4.5, Opus 4.5, and Opus 4.6), we can use Anthropic's native output_config.format API instead of converting to tools. This ensures proper schema validation and enforcement of required fields.

Older models continue to use the tool-based fallback approach for backward compatibility.

Changes

  • Added model detection to identify models that support native structured outputs
  • Use output_config.format API for supported models with json_schema type
  • Maintain tool-based fallback for older models and json_object format
  • No beta headers required (structured outputs are generally available)

Testing

Tested with Claude Sonnet 4.5 using structured outputs with strict: true - required fields are now properly enforced.

@vercel
Copy link

vercel bot commented Feb 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ai-proxy Ready Ready Preview, Comment Feb 6, 2026 10:24pm

Request Review

@dpdiliberto dpdiliberto marked this pull request as draft February 6, 2026 22:24
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7aadaeaab5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +2623 to +2627
// Use Anthropic's native output_config.format API for proper strict validation
params.output_config = {
format: {
type: "json_schema",
schema: parsed.data.json_schema.schema,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve json_schema strict when building output_config

When response_format is json_schema and the model is in the native path, the proxy only forwards schema into output_config.format and drops json_schema.strict (and other metadata). That means callers who set strict: true will silently lose strict enforcement on these models, which is the core guarantee they asked for. This path is distinct from the other response_format mappings in this file that propagate strict, so the omission is likely unintended and will surface whenever clients rely on strict validation for required fields.

Useful? React with 👍 / 👎.

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.

1 participant