mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-08 03:08:45 +00:00
fix: add additionalProperties:false and resolve $defs/$ref in Anthropic output_format schemas
Anthropic API now requires additionalProperties=false for all object-type schemas in output_format. Also resolve $defs/$ref references by inlining them using unpack_defs before sending to Anthropic, since Anthropic doesn't support external schema references. Fixes: llm_translation_testing Anthropic JSON schema failures Co-authored-by: Ishaan Jaff <ishaan-jaff@users.noreply.github.com>
This commit is contained in:
parent
84dd6d8d47
commit
0a3ff899dd
1 changed files with 18 additions and 0 deletions
|
|
@ -317,6 +317,11 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
|
|||
else:
|
||||
result[key] = value
|
||||
|
||||
# Anthropic requires additionalProperties=false for object schemas
|
||||
# See: https://docs.anthropic.com/en/docs/build-with-claude/structured-outputs
|
||||
if result.get("type") == "object" and "additionalProperties" not in result:
|
||||
result["additionalProperties"] = False
|
||||
|
||||
return result
|
||||
|
||||
def get_json_schema_from_pydantic_object(
|
||||
|
|
@ -770,6 +775,19 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
|
|||
if json_schema is None:
|
||||
return None
|
||||
|
||||
# Resolve $ref/$defs before filtering — Anthropic doesn't support
|
||||
# external schema references (e.g., /$defs/CalendarEvent).
|
||||
import copy
|
||||
|
||||
from litellm.litellm_core_utils.prompt_templates.common_utils import (
|
||||
unpack_defs,
|
||||
)
|
||||
|
||||
json_schema = copy.deepcopy(json_schema)
|
||||
defs = json_schema.pop("$defs", json_schema.pop("definitions", {}))
|
||||
if defs:
|
||||
unpack_defs(json_schema, defs)
|
||||
|
||||
# Filter out unsupported fields for Anthropic's output_format API
|
||||
filtered_schema = self.filter_anthropic_output_schema(json_schema)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue