From 4d83b2bc7b810bc82f29d604c1454dc7643bd2ff Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Tue, 13 Oct 2026 21:20:06 -0700 Subject: [PATCH 1/9] fix(anthropic): close nullable and untyped object schemas in output_format Move the output_format schema shaping into litellm/llms/anthropic/output_schema.py so every Claude structured-output path can share it. Objects typed ["object", "null"] and nodes that declare properties without a type are now closed like plain objects, since the API rejects both, and string formats outside the supported set move into the description like other unsupported keywords --- litellm/llms/anthropic/chat/transformation.py | 168 +-------------- litellm/llms/anthropic/output_schema.py | 196 ++++++++++++++++++ .../anthropic/test_anthropic_output_schema.py | 69 ++++++ .../anthropic/test_anthropic_schema_filter.py | 43 ++++ 4 files changed, 313 insertions(+), 163 deletions(-) create mode 100644 litellm/llms/anthropic/output_schema.py create mode 100644 tests/unit/llms/anthropic/test_anthropic_output_schema.py diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index c43e5f9b05b..129e6d527a9 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -1,8 +1,7 @@ import json import re import time -from collections.abc import Callable, Mapping, Sequence -from types import MappingProxyType +from collections.abc import Mapping, Sequence from typing import TYPE_CHECKING, Any, ClassVar, Final, NoReturn, cast import httpx @@ -35,6 +34,7 @@ from litellm.litellm_core_utils.prompt_templates.mid_conversation_system import place_mid_conversation_system, split_leading_system_run, ) +from litellm.llms.anthropic.output_schema import shape_claude_output_schema from litellm.llms.base_llm.base_utils import ( ResponseFormatSchema, as_json_object, @@ -162,31 +162,6 @@ class _AnthropicToolResultBlock(TypedDict, total=False): content: ReadOnly[object] -_ENUM_TYPE_CHECKS: Final[Mapping[object, Callable[[object], bool]]] = MappingProxyType( - { - "null": lambda v: v is None, - "boolean": lambda v: isinstance(v, bool), - "integer": lambda v: isinstance(v, int) and not isinstance(v, bool), - "number": lambda v: isinstance(v, (int, float)) and not isinstance(v, bool), - "string": lambda v: isinstance(v, str), - "array": lambda v: isinstance(v, list), - "object": lambda v: isinstance(v, dict), - } -) - - -def _enum_conflicts_with_declared_type(schema: Mapping[str, object]) -> bool: - """Whether ``schema``'s ``enum`` cannot match its declared ``type``.""" - enum_values: Final = schema.get("enum") - declared_type: Final = schema.get("type") - if not isinstance(enum_values, list) or declared_type is None: - return False - if isinstance(declared_type, list): - return True - check: Final = _ENUM_TYPE_CHECKS.get(declared_type) - return check is not None and not all(check(value) for value in enum_values) - - # Single, internal-only key on ``litellm_params`` used to thread the per- # request reverse map (sanitized -> original) from request build to response # parsing. ``litellm_params`` is never serialized to a provider; ``optional_ @@ -571,142 +546,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): return params @staticmethod - def filter_anthropic_output_schema(schema: dict[str, Any]) -> dict[str, Any]: - """ - Filter out unsupported fields from JSON schema for Anthropic's output_format API. - - Anthropic's output_format doesn't support certain JSON schema properties. - These are constraints that cannot be enforced by the constrained-decoding - grammar Anthropic compiles the schema into, so the API rejects them with a - 400 ``invalid_request_error`` (e.g. "output_format.schema: For 'array' type, - property 'uniqueItems' is not supported"): - - maxItems/minItems/uniqueItems/contains/minContains/maxContains/prefixItems: array constraints - - minimum/maximum/exclusiveMinimum/exclusiveMaximum/multipleOf: numeric constraints - - minLength/maxLength: string constraints - - minProperties/maxProperties/patternProperties/propertyNames: object constraints - - dependentRequired/dependentSchemas/unevaluatedProperties: object constraints - - if/then/else/not: conditional and negation keywords - - ``oneOf`` is also rejected ("Schema type 'oneOf' is not supported") and is - rewritten to ``anyOf``, matching the Anthropic SDK. Unknown keywords are - ignored by the API, so anything not listed here passes through untouched. - - This mirrors the transformation done by the Anthropic Python SDK. - See: https://platform.claude.com/docs/en/build-with-claude/structured-outputs#how-sdk-transformation-works - - The SDK approach: - 1. Remove unsupported constraints from schema - 2. Add constraint info to description (e.g., "Must be at least 100") - 3. Validate responses against original schema - Args: - schema: The JSON schema dictionary to filter - - Returns: - A new dictionary with unsupported fields removed and descriptions updated - - Related issues: - - https://github.com/BerriAI/litellm/issues/19444 - """ - if not isinstance(schema, dict): - return schema - - constraint_labels: Final = { - "minItems": "minimum number of items: {}", - "maxItems": "maximum number of items: {}", - "uniqueItems": "all array items must be unique", - "contains": "array must contain an item matching: {}", - "minContains": "minimum number of matching items: {}", - "maxContains": "maximum number of matching items: {}", - "prefixItems": "leading items must match, in order: {}", - "minimum": "minimum value: {}", - "maximum": "maximum value: {}", - "exclusiveMinimum": "exclusive minimum value: {}", - "exclusiveMaximum": "exclusive maximum value: {}", - "multipleOf": "must be a multiple of {}", - "minLength": "minimum length: {}", - "maxLength": "maximum length: {}", - "minProperties": "minimum number of properties: {}", - "maxProperties": "maximum number of properties: {}", - "patternProperties": "properties whose names match each pattern must satisfy: {}", - "propertyNames": "property names must satisfy: {}", - "dependentRequired": "dependent required properties: {}", - "dependentSchemas": "dependent schemas: {}", - "unevaluatedProperties": "unevaluated properties must satisfy: {}", - "if": "conditional (if): {}", - "then": "conditional (then): {}", - "else": "conditional (else): {}", - "not": "must not match: {}", - } - unsupported_fields: Final = set(constraint_labels) - - # Build description additions from removed constraints. Iterating - # constraint_labels (not the set) keeps the note order deterministic across - # processes, so identical requests serialize identically regardless of - # PYTHONHASHSEED and stay cache-friendly. - constraint_descriptions: Final[list] = [] - for field, label in constraint_labels.items(): - if field not in schema: - continue - value = schema[field] - # A falsy boolean constraint (e.g. ``uniqueItems: false``) imposes no - # real requirement, so don't add a misleading advisory note for it. - if isinstance(value, bool) and not value: - continue - # Sub-schema constraints (e.g. ``contains``) are serialized as JSON so - # the advisory note preserves what the constraint actually required, - # instead of just noting that it existed. - note_value = json.dumps(value) if isinstance(value, (dict, list)) else value - constraint_descriptions.append(label.format(note_value)) - - result: Final[dict[str, Any]] = {} - - # Update description with removed constraint info - if constraint_descriptions: - existing_desc: Final = schema.get("description", "") - constraint_note: Final = "Note: " + ", ".join(constraint_descriptions) + "." - if existing_desc: - result["description"] = existing_desc + " " + constraint_note - else: - result["description"] = constraint_note - - drops_conflicting_type: Final = _enum_conflicts_with_declared_type(schema) - - for key, value in schema.items(): - if key in unsupported_fields: - continue - if key == "type" and drops_conflicting_type: - continue - if key == "description" and "description" in result: - # Already handled above - continue - - if key == "properties" and isinstance(value, dict): - result[key] = {k: AnthropicConfig.filter_anthropic_output_schema(v) for k, v in value.items()} - elif key == "items" and isinstance(value, dict): - result[key] = AnthropicConfig.filter_anthropic_output_schema(value) - elif key == "$defs" and isinstance(value, dict): - result[key] = {k: AnthropicConfig.filter_anthropic_output_schema(v) for k, v in value.items()} - elif key == "anyOf" and isinstance(value, list): - result["anyOf"] = result.get("anyOf", []) + [ - AnthropicConfig.filter_anthropic_output_schema(item) for item in value - ] - elif key == "allOf" and isinstance(value, list): - result[key] = [AnthropicConfig.filter_anthropic_output_schema(item) for item in value] - elif key == "oneOf" and isinstance(value, list): - # Anthropic rejects oneOf ("Schema type 'oneOf' is not supported"); - # the Anthropic SDK rewrites it to anyOf, so do the same. - result["anyOf"] = result.get("anyOf", []) + [ - AnthropicConfig.filter_anthropic_output_schema(item) for item in value - ] - 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": - result["additionalProperties"] = False - - return result + def filter_anthropic_output_schema(schema: Mapping[str, object]) -> dict[str, object]: + """Shape ``schema`` for Anthropic's ``output_format`` API. See ``shape_claude_output_schema``.""" + return shape_claude_output_schema(schema) def get_json_schema_from_pydantic_object(self, response_format: type[BaseModel] | dict | None) -> dict | None: return type_to_response_format_param( diff --git a/litellm/llms/anthropic/output_schema.py b/litellm/llms/anthropic/output_schema.py new file mode 100644 index 00000000000..0b2f66a8767 --- /dev/null +++ b/litellm/llms/anthropic/output_schema.py @@ -0,0 +1,196 @@ +""" +Shape a JSON schema into what Claude's structured-output grammar accepts, for +Anthropic ``output_format`` and Bedrock Converse and Invoke alike. Mirrors the +Anthropic SDK transform: +https://platform.claude.com/docs/en/build-with-claude/structured-outputs#how-sdk-transformation-works +""" + +import json +from collections.abc import Callable, Iterator, Mapping +from types import MappingProxyType +from typing import Final + +from pydantic import TypeAdapter, ValidationError + +# anthropic SDK ``lib/_parse/_transform.py`` SupportedStringFormats, checked 2026-10-13 +SUPPORTED_STRING_FORMATS: Final = frozenset( + {"date-time", "time", "date", "duration", "email", "hostname", "uri", "ipv4", "ipv6", "uuid"} +) + +_CONSTRAINT_NOTES: Final[Mapping[str, str]] = MappingProxyType( + { + "minItems": "minimum number of items: {}", + "maxItems": "maximum number of items: {}", + "uniqueItems": "all array items must be unique", + "contains": "array must contain an item matching: {}", + "minContains": "minimum number of matching items: {}", + "maxContains": "maximum number of matching items: {}", + "prefixItems": "leading items must match, in order: {}", + "minimum": "minimum value: {}", + "maximum": "maximum value: {}", + "exclusiveMinimum": "exclusive minimum value: {}", + "exclusiveMaximum": "exclusive maximum value: {}", + "multipleOf": "must be a multiple of {}", + "minLength": "minimum length: {}", + "maxLength": "maximum length: {}", + "minProperties": "minimum number of properties: {}", + "maxProperties": "maximum number of properties: {}", + "patternProperties": "properties whose names match each pattern must satisfy: {}", + "propertyNames": "property names must satisfy: {}", + "dependentRequired": "dependent required properties: {}", + "dependentSchemas": "dependent schemas: {}", + "unevaluatedProperties": "unevaluated properties must satisfy: {}", + "if": "conditional (if): {}", + "then": "conditional (then): {}", + "else": "conditional (else): {}", + "not": "must not match: {}", + } +) + +_SCHEMA_MAP_KEYS: Final = frozenset({"properties", "$defs", "definitions"}) +_COMPOSITION_KEYS: Final = frozenset({"anyOf", "oneOf", "allOf", "$ref"}) + +_ENUM_TYPE_CHECKS: Final[Mapping[str, Callable[[object], bool]]] = MappingProxyType( + { + "null": lambda v: v is None, + "boolean": lambda v: isinstance(v, bool), + "integer": lambda v: isinstance(v, int) and not isinstance(v, bool), + "number": lambda v: isinstance(v, (int, float)) and not isinstance(v, bool), + "string": lambda v: isinstance(v, str), + "array": lambda v: isinstance(v, list), + "object": lambda v: isinstance(v, dict), + } +) + +_SCHEMA: Final = TypeAdapter(Mapping[str, object]) +_ITEMS: Final = TypeAdapter(list[object]) +_TYPE_NAMES: Final = TypeAdapter(list[str]) + + +def _as_schema(value: object) -> Mapping[str, object] | None: + try: + return _SCHEMA.validate_python(value) + except ValidationError: + return None + + +def _as_items(value: object) -> list[object] | None: + try: + return _ITEMS.validate_python(value) + except ValidationError: + return None + + +def _declared_types(value: object) -> frozenset[str]: + if isinstance(value, str): + return frozenset((value,)) + try: + return frozenset(_TYPE_NAMES.validate_python(value)) + except ValidationError: + return frozenset() + + +def _enum_conflicts_with_declared_type(schema: Mapping[str, object]) -> bool: + """Whether ``schema``'s ``enum`` cannot match its declared ``type``.""" + enum_values: Final = _as_items(schema.get("enum")) + declared_type: Final = schema.get("type") + if enum_values is None or declared_type is None: + return False + if isinstance(declared_type, str): + check: Final = _ENUM_TYPE_CHECKS.get(declared_type) + return check is not None and not all(check(value) for value in enum_values) + return _as_items(declared_type) is not None + + +def _note_value(value: object) -> object: + return value if value is None or isinstance(value, (str, int, float)) else json.dumps(value) + + +def _constraint_notes(schema: Mapping[str, object]) -> Iterator[str]: + for key, label in _CONSTRAINT_NOTES.items(): + if schema.get(key, False) is not False: + yield label.format(_note_value(schema[key])) + + +def _unsupported_format(schema: Mapping[str, object]) -> str | None: + value: Final = schema.get("format") + if isinstance(value, str) and value not in SUPPORTED_STRING_FORMATS: + return value + return None + + +def _description_field(existing: object, notes: tuple[str, ...]) -> Mapping[str, object]: + if not notes: + return {} + note: Final = "Note: " + ", ".join(notes) + "." + if isinstance(existing, str) and existing: + return {"description": existing + " " + note} + return {"description": note} + + +def _object_fields(declared_type: object, schema: Mapping[str, object]) -> Mapping[str, object]: + """Claude 400s on any object node not closed with ``additionalProperties: false`` or missing ``type``.""" + if "object" in _declared_types(declared_type): + return {"additionalProperties": False} + if ( + declared_type is None + and _as_schema(schema.get("properties")) is not None + and not _COMPOSITION_KEYS & schema.keys() + ): + return {"type": "object", "additionalProperties": False} + return {} + + +def _shape_if_schema(value: object) -> object: + nested: Final = _as_schema(value) + return value if nested is None else shape_claude_output_schema(nested) + + +def _shape_each(value: object) -> object: + items: Final = _as_items(value) + return value if items is None else [_shape_if_schema(item) for item in items] + + +def _shape_nested(key: str, value: object) -> object: + if key in _SCHEMA_MAP_KEYS: + members: Final = _as_schema(value) + return value if members is None else {name: _shape_if_schema(member) for name, member in members.items()} + if key == "items": + return _shape_if_schema(value) + if key == "allOf": + return _shape_each(value) + return value + + +def _any_of_field(schema: Mapping[str, object]) -> Mapping[str, object]: + variants: Final = [*(_as_items(schema.get("anyOf")) or ()), *(_as_items(schema.get("oneOf")) or ())] + return {"anyOf": [_shape_if_schema(variant) for variant in variants]} if variants else {} + + +def shape_claude_output_schema(schema: Mapping[str, object]) -> dict[str, object]: + """Return a new schema that Claude's structured-output API accepts. Idempotent.""" + drops_type: Final = _enum_conflicts_with_declared_type(schema) + declared_type: Final = None if drops_type else schema.get("type") + object_fields: Final = _object_fields(declared_type, schema) + unsupported_format: Final = _unsupported_format(schema) + notes: Final = ( + *_constraint_notes(schema), + *(() if unsupported_format is None else (f"format: {unsupported_format}",)), + ) + dropped: Final = frozenset( + (*_CONSTRAINT_NOTES, "anyOf", "oneOf") + + (("type",) if drops_type else ()) + + (("additionalProperties",) if object_fields else ()) + + (("description",) if notes else ()) + + (("format",) if unsupported_format is not None else ()) + ) + kept: Final = {key: _shape_nested(key, value) for key, value in schema.items() if key not in dropped} + return {**_description_field(schema.get("description"), notes), **kept, **_any_of_field(schema), **object_fields} + + +def shape_claude_output_format(output_format: Mapping[str, object]) -> dict[str, object]: + """Shape the ``schema`` inside an Anthropic ``output_format`` block, leaving the rest untouched.""" + schema: Final = _as_schema(output_format.get("schema")) + if schema is None: + return dict(output_format) + return {**output_format, "schema": shape_claude_output_schema(schema)} diff --git a/tests/unit/llms/anthropic/test_anthropic_output_schema.py b/tests/unit/llms/anthropic/test_anthropic_output_schema.py new file mode 100644 index 00000000000..fcd6579689e --- /dev/null +++ b/tests/unit/llms/anthropic/test_anthropic_output_schema.py @@ -0,0 +1,69 @@ +"""Tests for the shared Claude structured-output schema shaper.""" + +import copy + +import pytest + +from litellm.llms.anthropic.output_schema import ( + SUPPORTED_STRING_FORMATS, + shape_claude_output_format, + shape_claude_output_schema, +) + +_MESSY_SCHEMA = { + "type": "object", + "description": "A record", + "properties": { + "tags": {"type": "array", "items": {"type": "string"}, "maxItems": 3, "uniqueItems": True}, + "score": {"type": "number", "minimum": 0, "exclusiveMaximum": 1}, + "id": {"type": "string", "format": "int64", "minLength": 1}, + "meta": {"type": ["object", "null"], "properties": {"a": {"type": "string"}}}, + "kind": {"oneOf": [{"type": "string"}, {"type": "integer"}]}, + "labels": {"type": "object", "additionalProperties": {"type": "string"}}, + "nested": {"properties": {"b": {"type": "boolean"}}}, + }, + "required": ["tags", "score", "id"], + "additionalProperties": True, +} + + +def test_shaping_is_idempotent(): + once = shape_claude_output_schema(_MESSY_SCHEMA) + + assert shape_claude_output_schema(once) == once + + +def test_shaping_does_not_mutate_the_input(): + before = copy.deepcopy(_MESSY_SCHEMA) + + shape_claude_output_schema(_MESSY_SCHEMA) + + assert _MESSY_SCHEMA == before + + +@pytest.mark.parametrize("fmt", sorted(SUPPORTED_STRING_FORMATS)) +def test_supported_string_format_is_kept(fmt): + assert shape_claude_output_schema({"type": "string", "format": fmt}) == {"type": "string", "format": fmt} + + +def test_output_format_block_shapes_only_its_schema(): + block = { + "type": "json_schema", + "schema": {"type": "object", "properties": {"n": {"type": "integer", "minimum": 1}}}, + } + + result = shape_claude_output_format(block) + + assert result == { + "type": "json_schema", + "schema": { + "type": "object", + "properties": {"n": {"type": "integer", "description": "Note: minimum value: 1."}}, + "additionalProperties": False, + }, + } + assert block["schema"]["properties"]["n"] == {"type": "integer", "minimum": 1} + + +def test_output_format_block_without_schema_is_returned_unchanged(): + assert shape_claude_output_format({"type": "json_schema"}) == {"type": "json_schema"} diff --git a/tests/unit/llms/anthropic/test_anthropic_schema_filter.py b/tests/unit/llms/anthropic/test_anthropic_schema_filter.py index 32423d6679f..33be256366f 100644 --- a/tests/unit/llms/anthropic/test_anthropic_schema_filter.py +++ b/tests/unit/llms/anthropic/test_anthropic_schema_filter.py @@ -508,3 +508,46 @@ class TestFilterAnthropicOutputSchema: result = AnthropicConfig.filter_anthropic_output_schema(schema) assert "type" not in result["properties"]["rows"]["items"]["properties"]["c"] + + def test_closes_nullable_object(self): + """``type: ["object", "null"]`` is still an object and must be closed too, + otherwise the API answers "'additionalProperties' must be explicitly set + to false".""" + schema = { + "type": "object", + "properties": {"meta": {"type": ["object", "null"], "properties": {"a": {"type": "string"}}}}, + } + + result = AnthropicConfig.filter_anthropic_output_schema(schema) + + assert result["properties"]["meta"]["additionalProperties"] is False + assert result["properties"]["meta"]["type"] == ["object", "null"] + + def test_closes_untyped_node_that_declares_properties(self): + """A node with ``properties`` but no ``type`` is an object by structure. + The API rejects it with "Schema type is missing", so it is typed and closed.""" + schema = { + "type": "object", + "properties": {"meta": {"properties": {"a": {"type": "string"}}, "required": ["a"]}}, + } + + result = AnthropicConfig.filter_anthropic_output_schema(schema) + + assert result["properties"]["meta"] == { + "properties": {"a": {"type": "string"}}, + "required": ["a"], + "type": "object", + "additionalProperties": False, + } + + def test_moves_unsupported_string_format_into_description(self): + """A ``format`` outside the supported set 400s ("format 'int64' is not + supported"), so it is dropped and noted like any other constraint.""" + schema = { + "type": "object", + "properties": {"id": {"type": "string", "format": "int64", "description": "row id"}}, + } + + result = AnthropicConfig.filter_anthropic_output_schema(schema) + + assert result["properties"]["id"] == {"type": "string", "description": "row id Note: format: int64."} From 07fb7e2bd5e26c3f180760bdd8719d71f66705af Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Tue, 13 Oct 2026 21:20:10 -0700 Subject: [PATCH 2/9] fix(bedrock): shape Claude structured-output schemas on Converse and Invoke Bedrock relays Claude structured-output schemas to the same grammar compiler as the Anthropic API, so keywords like maxItems, minimum or an unsupported format came back as a 400. Converse (for Claude models) and Invoke now run the schema through the shared Anthropic shaper. Non-Claude Converse models accept those keywords and keep the previous behavior --- .../bedrock/chat/converse_transformation.py | 21 ++++- litellm/llms/bedrock/common_utils.py | 11 ++- ...ations_anthropic_claude3_transformation.py | 4 +- .../chat/test_converse_transformation.py | 60 +++++++++++++- .../test_anthropic_claude3_transformation.py | 9 ++- .../llms/bedrock/test_bedrock_common_utils.py | 79 ++++++++++++++++++- 6 files changed, 169 insertions(+), 15 deletions(-) diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py index 1cdcf0d6e4c..a97e7a90749 100644 --- a/litellm/llms/bedrock/chat/converse_transformation.py +++ b/litellm/llms/bedrock/chat/converse_transformation.py @@ -51,6 +51,7 @@ from litellm.llms.anthropic.chat.transformation import ( AnthropicConfig, ) from litellm.llms.anthropic.common_utils import AnthropicModelInfo +from litellm.llms.anthropic.output_schema import shape_claude_output_schema from litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException from litellm.llms.bedrock.chat.tool_result_images import place_tool_result_images from litellm.llms.bedrock.common_utils import bedrock_model_supports_regex_lookaround @@ -972,8 +973,15 @@ class AmazonConverseConfig(BaseConfig): return result + @staticmethod + def _native_output_schema(model: str, json_schema: dict[str, object]) -> Mapping[str, object]: + if BedrockModelInfo.get_base_model(model).startswith("anthropic"): + return shape_claude_output_schema(json_schema) + return AmazonConverseConfig._add_additional_properties_to_schema(json_schema) + @staticmethod def _create_output_config_for_response_format( + model: str, json_schema: dict | None = None, name: str | None = None, description: str | None = None, @@ -981,6 +989,11 @@ class AmazonConverseConfig(BaseConfig): """ Build an outputConfig block for Bedrock's native structured outputs API. + Claude schemas are shaped like the direct Anthropic path because Bedrock + relays them to the same grammar compiler, which rejects keywords it + cannot enforce. Other models accept those keywords, so they only get + ``additionalProperties: false`` filled in where it is missing. + The Converse API expects: { "outputConfig": { @@ -997,9 +1010,9 @@ class AmazonConverseConfig(BaseConfig): } } """ - if json_schema is not None: - json_schema = AmazonConverseConfig._add_additional_properties_to_schema(json_schema) - schema_str: Final = json.dumps(json_schema) if json_schema is not None else "{}" + schema_str: Final = ( + "{}" if json_schema is None else json.dumps(AmazonConverseConfig._native_output_schema(model, json_schema)) + ) json_schema_def: Final[JsonSchemaDefinition] = {"schema": schema_str} if name is not None: json_schema_def["name"] = name @@ -1271,6 +1284,7 @@ class AmazonConverseConfig(BaseConfig): # Requires an explicit schema — json_object with no schema falls through # to the tool-call path below. output_config: Final = self._create_output_config_for_response_format( + model=model, json_schema=json_schema, name=name, description=description, @@ -1650,6 +1664,7 @@ class AmazonConverseConfig(BaseConfig): and self._supports_native_structured_outputs(model, self.custom_llm_provider) ): output_config = self._create_output_config_for_response_format( + model=model, json_schema=output_config_format.get("schema"), name=output_config_format.get("name"), description=output_config_format.get("description"), diff --git a/litellm/llms/bedrock/common_utils.py b/litellm/llms/bedrock/common_utils.py index 9722f9dccb2..942da3f56d2 100644 --- a/litellm/llms/bedrock/common_utils.py +++ b/litellm/llms/bedrock/common_utils.py @@ -29,6 +29,7 @@ import litellm from litellm import verbose_logger from litellm.litellm_core_utils.aws_partition import get_aws_dns_suffix from litellm.litellm_core_utils.optional_imports import ensure_optional_import +from litellm.llms.anthropic.output_schema import shape_claude_output_format from litellm.llms.base_llm.anthropic_messages.transformation import ( BaseAnthropicMessagesConfig, ) @@ -266,6 +267,7 @@ def apply_bedrock_invoke_structured_output( ``output_config.format``, keeping the pre-existing precedence of the legacy field when a request carries both. Models flagged ``supports_native_structured_output`` in the model map get the schema + shaped by ``shape_claude_output_format`` like the direct Anthropic path and forwarded as ``output_config.format``, which Bedrock relays to the model for enforced structured output. For every other model the schema is inlined into the last user message as best-effort text, with a warning because nothing @@ -273,16 +275,19 @@ def apply_bedrock_invoke_structured_output( """ legacy_output_format: Final = request_body.pop("output_format", None) output_config_format: Final = pop_bedrock_invoke_output_config_format(request_body) - schema_format: Final = legacy_output_format if isinstance(legacy_output_format, dict) else output_config_format + schema_format: Final[dict[str, object] | None] = ( + legacy_output_format if isinstance(legacy_output_format, dict) else output_config_format + ) if schema_format is None: return if _bedrock_model_supports(model, "supports_native_structured_output"): + native_format: Final = shape_claude_output_format(schema_format) existing_output_config: Final = request_body.get("output_config") if isinstance(existing_output_config, dict): - existing_output_config["format"] = schema_format + existing_output_config["format"] = native_format else: - request_body["output_config"] = {"format": schema_format} # rebind-ok: out-param + request_body["output_config"] = {"format": native_format} # rebind-ok: out-param return verbose_logger.warning( diff --git a/tests/unit/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude3_transformation.py b/tests/unit/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude3_transformation.py index 859b57c7cf3..5dd6609f415 100644 --- a/tests/unit/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude3_transformation.py +++ b/tests/unit/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude3_transformation.py @@ -529,7 +529,7 @@ def test_output_config_format_forwarded_for_bedrock_chat_invoke_request(local_mo for models with native structured-output support (Claude Sonnet 4.6).""" schema_format = { "type": "json_schema", - "schema": {"type": "object", "properties": {"answer": {"type": "string"}}}, + "schema": {"type": "object", "properties": {"answer": {"type": "string"}}, "additionalProperties": False}, } result = AmazonAnthropicClaudeConfig().transform_request( @@ -670,7 +670,7 @@ def test_bedrock_chat_invoke_drop_params_keeps_native_output_config_format(local monkeypatch.setattr(litellm, "drop_params", True) schema_format = { "type": "json_schema", - "schema": {"type": "object", "properties": {"zebra_count": {"type": "integer"}}}, + "schema": {"type": "object", "properties": {"zebra_count": {"type": "integer"}}, "additionalProperties": False}, } result = AmazonAnthropicClaudeConfig().transform_request( diff --git a/tests/unit/llms/bedrock/chat/test_converse_transformation.py b/tests/unit/llms/bedrock/chat/test_converse_transformation.py index 469d06f7205..beb48ac022e 100644 --- a/tests/unit/llms/bedrock/chat/test_converse_transformation.py +++ b/tests/unit/llms/bedrock/chat/test_converse_transformation.py @@ -4696,6 +4696,7 @@ def test_create_output_config_for_response_format(): } output_config = config._create_output_config_for_response_format( + model="qwen.qwen3-32b-v1:0", json_schema=schema, name="PersonInfo", description="A person's info", @@ -5280,7 +5281,7 @@ def test_output_config_applies_additional_properties(): }, } output_config = AmazonConverseConfig._create_output_config_for_response_format( - json_schema=schema, name="test_schema" + model="qwen.qwen3-32b-v1:0", json_schema=schema, name="test_schema" ) parsed = json.loads( output_config["textFormat"]["structure"]["jsonSchema"]["schema"] @@ -5289,6 +5290,63 @@ def test_output_config_applies_additional_properties(): assert parsed["properties"]["nested"]["additionalProperties"] is False +_SCHEMA_CLAUDE_REJECTS = { + "type": "object", + "properties": { + "score": {"type": "number", "minimum": 0, "maximum": 1}, + "tags": {"type": "array", "items": {"type": "string"}, "maxItems": 3}, + "id": {"type": "string", "format": "int64"}, + "meta": {"type": ["object", "null"], "properties": {"a": {"type": "string"}}}, + "labels": {"type": "object", "additionalProperties": {"type": "string"}}, + }, + "required": ["score"], +} + + +def _converse_schema_for(model: str) -> dict: + response_format = {"type": "json_schema", "json_schema": {"name": "Score", "schema": _SCHEMA_CLAUDE_REJECTS}} + result = AmazonConverseConfig()._translate_response_format_param( + value=response_format, + model=model, + optional_params={}, + non_default_params={"response_format": response_format}, + is_thinking_enabled=False, + ) + return json.loads(result["outputConfig"]["textFormat"]["structure"]["jsonSchema"]["schema"]) + + +def test_claude_response_format_schema_shaped_like_anthropic_path(local_model_cost_map): + """Bedrock relays a Claude schema to Claude's grammar compiler, which 400s on + keywords it cannot enforce ("For 'array' type, property 'maxItems' is not + supported") and on any object that is not closed, so Converse shapes it like + the direct Anthropic path.""" + assert _converse_schema_for("us.anthropic.claude-mythos-5-1") == { + "type": "object", + "properties": { + "score": {"type": "number", "description": "Note: minimum value: 0, maximum value: 1."}, + "tags": {"type": "array", "items": {"type": "string"}, "description": "Note: maximum number of items: 3."}, + "id": {"type": "string", "description": "Note: format: int64."}, + "meta": { + "type": ["object", "null"], + "properties": {"a": {"type": "string"}}, + "additionalProperties": False, + }, + "labels": {"type": "object", "additionalProperties": False}, + }, + "required": ["score"], + "additionalProperties": False, + } + + +def test_non_claude_response_format_schema_keeps_constraints(local_model_cost_map): + """Non-Claude Bedrock models enforce these keywords and schema-valued maps, + so only the missing ``additionalProperties`` is filled in.""" + sent = _converse_schema_for("qwen.qwen3-32b-v1:0") + + assert sent["properties"] == _SCHEMA_CLAUDE_REJECTS["properties"] + assert sent["additionalProperties"] is False + + _TOOL_PARAM = [ { "type": "function", diff --git a/tests/unit/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py b/tests/unit/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py index bb304fa62cc..457c1145e45 100644 --- a/tests/unit/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py +++ b/tests/unit/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py @@ -1050,6 +1050,7 @@ def test_bedrock_messages_forwards_output_config_with_output_format(): "schema": { "type": "object", "properties": {"answer": {"type": "string"}}, + "additionalProperties": False, }, } optional_params = { @@ -3195,11 +3196,11 @@ def test_bedrock_messages_legacy_output_format_wins_over_output_config_format(lo cfg = AmazonAnthropicClaudeMessagesConfig() legacy_format = { "type": "json_schema", - "schema": {"type": "object", "properties": {"legacy_field": {"type": "string"}}}, + "schema": {"type": "object", "properties": {"legacy_field": {"type": "string"}}, "additionalProperties": False}, } newer_format = { "type": "json_schema", - "schema": {"type": "object", "properties": {"newer_field": {"type": "string"}}}, + "schema": {"type": "object", "properties": {"newer_field": {"type": "string"}}, "additionalProperties": False}, } result = cfg.transform_anthropic_messages_request( @@ -3229,7 +3230,7 @@ def test_bedrock_messages_drop_params_keeps_native_output_config_format(local_mo cfg = AmazonAnthropicClaudeMessagesConfig() schema_format = { "type": "json_schema", - "schema": {"type": "object", "properties": {"zebra_count": {"type": "integer"}}}, + "schema": {"type": "object", "properties": {"zebra_count": {"type": "integer"}}, "additionalProperties": False}, } result = cfg.transform_anthropic_messages_request( @@ -3254,7 +3255,7 @@ def test_bedrock_messages_strips_effort_but_keeps_format_for_sonnet_4_5(local_mo cfg = AmazonAnthropicClaudeMessagesConfig() schema_format = { "type": "json_schema", - "schema": {"type": "object", "properties": {"zebra_count": {"type": "integer"}}}, + "schema": {"type": "object", "properties": {"zebra_count": {"type": "integer"}}, "additionalProperties": False}, } result = cfg.transform_anthropic_messages_request( diff --git a/tests/unit/llms/bedrock/test_bedrock_common_utils.py b/tests/unit/llms/bedrock/test_bedrock_common_utils.py index d10ce9d1fae..304397c883d 100644 --- a/tests/unit/llms/bedrock/test_bedrock_common_utils.py +++ b/tests/unit/llms/bedrock/test_bedrock_common_utils.py @@ -659,8 +659,14 @@ def test_apply_structured_output_prefers_legacy_output_format(local_model_cost_m apply_bedrock_invoke_structured_output, ) - legacy = {"type": "json_schema", "schema": {"type": "object", "properties": {"a": {"type": "string"}}}} - newer = {"type": "json_schema", "schema": {"type": "object", "properties": {"b": {"type": "string"}}}} + legacy = { + "type": "json_schema", + "schema": {"type": "object", "properties": {"a": {"type": "string"}}, "additionalProperties": False}, + } + newer = { + "type": "json_schema", + "schema": {"type": "object", "properties": {"b": {"type": "string"}}, "additionalProperties": False}, + } body = { "messages": [{"role": "user", "content": "hi"}], "output_format": legacy, @@ -676,6 +682,75 @@ def test_apply_structured_output_prefers_legacy_output_format(local_model_cost_m assert "output_format" not in body +_SCHEMA_WITH_REJECTED_KEYWORDS = { + "type": "object", + "properties": { + "tags": {"type": "array", "items": {"type": "string"}, "maxItems": 3}, + "meta": {"type": ["object", "null"], "properties": {"a": {"type": "string"}}}, + }, + "required": ["tags"], +} + + +def test_apply_structured_output_shapes_schema_for_native_models(local_model_cost_map): + """Bedrock relays the schema to Claude's grammar compiler, which 400s on + keywords it cannot enforce and on open objects, so the Invoke path shapes + the schema like the direct Anthropic path before forwarding it.""" + from litellm.llms.bedrock.common_utils import apply_bedrock_invoke_structured_output + + body = { + "messages": [{"role": "user", "content": "hi"}], + "output_format": {"type": "json_schema", "schema": _SCHEMA_WITH_REJECTED_KEYWORDS}, + } + + apply_bedrock_invoke_structured_output( + model="us.anthropic.claude-sonnet-4-5-20250929-v1:0", + request_body=body, + ) + + assert body["output_config"]["format"] == { + "type": "json_schema", + "schema": { + "type": "object", + "properties": { + "tags": { + "type": "array", + "items": {"type": "string"}, + "description": "Note: maximum number of items: 3.", + }, + "meta": { + "type": ["object", "null"], + "properties": {"a": {"type": "string"}}, + "additionalProperties": False, + }, + }, + "required": ["tags"], + "additionalProperties": False, + }, + } + + +def test_apply_structured_output_inlines_raw_schema_for_other_models(local_model_cost_map): + """The prompt-engineered fallback is text the model reads, not a grammar, so + the schema is inlined untouched.""" + import json + + from litellm.llms.bedrock.common_utils import apply_bedrock_invoke_structured_output + + body = { + "messages": [{"role": "user", "content": "hi"}], + "output_format": {"type": "json_schema", "schema": _SCHEMA_WITH_REJECTED_KEYWORDS}, + } + + apply_bedrock_invoke_structured_output( + model="anthropic.claude-3-haiku-20240307-v1:0", + request_body=body, + ) + + assert "output_config" not in body + assert json.loads(body["messages"][-1]["content"][-1]["text"]) == _SCHEMA_WITH_REJECTED_KEYWORDS + + def test_sign_aws_request_assumes_role_with_external_id(monkeypatch): """A trust policy requiring sts:ExternalId must be satisfied when signing batch API requests.""" import datetime From bdbe322eb822f911fd8c99b3128ee7434c0a3aa7 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Tue, 13 Oct 2026 21:23:30 -0700 Subject: [PATCH 3/9] test(e2e): cover Bedrock Converse output_format with constraint keywords --- .../e2e/llm_translation/structured_output.py | 28 ++++++++++++++- .../test_messages_bedrock_e2e.py | 34 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/tests/e2e/llm_translation/structured_output.py b/tests/e2e/llm_translation/structured_output.py index b20fe769eaa..7860f8e4613 100644 --- a/tests/e2e/llm_translation/structured_output.py +++ b/tests/e2e/llm_translation/structured_output.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import Final -from pydantic import TypeAdapter +from pydantic import BaseModel, ConfigDict, TypeAdapter SENTIMENT_PROMPT: Final = "Classify the sentiment of this review: 'The battery died after two days.'" SENTIMENT_LABELS: Final = frozenset({"positive", "negative", "neutral"}) @@ -15,10 +15,36 @@ SENTIMENT_OUTPUT_FORMAT: Final[dict[str, object]] = { "additionalProperties": False, }, } +CONSTRAINED_SENTIMENT_OUTPUT_FORMAT: Final[dict[str, object]] = { + "type": "json_schema", + "schema": { + "type": "object", + "properties": { + "sentiment": {"type": "string", "enum": sorted(SENTIMENT_LABELS)}, + "confidence": {"type": "number", "minimum": 0, "maximum": 1}, + "reasons": {"type": "array", "items": {"type": "string"}, "maxItems": 3}, + }, + "required": ["sentiment", "confidence", "reasons"], + "additionalProperties": False, + }, +} _SENTIMENT_JSON: Final = TypeAdapter(dict[str, str]) +class _ConstrainedSentiment(BaseModel): + model_config = ConfigDict(frozen=True, extra="forbid") + + sentiment: str + confidence: float + reasons: tuple[str, ...] + + def assert_sentiment_json(text: str) -> None: parsed = _SENTIMENT_JSON.validate_json(text) assert set(parsed) == {"sentiment"}, f"output_format schema not enforced, extra or missing keys: {parsed}" assert parsed["sentiment"] in SENTIMENT_LABELS, f"sentiment outside the schema enum: {parsed}" + + +def assert_constrained_sentiment_json(text: str) -> None: + parsed = _ConstrainedSentiment.model_validate_json(text) + assert parsed.sentiment in SENTIMENT_LABELS, f"sentiment outside the schema enum: {parsed}" diff --git a/tests/e2e/llm_translation/test_messages_bedrock_e2e.py b/tests/e2e/llm_translation/test_messages_bedrock_e2e.py index 31daa9bba32..d28d5c38fe3 100644 --- a/tests/e2e/llm_translation/test_messages_bedrock_e2e.py +++ b/tests/e2e/llm_translation/test_messages_bedrock_e2e.py @@ -10,7 +10,13 @@ from lifecycle import ResourceManager from models import LiteLLMParamsBody from proxy_client import ProxyClient from sdk_clients import NO_PROXY_CACHE, SdkClients -from structured_output import SENTIMENT_OUTPUT_FORMAT, SENTIMENT_PROMPT, assert_sentiment_json +from structured_output import ( + CONSTRAINED_SENTIMENT_OUTPUT_FORMAT, + SENTIMENT_OUTPUT_FORMAT, + SENTIMENT_PROMPT, + assert_constrained_sentiment_json, + assert_sentiment_json, +) pytestmark = pytest.mark.e2e @@ -136,6 +142,32 @@ class TestBedrockMessages: assert len(texts) == len(message.content), f"structured output came back as non-text blocks: {message!r}" assert_sentiment_json("".join(texts)) + @meta( + Subject( + domain=Domain.LLM_TRANSLATION, + route=Route.MESSAGES, + providers=(Provider.BEDROCK,), + models=(CONVERSE_CLAUDE_BACKEND,), + capabilities=(Capability.RESPONSE_SCHEMA,), + mode=Mode.NONSTREAM, + ) + ) + def test_converse_output_format_accepts_constraint_keywords( + self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients + ) -> None: + model = _register(proxy, resources, CONVERSE_CLAUDE_BACKEND) + client = sdk.anthropic(resources.key()) + + message = client.messages.create( + model=model, + max_tokens=256, + messages=[{"role": "user", "content": SENTIMENT_PROMPT}], + extra_body={**NO_PROXY_CACHE, "output_format": CONSTRAINED_SENTIMENT_OUTPUT_FORMAT}, + ) + texts = tuple(block.text for block in message.content if isinstance(block, TextBlock)) + assert len(texts) == len(message.content), f"structured output came back as non-text blocks: {message!r}" + assert_constrained_sentiment_json("".join(texts)) + @pytest.mark.covers("llm.messages.bedrock_converse.basic.stream.works") @meta( Subject( From b0d342d0ee0b43a2fba025587dc48aed9d50ea65 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Tue, 13 Oct 2026 22:00:22 -0700 Subject: [PATCH 4/9] fix(bedrock): forward a non-object Converse json_schema unchanged instead of crashing A boolean or string json_schema on a Claude Converse deployment hit an AttributeError inside the shaper and surfaced as a 500. It is now sent to Bedrock as is, the way non-Claude models and Invoke already handle it, so the caller gets Bedrock's own 400 --- .../llms/bedrock/chat/converse_transformation.py | 4 +++- .../bedrock/chat/test_converse_transformation.py | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py index a97e7a90749..cc82f7217b4 100644 --- a/litellm/llms/bedrock/chat/converse_transformation.py +++ b/litellm/llms/bedrock/chat/converse_transformation.py @@ -974,7 +974,9 @@ class AmazonConverseConfig(BaseConfig): return result @staticmethod - def _native_output_schema(model: str, json_schema: dict[str, object]) -> Mapping[str, object]: + def _native_output_schema(model: str, json_schema: object) -> object: + if not isinstance(json_schema, dict): + return json_schema if BedrockModelInfo.get_base_model(model).startswith("anthropic"): return shape_claude_output_schema(json_schema) return AmazonConverseConfig._add_additional_properties_to_schema(json_schema) diff --git a/tests/unit/llms/bedrock/chat/test_converse_transformation.py b/tests/unit/llms/bedrock/chat/test_converse_transformation.py index beb48ac022e..c0f39045d5c 100644 --- a/tests/unit/llms/bedrock/chat/test_converse_transformation.py +++ b/tests/unit/llms/bedrock/chat/test_converse_transformation.py @@ -5303,8 +5303,8 @@ _SCHEMA_CLAUDE_REJECTS = { } -def _converse_schema_for(model: str) -> dict: - response_format = {"type": "json_schema", "json_schema": {"name": "Score", "schema": _SCHEMA_CLAUDE_REJECTS}} +def _converse_schema_json(model: str, schema: object) -> str: + response_format = {"type": "json_schema", "json_schema": {"name": "Score", "schema": schema}} result = AmazonConverseConfig()._translate_response_format_param( value=response_format, model=model, @@ -5312,7 +5312,11 @@ def _converse_schema_for(model: str) -> dict: non_default_params={"response_format": response_format}, is_thinking_enabled=False, ) - return json.loads(result["outputConfig"]["textFormat"]["structure"]["jsonSchema"]["schema"]) + return result["outputConfig"]["textFormat"]["structure"]["jsonSchema"]["schema"] + + +def _converse_schema_for(model: str) -> dict: + return json.loads(_converse_schema_json(model, _SCHEMA_CLAUDE_REJECTS)) def test_claude_response_format_schema_shaped_like_anthropic_path(local_model_cost_map): @@ -5347,6 +5351,12 @@ def test_non_claude_response_format_schema_keeps_constraints(local_model_cost_ma assert sent["additionalProperties"] is False +@pytest.mark.parametrize("model", ["us.anthropic.claude-mythos-5-1", "qwen.qwen3-32b-v1:0"]) +@pytest.mark.parametrize("schema", [True, "not a schema"]) +def test_non_object_response_format_schema_is_forwarded_unchanged(local_model_cost_map, model: str, schema: object): + assert json.loads(_converse_schema_json(model, schema)) == schema + + _TOOL_PARAM = [ { "type": "function", From 4cd5e9f4f0e64b687df82ab1ffbab28e7cc04d24 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Tue, 13 Oct 2026 22:00:22 -0700 Subject: [PATCH 5/9] test(anthropic): cover definitions, allOf and composition handling in the Claude schema shaper Pins recursion into $defs, definitions and allOf, and the rule that an untyped node with properties plus anyOf, oneOf, allOf or $ref is not forced to an object. The supported-format test now uses literal formats instead of the module's own set, which becomes private --- litellm/llms/anthropic/output_schema.py | 4 +-- .../anthropic/test_anthropic_output_schema.py | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/litellm/llms/anthropic/output_schema.py b/litellm/llms/anthropic/output_schema.py index 0b2f66a8767..d691f76b616 100644 --- a/litellm/llms/anthropic/output_schema.py +++ b/litellm/llms/anthropic/output_schema.py @@ -13,7 +13,7 @@ from typing import Final from pydantic import TypeAdapter, ValidationError # anthropic SDK ``lib/_parse/_transform.py`` SupportedStringFormats, checked 2026-10-13 -SUPPORTED_STRING_FORMATS: Final = frozenset( +_SUPPORTED_STRING_FORMATS: Final = frozenset( {"date-time", "time", "date", "duration", "email", "hostname", "uri", "ipv4", "ipv6", "uuid"} ) @@ -114,7 +114,7 @@ def _constraint_notes(schema: Mapping[str, object]) -> Iterator[str]: def _unsupported_format(schema: Mapping[str, object]) -> str | None: value: Final = schema.get("format") - if isinstance(value, str) and value not in SUPPORTED_STRING_FORMATS: + if isinstance(value, str) and value not in _SUPPORTED_STRING_FORMATS: return value return None diff --git a/tests/unit/llms/anthropic/test_anthropic_output_schema.py b/tests/unit/llms/anthropic/test_anthropic_output_schema.py index fcd6579689e..71e1aa2712c 100644 --- a/tests/unit/llms/anthropic/test_anthropic_output_schema.py +++ b/tests/unit/llms/anthropic/test_anthropic_output_schema.py @@ -5,7 +5,6 @@ import copy import pytest from litellm.llms.anthropic.output_schema import ( - SUPPORTED_STRING_FORMATS, shape_claude_output_format, shape_claude_output_schema, ) @@ -41,11 +40,38 @@ def test_shaping_does_not_mutate_the_input(): assert _MESSY_SCHEMA == before -@pytest.mark.parametrize("fmt", sorted(SUPPORTED_STRING_FORMATS)) -def test_supported_string_format_is_kept(fmt): +# Claude-supported formats per the anthropic SDK ``lib/_parse/_transform.py`` SupportedStringFormats, checked 2026-10-13 +@pytest.mark.parametrize("fmt", ["date-time", "date", "email", "uri", "uuid"]) +def test_supported_string_format_is_kept(fmt: str): assert shape_claude_output_schema({"type": "string", "format": fmt}) == {"type": "string", "format": fmt} +_BOUNDED = {"type": "number", "minimum": 0} +_BOUNDED_SHAPED = {"type": "number", "description": "Note: minimum value: 0."} + + +@pytest.mark.parametrize( + ("schema", "expected"), + [ + ({"$defs": {"X": _BOUNDED}}, {"$defs": {"X": _BOUNDED_SHAPED}}), + ({"definitions": {"X": _BOUNDED}}, {"definitions": {"X": _BOUNDED_SHAPED}}), + ({"allOf": [_BOUNDED]}, {"allOf": [_BOUNDED_SHAPED]}), + ], + ids=["$defs", "definitions", "allOf"], +) +def test_shapes_schemas_nested_under_definitions_and_all_of(schema: dict[str, object], expected: dict[str, object]): + assert shape_claude_output_schema(schema) == expected + + +@pytest.mark.parametrize("composition", ["anyOf", "oneOf", "allOf", "$ref"]) +def test_untyped_node_with_properties_and_composition_is_not_forced_to_object(composition: str): + value = "#/$defs/X" if composition == "$ref" else [{"type": "object", "properties": {}}] + result = shape_claude_output_schema({"properties": {"a": {"type": "string"}}, composition: value}) + + assert "type" not in result + assert "additionalProperties" not in result + + def test_output_format_block_shapes_only_its_schema(): block = { "type": "json_schema", From ac2e1bbdd3ed618cd7c498574a271dbd7f97f33d Mon Sep 17 00:00:00 2001 From: mateo Date: Tue, 27 Oct 2026 20:18:22 +0000 Subject: [PATCH 6/9] fix(anthropic): keep a dropped enum-conflicting type off nullable object nodes and bound the constrained sentiment e2e check Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/llms/anthropic/output_schema.py | 2 +- tests/e2e/llm_translation/structured_output.py | 6 +++--- .../llms/anthropic/test_anthropic_output_schema.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/litellm/llms/anthropic/output_schema.py b/litellm/llms/anthropic/output_schema.py index 15bdceb376b..803e2e4a14d 100644 --- a/litellm/llms/anthropic/output_schema.py +++ b/litellm/llms/anthropic/output_schema.py @@ -191,7 +191,7 @@ def shape_claude_output_schema(schema: Mapping[str, object]) -> dict[str, object """Return a new schema that Claude's structured-output API accepts. Idempotent.""" drops_type: Final = _enum_conflicts_with_declared_type(schema) declared_type: Final = None if drops_type else schema.get("type") - object_fields: Final = _object_fields(declared_type, schema) + object_fields: Final = {} if drops_type else _object_fields(declared_type, schema) unsupported_format: Final = _unsupported_format(schema, declared_type) notes: Final = ( *_constraint_notes(schema), diff --git a/tests/e2e/llm_translation/structured_output.py b/tests/e2e/llm_translation/structured_output.py index 7860f8e4613..99cb222ada6 100644 --- a/tests/e2e/llm_translation/structured_output.py +++ b/tests/e2e/llm_translation/structured_output.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import Final -from pydantic import BaseModel, ConfigDict, TypeAdapter +from pydantic import BaseModel, ConfigDict, Field, TypeAdapter SENTIMENT_PROMPT: Final = "Classify the sentiment of this review: 'The battery died after two days.'" SENTIMENT_LABELS: Final = frozenset({"positive", "negative", "neutral"}) @@ -35,8 +35,8 @@ class _ConstrainedSentiment(BaseModel): model_config = ConfigDict(frozen=True, extra="forbid") sentiment: str - confidence: float - reasons: tuple[str, ...] + confidence: float = Field(ge=0, le=1) + reasons: tuple[str, ...] = Field(max_length=3) def assert_sentiment_json(text: str) -> None: diff --git a/tests/unit/llms/anthropic/test_anthropic_output_schema.py b/tests/unit/llms/anthropic/test_anthropic_output_schema.py index 71e1aa2712c..273f3c4cfd8 100644 --- a/tests/unit/llms/anthropic/test_anthropic_output_schema.py +++ b/tests/unit/llms/anthropic/test_anthropic_output_schema.py @@ -93,3 +93,13 @@ def test_output_format_block_shapes_only_its_schema(): def test_output_format_block_without_schema_is_returned_unchanged(): assert shape_claude_output_format({"type": "json_schema"}) == {"type": "json_schema"} + + +def test_nullable_object_enum_keeps_its_type_dropped_instead_of_forcing_object() -> None: + """``enum: [null]`` on an ``["object", "null"]`` node conflicts with the declared type, so the type + is dropped. Re-adding ``type: object`` would make the node impossible to satisfy.""" + shaped = shape_claude_output_schema( + {"type": ["object", "null"], "properties": {"a": {"type": "string"}}, "enum": [None]} + ) + + assert shaped == {"properties": {"a": {"type": "string"}}, "enum": [None]} From 060c000924e91b345a6d1af6c17e99765c062045 Mon Sep 17 00:00:00 2001 From: mateo Date: Tue, 27 Oct 2026 20:27:45 +0000 Subject: [PATCH 7/9] test(integration): prove an unlisted string format and an untyped node reach Bedrock shaped on Converse and Invoke Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- ...t_bedrock_native_structured_output_wire.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/integration/providers/test_bedrock_native_structured_output_wire.py b/tests/integration/providers/test_bedrock_native_structured_output_wire.py index 5803d1dee44..3f31b25b8ac 100644 --- a/tests/integration/providers/test_bedrock_native_structured_output_wire.py +++ b/tests/integration/providers/test_bedrock_native_structured_output_wire.py @@ -88,6 +88,15 @@ _LIMITS_SCHEMA: Final[dict[str, JsonValue]] = { "required": ["tier", "summary", "tags", "owner", "backup"], "$defs": {"Person": {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}}, } +_UNLISTED_FORMAT_SCHEMA: Final[dict[str, JsonValue]] = { + "type": "object", + "properties": { + "id": {"type": "string", "format": "int64"}, + "meta": {"properties": {"a": {"type": "string"}}, "required": ["a"]}, + }, + "required": ["id", "meta"], + "additionalProperties": False, +} _OPEN_COUNTS: Final[dict[str, JsonValue]] = {"type": "object", "additionalProperties": {"type": "integer"}} _DICT_SCHEMA: Final[dict[str, JsonValue]] = { "type": "object", @@ -754,6 +763,35 @@ def test_native_schema_reaches_bedrock_with_limits_in_descriptions_and_every_obj assert _JSON_TOOL not in _tools(body, route == "invoke"), body +@pytest.mark.parametrize("route", ["converse", "invoke"]) +def test_unlisted_string_format_reaches_bedrock_as_a_description_note_with_the_untyped_node_closed( + gateway: Gateway, route: str +) -> None: + """Bedrock Claude rejects a string ``format`` outside Claude's list and a node with no ``type``, so the + proxy notes the format in the description and types the node, instead of dropping to the json tool.""" + marker, marker_hex = _marker() + with wire_server(_bedrock) as wire, gateway.scenario() as scenario: + model: Final = _deployment(scenario, wire, route, _SONNET) + body: Final = _chat_body(model, marker, response_format=_response_format(_UNLISTED_FORMAT_SCHEMA)) + assert _content_json(_chat_message(gateway.request("POST", "/v1/chat/completions", body))) == _payload( + marker_hex + ) + _, sent = _one_call(wire, marker_hex) + schema: Final = _native_schema(sent, route == "invoke") + assert schema is not None, sent + assert _bedrock_schema_fault(schema) is None, schema + assert object_value(schema["properties"]) == { + "id": {"type": "string", "description": "Note: format: int64."}, + "meta": { + "type": "object", + "properties": {"a": {"type": "string"}}, + "required": ["a"], + "additionalProperties": False, + }, + }, schema + assert _JSON_TOOL not in _tools(sent, route == "invoke"), sent + + @pytest.mark.parametrize("route", ["converse", "invoke"]) def test_open_object_schema_takes_the_json_tool_on_a_native_model_and_keeps_the_dict_open( gateway: Gateway, route: str From 78db493c38475297538ed16fb63396745d111729 Mon Sep 17 00:00:00 2001 From: mateo Date: Tue, 27 Oct 2026 20:40:29 +0000 Subject: [PATCH 8/9] test(integration): prove direct Anthropic gets the int64 note and a typed untyped node on the wire Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- ...vertex_anthropic_structured_output_wire.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/integration/providers/test_vertex_anthropic_structured_output_wire.py b/tests/integration/providers/test_vertex_anthropic_structured_output_wire.py index 2918f364c56..cc0642b31cf 100644 --- a/tests/integration/providers/test_vertex_anthropic_structured_output_wire.py +++ b/tests/integration/providers/test_vertex_anthropic_structured_output_wire.py @@ -1591,6 +1591,53 @@ def test_vertex_chat_native_schema_keeps_min_items_one_and_closes_a_nullable_obj assert "tools" not in sent.body, sent.body +def test_anthropic_chat_native_schema_notes_an_unlisted_string_format_and_types_an_untyped_node( + gateway: Gateway, +) -> None: + """Anthropic 400s on a string ``format`` outside Claude's list ("format 'int64' is not supported") and on + a node with properties but no ``type`` ("Schema type is missing"), so the proxy rewrites both.""" + marker, marker_hex = _marker("native") + schema: Final[dict[str, JsonValue]] = { + "type": "object", + "properties": { + "id": {"type": "string", "format": "int64"}, + "meta": {"properties": {"a": {"type": "string"}}, "required": ["a"]}, + }, + "required": ["id", "meta"], + "additionalProperties": False, + } + expected: Final[dict[str, JsonValue]] = { + **schema, + "properties": { + "id": {"description": "Note: format: int64.", "type": "string"}, + "meta": { + "properties": {"a": {"type": "string"}}, + "required": ["a"], + "type": "object", + "additionalProperties": False, + }, + }, + } + response_format: Final[dict[str, JsonValue]] = { + "type": "json_schema", + "json_schema": {"name": "record", "schema": schema}, + } + with wire_server(_peer) as wire, gateway.scenario() as scenario: + model: Final = _native_deployment(gateway, scenario, wire.url, "anthropic", _NATIVE_MODEL) + response: Final = gateway.request( + "POST", "/v1/chat/completions", _chat_body(model, marker, response_format=response_format) + ) + assert _chat_content(response) == _payload(marker_hex), response.text + sent: Final = _one_delivery(wire, marker_hex) + assert _native_format(sent.body) == {"type": "json_schema", "schema": expected}, sent.body + assert "tools" not in sent.body, sent.body + + +def _native_format(body: Mapping[str, JsonValue]) -> JsonValue: + output_format: Final = body.get("output_format") + return output_format if output_format is not None else object_value(body["output_config"])["format"] + + @pytest.mark.parametrize("stream", [False, True], ids=["non_stream", "stream"]) def test_vertex_messages_folds_output_format_into_output_config(gateway: Gateway, stream: bool) -> None: marker, marker_hex = _marker("native") From ecdb56a1cc7e8b7975b64a95e3a13e2926dc3d79 Mon Sep 17 00:00:00 2001 From: mateo Date: Tue, 27 Oct 2026 20:44:02 +0000 Subject: [PATCH 9/9] fix(anthropic): keep an untyped enum node untyped so shaping stays idempotent Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/llms/anthropic/output_schema.py | 1 + tests/unit/llms/anthropic/test_anthropic_output_schema.py | 1 + 2 files changed, 2 insertions(+) diff --git a/litellm/llms/anthropic/output_schema.py b/litellm/llms/anthropic/output_schema.py index 803e2e4a14d..d977c691d1a 100644 --- a/litellm/llms/anthropic/output_schema.py +++ b/litellm/llms/anthropic/output_schema.py @@ -152,6 +152,7 @@ def _object_fields(declared_type: object, schema: Mapping[str, object]) -> Mappi return {"additionalProperties": False} if ( declared_type is None + and "enum" not in schema and _as_schema(schema.get("properties")) is not None and not _COMPOSITION_KEYS & schema.keys() ): diff --git a/tests/unit/llms/anthropic/test_anthropic_output_schema.py b/tests/unit/llms/anthropic/test_anthropic_output_schema.py index 273f3c4cfd8..4133e165e9e 100644 --- a/tests/unit/llms/anthropic/test_anthropic_output_schema.py +++ b/tests/unit/llms/anthropic/test_anthropic_output_schema.py @@ -103,3 +103,4 @@ def test_nullable_object_enum_keeps_its_type_dropped_instead_of_forcing_object() ) assert shaped == {"properties": {"a": {"type": "string"}}, "enum": [None]} + assert shape_claude_output_schema(shaped) == shaped