From 0a3ff899ddd928a42d9a30e23fc9c8dc8195291d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 7 Mar 2026 06:48:26 +0000 Subject: [PATCH] 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 --- litellm/llms/anthropic/chat/transformation.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 5227d369027..04b27e87821 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -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)