fix(anthropic_responses): return tool_choice string literals for auto/any/none

This commit is contained in:
Devin AI 2026-07-08 17:06:52 +00:00
parent c2d8a17692
commit cc9d6d6257
2 changed files with 19 additions and 11 deletions

View file

@ -198,14 +198,16 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
@staticmethod
def translate_tool_choice_to_responses_api(
tool_choice: AnthropicMessagesToolChoice,
) -> Dict[str, Any]:
) -> Union[str, Dict[str, Any]]:
"""Convert Anthropic tool_choice to Responses API tool_choice."""
tc_type = tool_choice.get("type")
if tc_type == "any":
return {"type": "required"}
return "required"
elif tc_type == "none":
return "none"
elif tc_type == "tool":
return {"type": "function", "name": tool_choice.get("name", "")}
return {"type": "auto"}
return "auto"
@staticmethod
def translate_context_management_to_responses_api(

View file

@ -587,14 +587,20 @@ class TestTranslateToolChoiceToResponsesAPI:
"""Anthropic tool_choice -> Responses API tool_choice."""
def test_auto_maps_to_auto(self):
assert _ADAPTER.translate_tool_choice_to_responses_api({"type": "auto"}) == {
"type": "auto"
}
assert (
_ADAPTER.translate_tool_choice_to_responses_api({"type": "auto"}) == "auto"
)
def test_any_maps_to_required(self):
assert _ADAPTER.translate_tool_choice_to_responses_api({"type": "any"}) == {
"type": "required"
}
assert (
_ADAPTER.translate_tool_choice_to_responses_api({"type": "any"})
== "required"
)
def test_none_maps_to_none(self):
assert (
_ADAPTER.translate_tool_choice_to_responses_api({"type": "none"}) == "none"
)
def test_specific_tool_maps_to_function(self):
result = _ADAPTER.translate_tool_choice_to_responses_api(
@ -603,8 +609,8 @@ class TestTranslateToolChoiceToResponsesAPI:
assert result == {"type": "function", "name": "get_weather"}
def test_unknown_type_defaults_to_auto(self):
result = _ADAPTER.translate_tool_choice_to_responses_api({"type": "none"})
assert result == {"type": "auto"}
result = _ADAPTER.translate_tool_choice_to_responses_api({"type": "auto"})
assert result == "auto"
# ---------------------------------------------------------------------------