Skip to content

feat: add structured output support to ChatAnthropic#474

Merged
brainlid merged 1 commit intobrainlid:mainfrom
nallwhy:anthoropic_structured_output
Feb 28, 2026
Merged

feat: add structured output support to ChatAnthropic#474
brainlid merged 1 commit intobrainlid:mainfrom
nallwhy:anthoropic_structured_output

Conversation

@nallwhy
Copy link
Contributor

@nallwhy nallwhy commented Feb 24, 2026

Summary

  • Add json_response and json_schema fields to ChatAnthropic for Anthropic's native structured outputs API (output_config.format)
  • Require json_schema when json_response: true via changeset validation, since Anthropic has no schema-less JSON mode unlike OpenAI
  • No model allowlist — unsupported models will receive a clear API error, keeping the implementation future-proof

References

Copilot AI review requested due to automatic review settings February 24, 2026 06:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds native structured output support to ChatAnthropic using Anthropic's output_config.format API. The implementation allows developers to constrain Claude's responses to follow specific JSON schemas, which is supported on Claude Opus 4.6, Sonnet 4.6, Sonnet 4.5, Opus 4.5, and Haiku 4.5.

Changes:

  • Added json_response (boolean) and json_schema (map) fields to ChatAnthropic model with appropriate defaults
  • Implemented validation requiring json_schema when json_response: true, reflecting Anthropic's requirement for schema (unlike OpenAI/DeepSeek which support schema-less JSON mode)
  • Updated API formatting to include output_config with proper structure when structured outputs are enabled
  • Added comprehensive documentation with examples and compatibility notes
  • Added serialization support for the new fields
  • Included comprehensive test coverage for validation, API formatting, and serialization

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/chat_models/chat_anthropic.ex Core implementation: added fields, validation, API formatting (set_output_config), serialization, and comprehensive documentation for structured outputs
test/chat_models/chat_anthropic_test.exs Comprehensive test coverage: field initialization, validation errors, API formatting, and serialization tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@brainlid
Copy link
Owner

Nice @nallwhy! Thanks!
❤️💛💙💜

@brainlid brainlid merged commit aba572c into brainlid:main Feb 28, 2026
5 of 6 checks passed
brainlid added a commit that referenced this pull request Mar 4, 2026
* main:
  feat: add file_id support to ChatAnthropic (#475)
  fix: handle nil content in run_message_processors (#473)
  feat: add structured output support to ChatAnthropic (#474)
  fix: Fix verbosity parameter and better error handling (#476)
  formatting
  feat(gemini): adding inline_data support for pdf and csv (#478)
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