Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
def __init__(self):
pass

def _normalize_tool_choice_for_responses_api(self, tool_choice: Any) -> Any:
"""Chat tool_choice uses function.name; Responses API expects top-level name."""
if not isinstance(tool_choice, dict) or tool_choice.get("type") != "function":
return tool_choice
if isinstance(tool_choice.get("name"), str) and tool_choice.get("name"):
# Return only Responses shape so stray chat ``function`` key is not sent upstream.
return {"type": "function", "name": tool_choice["name"]}
fn = tool_choice.get("function")
if isinstance(fn, dict):
fn_name = fn.get("name")
if isinstance(fn_name, str) and fn_name:
return {"type": "function", "name": fn_name}
return tool_choice

def _handle_raw_dict_response_item(
self, item: Dict[str, Any], index: int
) -> Tuple[Optional[Any], int]:
Expand Down Expand Up @@ -309,6 +323,10 @@ def _map_optional_params_to_responses_api_request(
text_format = self._transform_response_format_to_text_format(value)
if text_format:
responses_api_request["text"] = text_format # type: ignore
elif key == "tool_choice":
responses_api_request["tool_choice"] = ( # type: ignore[assignment]
self._normalize_tool_choice_for_responses_api(value)
)
elif key in ResponsesAPIOptionalRequestParams.__annotations__.keys():
responses_api_request[key] = value # type: ignore
elif key == "previous_response_id":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,56 @@ def test_map_optional_params_preserves_reasoning_summary():
assert responses_api_request["reasoning"]["summary"] == "detailed"


def test_map_optional_params_tool_choice_chat_nested_to_responses_api():
"""Chat tool_choice must become Responses ToolChoiceFunction (top-level name)."""
from litellm.completion_extras.litellm_responses_transformation.transformation import (
LiteLLMResponsesTransformationHandler,
)
from litellm.types.llms.openai import ResponsesAPIOptionalRequestParams

handler = LiteLLMResponsesTransformationHandler()
responses_api_request = ResponsesAPIOptionalRequestParams()
handler._map_optional_params_to_responses_api_request(
{
"stream": False,
"tool_choice": {
"type": "function",
"function": {"name": "Echo"},
},
},
responses_api_request,
)
assert responses_api_request["tool_choice"] == {
"type": "function",
"name": "Echo",
}
Comment thread
Sameerlite marked this conversation as resolved.


@pytest.mark.parametrize(
("tool_choice", "expected"),
[
("auto", "auto"),
("none", "none"),
(
{"type": "function", "name": "Echo"},
{"type": "function", "name": "Echo"},
),
(
{"type": "function", "name": "foo", "function": {"name": "bar"}},
{"type": "function", "name": "foo"},
),
({"type": "required"}, {"type": "required"}),
],
)
def test_normalize_tool_choice_for_responses_api(tool_choice, expected):
from litellm.completion_extras.litellm_responses_transformation.transformation import (
LiteLLMResponsesTransformationHandler,
)

handler = LiteLLMResponsesTransformationHandler()
assert handler._normalize_tool_choice_for_responses_api(tool_choice) == expected


def test_convert_chat_completion_file_type_to_input_file():
"""
Test that Chat Completion content with type 'file' is correctly mapped
Expand Down
Loading