Skip to content

Add Anthropic structured output support#608

Open
hiasinho wants to merge 3 commits intocrmne:mainfrom
hiasinho:feature/anthropic-structured-output
Open

Add Anthropic structured output support#608
hiasinho wants to merge 3 commits intocrmne:mainfrom
hiasinho:feature/anthropic-structured-output

Conversation

@hiasinho
Copy link

What this does

Adds native structured output support for Anthropic Claude 4.5+ models using the GA output_config API. When with_schema is called on a chat using a supported Anthropic model, the schema is sent via output_config.format with type json_schema, and the response is automatically parsed as JSON.

Because I can't contribute to PR #528, I opened this one.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • I read the Contributing Guide
  • This aligns with RubyLLM's focus on LLM communication
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my specific use case

Required for new features

PRs for new features or enhancements without a prior approved issue will be closed.

Quality check

  • I ran overcommit --install and all hooks pass
  • I tested my changes thoroughly
    • For provider changes: Re-recorded VCR cassettes with bundle exec rake vcr:record[provider_name]
    • All tests pass: bundle exec rspec
  • I updated documentation if needed
  • I didn't modify auto-generated files manually (models.json, aliases.json)

AI-generated code

  • I used AI tools to help write this code
  • I have reviewed and understand all generated code (required if above is checked)

API changes

  • Breaking change
  • New public methods/classes
  • Changed method signatures
  • No API changes

Wire output_config into the Anthropic chat payload when a schema is
provided, using the GA format ({ format: { type: 'json_schema', schema } })
with no beta headers required. The build_output_config method deep-dups the
schema and strips :strict keys that Anthropic rejects.

Fix model detection regexes in capabilities to match Claude 4+ model IDs
(supports_functions?, supports_json_mode?, capabilities_for) and add
supports_structured_output? for Claude 4.5+ models.

Re-record Anthropic VCR cassettes.
@hiasinho hiasinho changed the title Feature/anthropic structured output Add Anthropic structured output support Feb 15, 2026
Comment on lines 41 to 42
def supports_structured_output?(model_id)
model_id.match?(/claude-(?:sonnet|opus|haiku)-4-[56789]/)
Copy link

Choose a reason for hiding this comment

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

  # Structured output supported on Claude 4.5+ and all future major versions (5+).
  # Uses \d{1,2} with boundary anchor to avoid matching date suffixes.
  def supports_structured_output?(model_id)
    match = model_id.match(/claude-(?:opus|sonnet|haiku)-(\d+)-(\d{1,2})(?:\b|-)/)
    return false unless match

    major, minor = match[1].to_i, match[2].to_i
    major > 4 || (major == 4 && minor >= 5)
  end

Copy link

@llenodo llenodo left a comment

Choose a reason for hiding this comment

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

Nice PR I almost implemented the same thing before finding this.

One suggestion to make it more future proof which assumes future versions of claude (5+) will support structured outputs so we wont need to come back to this later

@KieranP
Copy link

KieranP commented Feb 17, 2026

@hiasinho Any chance this PR could also include structured output for anthropic models through AWS Bedrock? The AWS bedrock integration was recently rewritten to use converse API, which support structured output: https://docs.aws.amazon.com/bedrock/latest/userguide/structured-output.html

@hiasinho
Copy link
Author

Nice PR I almost implemented the same thing before finding this.

One suggestion to make it more future proof which assumes future versions of claude (5+) will support structured outputs so we wont need to come back to this later

Done!

@hiasinho
Copy link
Author

@hiasinho Any chance this PR could also include structured output for anthropic models through AWS Bedrock? The AWS bedrock integration was recently rewritten to use converse API, which support structured output: https://docs.aws.amazon.com/bedrock/latest/userguide/structured-output.html

Would love to do it, but I don't use AWS. So no creds to create VCRs. Tests will fail. Feel free to build on it.

@hiasinho hiasinho requested a review from llenodo February 18, 2026 07:32
llenodo added a commit to llenodo/ruby_llm that referenced this pull request Feb 18, 2026
Extend the Bedrock Converse API provider to support structured output
via outputConfig.textFormat, building on crmne#608's Anthropic implementation.

Key differences from the direct Anthropic API: schema must be a JSON
string (not a Hash), nested under structure.jsonSchema with a required
name field, and uses camelCase Converse API naming.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
llenodo added a commit to llenodo/ruby_llm that referenced this pull request Feb 18, 2026
Extend the Bedrock Converse API provider to support structured output
via outputConfig.textFormat, building on crmne#608's Anthropic implementation.

Key differences from the direct Anthropic API: schema must be a JSON
string (not a Hash), nested under structure.jsonSchema with a required
name field, and uses camelCase Converse API naming.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@llenodo
Copy link

llenodo commented Feb 18, 2026

@KieranP see #596 for bedrock support

PR is currently closed per community guidelines

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.

3 participants