Add structured output support for Anthropic provider#528
Add structured output support for Anthropic provider#528vojto wants to merge 8 commits intocrmne:mainfrom
Conversation
|
Do you plan to add or modify specs that hit the real API and produce new/updated VCR cassettes? |
|
@tpaulshippy that should be done now. I edited I modified |
|
ping @crmne |
|
@crmne supporting PR has been merged danielfriis/ruby_llm-schema#28 |
Implements structured outputs using Anthropic's structured-outputs-2025-11-13 beta API. Changes: - Add structured output capability detection for Claude 4+ models - Implement output_format parameter with json_schema type in chat payload - Add anthropic-beta header handling to append structured-outputs beta version - Add comprehensive specs for structured output functionality - Refactor model version detection helpers (claude3_or_newer, claude4_or_newer) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Move schema validation logic into dedicated class to reduce complexity in the Chat module and improve separation of concerns. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
The spec file legitimately tests two separate classes (Provider and Chat module) that are closely related but have distinct responsibilities. Disabling this cop for this file is the appropriate solution. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
ec98fd6 to
1e2215a
Compare
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Waiting for this also! |
|
Same! |
|
Same. |
|
The main branch has changed quite a bit, so if anyone wants to get it up to date, please feel free to do so. |
## 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 - [x] New feature - [ ] Breaking change - [x] Documentation - [ ] Performance improvement ## Scope check - [x] I read the [Contributing Guide](https://github.com/crmne/ruby_llm/blob/main/CONTRIBUTING.md) - [x] This aligns with RubyLLM's focus on **LLM communication** - [x] This isn't application-specific logic that belongs in user code - [x] This benefits most users, not just my specific use case ## Required for new features - [ ] I opened an issue **before** writing code and received maintainer approval - [x] Linked issue: #501 **PRs for new features or enhancements without a prior approved issue will be closed.** ## Quality check - [x] I ran `overcommit --install` and all hooks pass - [x] I tested my changes thoroughly - [x] For provider changes: Re-recorded VCR cassettes with `bundle exec rake vcr:record[provider_name]` - [x] All tests pass: `bundle exec rspec` - [x] I updated documentation if needed - [x] I didn't modify auto-generated files manually (`models.json`, `aliases.json`) ## AI-generated code - [x] I used AI tools to help write this code - [x] I have reviewed and understand all generated code (required if above is checked) ## API changes - [ ] Breaking change - [x] New public methods/classes - [ ] Changed method signatures - [ ] No API changes --------- Co-authored-by: Carmine Paolino <carmine@paolino.me>
|
Merged same functionality in #608 |
Hey, I tried to support structured output for Anthropic.
The only problem is that RubyLLM::Schema currently adds
strict: trueto each schema it creates. Here's a PR in that repo to fix that: danielfriis/ruby_llm-schema#28Until that is merged, the workaround is to create this subclass of RubyLLM::Schema:
AI generated:
Summary
structured-outputs-2025-11-13beta APIChanges
completemethod override to inject the structured-outputs beta header when schema is providedsupports_structured_output?method to detect Claude 4+ models that support structured outputsoutput_formatparameter withjson_schematype in payload renderingclaude3_or_newer?andclaude4_or_newer?helper methods for cleaner version detectionTest plan
🤖 Generated with Claude Code