From 1cbec472132b81380ebdc14740cc08407dae9c33 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Mon, 16 Feb 2026 13:18:00 +0530 Subject: [PATCH] Use supports_response_schema for checking support for response_format --- litellm/llms/anthropic/chat/transformation.py | 15 +---- .../test_anthropic_chat_transformation.py | 64 +++++++++++++++++++ 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index c2cfff80685..aec0d126235 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -67,6 +67,7 @@ from litellm.utils import ( has_tool_call_blocks, last_assistant_with_tool_calls_has_no_thinking_blocks, supports_reasoning, + supports_response_schema, token_counter, ) @@ -870,18 +871,8 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): if param == "top_p": optional_params["top_p"] = value if 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", - } + if supports_response_schema( + model=model, custom_llm_provider=self.custom_llm_provider ): _output_format = ( self.map_response_format_to_anthropic_output_format(value) 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.py index 57e0dd494e0..bc998310e51 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 @@ -2752,3 +2752,67 @@ def test_fast_mode_parameter_mapping(): assert "speed" in result assert result["speed"] == "fast" + + +def test_response_format_uses_supports_response_schema(): + """ + Test that response_format mapping uses supports_response_schema from model_cost + instead of hardcoded model name patterns. + + This test verifies that: + 1. Models with supports_response_schema=True use native output_format + 2. Models without it fall back to tool-based approach + """ + from unittest.mock import patch + + import litellm + + config = AnthropicConfig() + + # Test with a model that supports response_schema (claude-sonnet-4-5) + response_format = { + "type": "json_schema", + "json_schema": { + "name": "test_schema", + "schema": { + "type": "object", + "properties": {"result": {"type": "string"}}, + "required": ["result"] + } + } + } + + non_default_params = {"response_format": response_format} + optional_params = {} + + # Test with claude-sonnet-4-5 (supports response_schema) + result = config.map_openai_params( + non_default_params=non_default_params, + optional_params=optional_params, + model="claude-sonnet-4-5-20250929", + drop_params=False + ) + + # Should use native output_format + assert "output_format" in result + assert result["output_format"]["type"] == "json_schema" + assert result["json_mode"] is True + + # Test with a hypothetical model that doesn't support response_schema + # Mock supports_response_schema to return False for this test + with patch('litellm.llms.anthropic.chat.transformation.supports_response_schema') as mock_supports: + mock_supports.return_value = False + + optional_params_no_support = {} + result_no_support = config.map_openai_params( + non_default_params=non_default_params, + optional_params=optional_params_no_support, + model="claude-hypothetical-model", + drop_params=False + ) + + # Should use tool-based approach instead + assert "tools" in result_no_support + assert result_no_support["json_mode"] is True + # Should NOT have output_format for models that don't support it + assert "output_format" not in result_no_support