The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(anthropic): use native output capability (#33235)
Krrish Dholakia477ef3a7e2cc
diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.pyindex 1f5b76f3d0a..0ec1f3eae13 100644--- a/litellm/llms/anthropic/chat/transformation.py+++ b/litellm/llms/anthropic/chat/transformation.py@@ -1441,24 +1441,10 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):output_key=param,)elif param == "response_format" and isinstance(value, dict):- if any(- substring in model- for substring in {- "sonnet-4.5",- "sonnet-4-5",- "opus-4.1",- "opus-4-1",- "opus-4.5",- "opus-4-5",- "opus-4.6",- "opus-4-6",- "opus-4.7",- "opus-4-7",- "sonnet-4.6",- "sonnet-4-6",- "sonnet_4.6",- "sonnet_4_6",- }+ if AnthropicConfig._supports_model_capability(+ model,+ "supports_native_structured_output",+ self._resolved_provider,):_output_format = self.map_response_format_to_anthropic_output_format(value)if _output_format is not None:diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.pyindex 0bcf34a45d6..e006662ec4d 100644--- a/litellm/llms/anthropic/common_utils.py+++ b/litellm/llms/anthropic/common_utils.py@@ -340,11 +340,15 @@ class AnthropicModelInfo(BaseLLMModelInfo):def _get_model_capability(model: str, key: str) -> Optional[bool]:"""Read boolean capability ``key`` from the model map, or None whenno entry declares it."""+ from litellm.utils import _get_bundled_model_cost_map+try:- for cand in AnthropicModelInfo._model_map_lookup_candidates(model):- value = litellm.model_cost.get(cand, {}).get(key)- if isinstance(value, bool):- return value+ candidates = AnthropicModelInfo._model_map_lookup_candidates(model)+ for model_cost in (litellm.model_cost, _get_bundled_model_cost_map()):+ for cand in candidates:+ value = model_cost.get(cand, {}).get(key)+ if isinstance(value, bool):+ return valueexcept Exception:passreturn Nonediff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.jsonindex a6f59abc140..c99347e189c 100644--- a/litellm/model_prices_and_context_window_backup.json+++ b/litellm/model_prices_and_context_window_backup.json@@ -11331,6 +11331,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true},@@ -11362,6 +11363,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"supports_web_search": true@@ -11424,6 +11426,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_max_reasoning_effort": true,"supports_tool_choice": true,"supports_vision": true,@@ -11479,6 +11482,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true},@@ -11506,6 +11510,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true},@@ -11559,6 +11564,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"supports_output_config": true@@ -11586,6 +11592,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"supports_output_config": true@@ -11614,6 +11621,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"provider_specific_entry": {@@ -11648,6 +11656,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"provider_specific_entry": {@@ -11682,6 +11691,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_sampling_params": false,"supports_tool_choice": true,"supports_vision": true,@@ -11718,6 +11728,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_sampling_params": false,"supports_tool_choice": true,"supports_vision": true,@@ -11788,6 +11799,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_sampling_params": false,"supports_tool_choice": true,"supports_vision": true,diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.jsonindex 08a452be844..8f2de0d0398 100644--- a/model_prices_and_context_window.json+++ b/model_prices_and_context_window.json@@ -11331,6 +11331,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true},@@ -11362,6 +11363,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"supports_web_search": true@@ -11424,6 +11426,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_max_reasoning_effort": true,"supports_tool_choice": true,"supports_vision": true,@@ -11479,6 +11482,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true},@@ -11506,6 +11510,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true},@@ -11559,6 +11564,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"supports_output_config": true@@ -11586,6 +11592,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"supports_output_config": true@@ -11614,6 +11621,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"provider_specific_entry": {@@ -11648,6 +11656,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_tool_choice": true,"supports_vision": true,"provider_specific_entry": {@@ -11682,6 +11691,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_sampling_params": false,"supports_tool_choice": true,"supports_vision": true,@@ -11718,6 +11728,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_sampling_params": false,"supports_tool_choice": true,"supports_vision": true,@@ -11788,6 +11799,7 @@"supports_prompt_caching": true,"supports_reasoning": true,"supports_response_schema": true,+ "supports_native_structured_output": true,"supports_sampling_params": false,"supports_tool_choice": true,"supports_vision": true,diff --git a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.pyindex 43ef7fcd971..94a4a3fc945 100644--- a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py+++ b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py@@ -957,15 +957,15 @@ def test_anthropic_structured_output_beta_header():@pytest.mark.parametrize("model_name",[- "claude-opus-4-6-20250918",- "claude-opus-4.6-20250918",+ "claude-opus-4-8",+ "claude-opus-4-6-20260205","claude-opus-4-5-20251101","claude-opus-4.5-20251101",],)def test_opus_uses_native_structured_output(model_name):"""- Test that Opus 4.5 and 4.6 models use native Anthropic structured outputs+ Test that supported Opus models use native Anthropic structured outputs(output_format) rather than the tool-based workaround."""config = AnthropicConfig()@@ -1005,6 +1005,43 @@ def test_opus_uses_native_structured_output(model_name):assert optional_params.get("json_mode") is True+def test_native_structured_output_uses_bundled_capability_when_remote_map_lags(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ model = "claude-opus-4-8"+ monkeypatch.setattr(+ litellm,+ "model_cost",+ {model: {"supports_response_schema": True}},+ )+ litellm.get_model_info.cache_clear()++ try:+ optional_params = AnthropicConfig().map_openai_params(+ non_default_params={+ "response_format": {+ "type": "json_schema",+ "json_schema": {+ "name": "answer",+ "schema": {+ "type": "object",+ "properties": {"answer": {"type": "string"}},+ "required": ["answer"],+ },+ },+ }+ },+ optional_params={},+ model=model,+ drop_params=False,+ )+ finally:+ litellm.get_model_info.cache_clear()++ assert "output_format" in optional_params+ assert "tools" not in optional_params++def test_non_structured_output_model_uses_tool_workaround():"""Test that models NOT in the native structured output list still use thediff --git a/tests/test_litellm/test_claude_opus_4_8_config.py b/tests/test_litellm/test_claude_opus_4_8_config.pyindex 32f7d249e05..8eead8a9c84 100644--- a/tests/test_litellm/test_claude_opus_4_8_config.py+++ b/tests/test_litellm/test_claude_opus_4_8_config.py@@ -95,6 +95,8 @@ def test_opus_4_8_model_pricing_and_capabilities():assert info["supports_tool_choice"] is Trueassert info["supports_vision"] is True+ assert model_data["claude-opus-4-8"]["supports_native_structured_output"] is True+def test_opus_4_8_bedrock_regional_model_pricing():model_data = _load_root_cost_map()@@ -165,6 +167,7 @@ def test_opus_4_8_present_in_bundled_backup():"azure_ai/claude-opus-4-8",):assert model_name in backup, f"Missing from backup cost map: {model_name}"+ assert backup["claude-opus-4-8"]["supports_native_structured_output"] is Truedef test_opus_4_8_registered_for_bedrock_converse():