Use supports_response_schema for checking support for response_format

This commit is contained in:
Sameer Kankute 2026-02-16 13:18:00 +05:30
parent 44bb1dafdb
commit 1cbec47213
2 changed files with 67 additions and 12 deletions

View file

@ -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)

View file

@ -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