Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/_core_features/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ end

Not all models support structured output. Currently supported:
- **OpenAI**: GPT-4o, GPT-4o-mini, and newer models
- **Anthropic**: No native structured output support. You can simulate it with tool definitions or careful prompting
- **Anthropic**: Claude 4.5+ models (Haiku, Sonnet, Opus)
- **Gemini**: Gemini 1.5 Pro/Flash and newer

Models that don't support structured output:
Expand Down
18 changes: 14 additions & 4 deletions lib/ruby_llm/providers/anthropic/capabilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ def supports_vision?(model_id)
end

def supports_functions?(model_id)
model_id.match?(/claude-3/)
!model_id.match?(/claude-[12]/)
end

def supports_json_mode?(model_id)
model_id.match?(/claude-3/)
!model_id.match?(/claude-[12]/)
end

def supports_structured_output?(model_id)
match = model_id.match(/claude-(?:sonnet|opus|haiku)-(\d+)-(\d+)/)
return false unless match

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

def supports_extended_thinking?(model_id)
Expand Down Expand Up @@ -92,12 +101,13 @@ def modalities_for(model_id)
def capabilities_for(model_id)
capabilities = ['streaming']

if model_id.match?(/claude-3/)
unless model_id.match?(/claude-[12]/)
capabilities << 'function_calling'
capabilities << 'batch'
end

capabilities << 'reasoning' if model_id.match?(/claude-3-7|-4/)
capabilities << 'structured_output' if supports_structured_output?(model_id)
capabilities << 'reasoning' if model_id.match?(/claude-3-7-sonnet|claude-(?:sonnet|opus|haiku)-4/)
capabilities << 'citations' if model_id.match?(/claude-3\.5|claude-3-7/)
capabilities
end
Expand Down
14 changes: 11 additions & 3 deletions lib/ruby_llm/providers/anthropic/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def completion_url
'v1/messages'
end

def render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil) # rubocop:disable Metrics/ParameterLists,Lint/UnusedMethodArgument
def render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil) # rubocop:disable Metrics/ParameterLists
system_messages, chat_messages = separate_messages(messages)
system_content = build_system_content(system_messages)

build_base_payload(chat_messages, model, stream, thinking).tap do |payload|
add_optional_fields(payload, system_content:, tools:, temperature:)
add_optional_fields(payload, system_content:, tools:, temperature:, schema:)
end
end

Expand Down Expand Up @@ -59,10 +59,18 @@ def build_base_payload(chat_messages, model, stream, thinking)
payload
end

def add_optional_fields(payload, system_content:, tools:, temperature:)
def add_optional_fields(payload, system_content:, tools:, temperature:, schema: nil)
payload[:tools] = tools.values.map { |t| Tools.function_for(t) } if tools.any?
payload[:system] = system_content unless system_content.empty?
payload[:temperature] = temperature unless temperature.nil?
payload[:output_config] = build_output_config(schema) if schema
end

def build_output_config(schema)
normalized = RubyLLM::Utils.deep_dup(schema)
normalized.delete(:strict)
normalized.delete('strict')
{ format: { type: 'json_schema', schema: normalized } }
end

def parse_completion_response(response)
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading