From 75da9c820e7e4e611333cad52bb5644fe2a3a647 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 11 Nov 2026 04:49:36 -0800 Subject: [PATCH] fix(bedrock): walk legacy refs inside a $defs ref and skip malformed definitions on Claude A $ref the legacy-only pass does not resolve is now walked like any other node, so a definitions ref inside a $defs entry is inlined instead of dangling. A definitions or components value that is not an object is dropped instead of failing the request --- .../prompt_templates/common_utils.py | 40 +++++++++---------- ...llm_core_utils_prompt_templates_factory.py | 27 +++++++++++++ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index ffcdad5901a..40f86905ed0 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -934,12 +934,12 @@ _LEGACY_DEFS_KEYS: Final = ("definitions",) _LEGACY_REF_PREFIXES: Final = ("#/definitions/", "#/components/schemas/") -def _defs_block(value: object) -> Mapping[str, object]: +def _mapping_or_empty(value: object) -> Mapping[str, object]: return value if isinstance(value, dict) else {} def _local_defs(node: Mapping[str, object], defs_keys: tuple[str, ...]) -> Mapping[str, object]: - return dict(chain.from_iterable(_defs_block(node.get(key)).items() for key in defs_keys)) + return dict(chain.from_iterable(_mapping_or_empty(node.get(key)).items() for key in defs_keys)) def _scope_with_local_defs(scope: _DefsScope, local_defs: Mapping[str, object]) -> _DefsScope: @@ -1003,10 +1003,8 @@ def unpack_defs( # ----------------------------- dict ----------------------------- if isinstance(node, dict): # --- Case 1: this node *is* a reference --- - if "$ref" in node: - ref = node["$ref"] - if not isinstance(ref, str) or (legacy_only and not ref.startswith(_LEGACY_REF_PREFIXES)): - continue + ref = node.get("$ref") + if isinstance(ref, str) and (not legacy_only or ref.startswith(_LEGACY_REF_PREFIXES)): ref_name = ref.split("/")[-1] # Check for circular reference in the resolution chain @@ -1073,11 +1071,16 @@ def unpack_defs( return inlined_bytes -def _has_legacy_defs(schema: object) -> bool: - if not isinstance(schema, dict): - return False - components: Final = schema.get("components") - return "definitions" in schema or (isinstance(components, dict) and isinstance(components.get("schemas"), dict)) +def _legacy_defs(schema: object) -> Mapping[str, object] | None: + node: Final = _mapping_or_empty(schema) + components: Final = _mapping_or_empty(node.get("components")) + if "definitions" not in node and not isinstance(components.get("schemas"), dict): + return None + # On key collision, ``definitions`` wins over ``components.schemas`` -- + # ``unpack_defs`` keys refs by last path segment so a single name can only + # resolve to one body, and ``definitions`` is the JSON-Schema-native + # namespace. + return {**_mapping_or_empty(components.get("schemas")), **_mapping_or_empty(node.get("definitions"))} # Schema-bomb budget for ``$ref`` inlining: cap the cumulative JSON-byte @@ -1111,7 +1114,7 @@ def unpack_legacy_defs( schemas cannot expand into a schema-bomb before reaching the upstream provider -- raises ``ValueError`` on overflow. """ - if not _has_legacy_defs(schema): + if _legacy_defs(schema) is None: return schema if copy: import copy as _copy @@ -1122,15 +1125,12 @@ def unpack_legacy_defs( def inline_legacy_defs(schema: dict, max_inlined_bytes: int) -> int: - if not _has_legacy_defs(schema): + defs: Final = _legacy_defs(schema) + if defs is None: return 0 - # On key collision, ``definitions`` wins over ``components.schemas`` -- - # ``unpack_defs`` keys refs by last path segment so a single name can only - # resolve to one body, and ``definitions`` is the JSON-Schema-native - # namespace. - defs: Final = schema.pop("components", {}).get("schemas") or {} - defs.update(schema.pop("definitions", None) or {}) - return unpack_defs(schema, defs, max_inlined_bytes=max_inlined_bytes, legacy_only=True) + schema.pop("components", None) + schema.pop("definitions", None) + return unpack_defs(schema, dict(defs), max_inlined_bytes=max_inlined_bytes, legacy_only=True) def sanitize_input_schema_for_anthropic(input_schema: dict) -> "AnthropicInputSchema": diff --git a/tests/unit/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py b/tests/unit/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py index 4fed187ec32..27d967270f1 100644 --- a/tests/unit/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py +++ b/tests/unit/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py @@ -2780,6 +2780,20 @@ def test_bedrock_tools_pt_inlines_legacy_definitions_for_claude(legacy_defs: dic assert sent == {"type": "object", "properties": {"home": _ADDRESS_SCHEMA}, "required": ["home"]} +@pytest.mark.parametrize( + "legacy_defs", + [{"definitions": 5}, {"components": "openapi", "definitions": {"Address": _ADDRESS_SCHEMA}}], + ids=["definitions_not_a_map", "components_not_a_map"], +) +def test_bedrock_tools_pt_drops_a_malformed_legacy_defs_block_for_claude(legacy_defs: dict[str, object]) -> None: + parameters: Final = {"type": "object", "properties": {"name": {"type": "string"}}, **legacy_defs} + tools: Final = [{"type": "function", "function": {"name": "save_name", "parameters": parameters}}] + + sent: Final = _bedrock_tools_pt(tools, model="us.anthropic.claude-sonnet-5-5")[0]["toolSpec"]["inputSchema"]["json"] + + assert sent == {"type": "object", "properties": {"name": {"type": "string"}}, "required": []} + + def test_bedrock_tools_pt_sends_strict_claude_tools_with_their_defs() -> None: parameters: Final = { "type": "object", @@ -2827,6 +2841,19 @@ def test_bedrock_tools_pt_follows_a_chain_of_root_refs_for_claude() -> None: assert sent == {**_ADDRESS_SCHEMA, "$defs": defs} +def test_bedrock_tools_pt_inlines_legacy_refs_under_a_root_defs_ref_for_claude() -> None: + defs: Final = { + "Home": {"type": "object", "properties": {"address": {"$ref": "#/definitions/Address"}}, "required": ["address"]} + } + parameters: Final = {"$ref": "#/$defs/Home", "$defs": defs, "definitions": {"Address": _ADDRESS_SCHEMA}} + tools: Final = [{"type": "function", "function": {"name": "save_home", "parameters": parameters}}] + + sent: Final = _bedrock_tools_pt(tools, model="us.anthropic.claude-sonnet-5-5")[0]["toolSpec"]["inputSchema"]["json"] + + home: Final = {"type": "object", "properties": {"address": _ADDRESS_SCHEMA}, "required": ["address"]} + assert sent == {**home, "$defs": {"Home": home}} + + def test_bedrock_tools_pt_inlines_only_legacy_refs_for_claude() -> None: parameters: Final = { "type": "object",