From b6df3d7fe52096290e7276a2f85385a977a1f4ed Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:35:30 -0700 Subject: [PATCH] fix(responses): number bridged chat tool calls 0, 1 and pin the remaining bridge shapes (#46109) * test(responses): pin bridged commentary plus final answer as one chat choice Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(responses): pin reasoning, two-message and two-tool-call bridge shapes Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(responses): number bridged chat tool calls 0, 1 instead of all 0 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../transformation.py | 2 +- .../test_responses_bridge_incomplete.py | 473 ++++++++++- ...responses_transformation_transformation.py | 70 ++ .../test_litellm_completion_responses.py | 752 +++++++----------- 4 files changed, 833 insertions(+), 464 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index b76dd68018f..4f99c570551 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -2402,7 +2402,7 @@ class LiteLLMCompletionResponsesConfig: ), "function": function_dict, "type": "function", - "index": 0, + "index": index, } if provider_specific_fields: diff --git a/tests/integration/providers/test_responses_bridge_incomplete.py b/tests/integration/providers/test_responses_bridge_incomplete.py index 6e005f928ef..e970c806330 100644 --- a/tests/integration/providers/test_responses_bridge_incomplete.py +++ b/tests/integration/providers/test_responses_bridge_incomplete.py @@ -2,6 +2,7 @@ import json import uuid from typing import Final +import httpx import pytest from integration._support.client import Gateway, list_value, object_value from integration._support.wire import Reply, Request, wire_server @@ -589,13 +590,14 @@ _WEATHER_TOOL: Final[dict[str, JsonValue]] = { } -def _message_item(item_id: str, text: str) -> dict[str, JsonValue]: +def _message_item(item_id: str, text: str, phase: str | None = None) -> dict[str, JsonValue]: return { "type": "message", "id": item_id, "status": "completed", "role": "assistant", "content": [{"type": "output_text", "text": text, "annotations": []}], + **({} if phase is None else {"phase": phase}), } @@ -675,6 +677,219 @@ def test_chat_over_responses_deployment_folds_two_messages_and_function_call_int assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/responses")] +_COMMENTARY_THEN_FINAL_ANSWER: Final = ( + _message_item("msg_commentary", "Let me answer that.", phase="commentary"), + _message_item("msg_final", '{"city": "Paris", "summary": "Sunny, 21C"}', phase="final_answer"), +) + + +def test_chat_over_responses_deployment_folds_commentary_and_final_answer_into_one_choice( + gateway: Gateway, +) -> None: + identity: Final = "responses-bridge-commentary-final-" + uuid.uuid4().hex + + def respond(request: Request) -> Reply: + if request.method == "GET" and request.target == "/v1/models": + return Reply(body=b'{"object":"list","data":[]}') + assert request.method == "POST" and request.target == "/responses", request.target + return Reply( + body=json.dumps( + { + "id": "resp_commentary_final", + "object": "response", + "created_at": 1789788253, + "status": "completed", + "model": "gpt-6-sol", + "output": list(_COMMENTARY_THEN_FINAL_ANSWER), + "usage": {"input_tokens": 10, "output_tokens": 5, "total_tokens": 15}, + } + ).encode() + ) + + with wire_server(respond) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model="openai/responses/gpt-6-sol", api_base=wire.url, api_key="synthetic-openai-key" + ) + response: Final = gateway.request( + "POST", + "/v1/chat/completions", + { + "model": model, + "messages": [{"role": "user", "content": f"Weather in Paris as JSON. {identity}"}], + "tools": [_WEATHER_TOOL], + "cache": {"no-cache": True}, + }, + ) + assert response.status_code == 200, response.text + body: Final = response.json() + assert body["choices"] == [ + { + "finish_reason": "stop", + "index": 0, + "message": { + "role": "assistant", + "content": 'Let me answer that.{"city": "Paris", "summary": "Sunny, 21C"}', + }, + } + ], response.text + assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/responses")] + + +_REASONING_ITEM: Final[dict[str, JsonValue]] = { + "type": "reasoning", + "id": "rs_fold", + "summary": [{"type": "summary_text", "text": "Checking the forecast."}], +} + +_TIME_TOOL: Final[dict[str, JsonValue]] = { + "type": "function", + "function": { + "name": "get_time", + "description": "Get the local time for a city.", + "parameters": { + "type": "object", + "properties": {"city": {"type": "string"}}, + "required": ["city"], + }, + }, +} + +_TIME_CALL: Final[dict[str, JsonValue]] = { + "type": "function_call", + "id": "fc_2", + "call_id": "call_2", + "name": "get_time", + "arguments": '{"city":"Paris","tz":"CET"}', + "status": "completed", +} + +_REASONING_THEN_TWO_MESSAGES_THEN_CALL: Final = ( + _REASONING_ITEM, + _message_item("msg_first", "First message."), + _message_item("msg_second", "Second message."), + _WEATHER_CALL, +) + +_MESSAGE_THEN_TWO_CALLS: Final = ( + _message_item("msg_only", "Checking both."), + _WEATHER_CALL, + _TIME_CALL, +) + +_TWO_CALLS_CHAT_SHAPE: Final = [ + { + "id": "fc_1", + "type": "function", + "function": {"name": "get_weather", "arguments": '{"city":"Paris"}'}, + "index": 0, + }, + { + "id": "fc_2", + "type": "function", + "function": {"name": "get_time", "arguments": '{"city":"Paris","tz":"CET"}'}, + "index": 1, + }, +] + + +def _responses_body(response_id: str, output: tuple[dict[str, JsonValue], ...]) -> bytes: + return json.dumps( + { + "id": response_id, + "object": "response", + "created_at": 1789788253, + "status": "completed", + "model": "gpt-6-sol", + "output": list(output), + "usage": {"input_tokens": 10, "output_tokens": 5, "total_tokens": 15}, + } + ).encode() + + +def test_chat_over_responses_deployment_folds_reasoning_two_messages_and_function_call_into_one_choice( + gateway: Gateway, +) -> None: + identity: Final = "responses-bridge-reasoning-two-messages-" + uuid.uuid4().hex + + def respond(request: Request) -> Reply: + if request.method == "GET" and request.target == "/v1/models": + return Reply(body=b'{"object":"list","data":[]}') + assert request.method == "POST" and request.target == "/responses", request.target + return Reply(body=_responses_body("resp_reasoning_two_messages", _REASONING_THEN_TWO_MESSAGES_THEN_CALL)) + + with wire_server(respond) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model="openai/responses/gpt-6-sol", api_base=wire.url, api_key="synthetic-openai-key" + ) + response: Final = gateway.request( + "POST", + "/v1/chat/completions", + { + "model": model, + "messages": [{"role": "user", "content": f"What is the weather in Paris? {identity}"}], + "tools": [_WEATHER_TOOL], + "cache": {"no-cache": True}, + }, + ) + assert response.status_code == 200, response.text + body: Final = response.json() + assert body["choices"] == [ + { + "finish_reason": "tool_calls", + "index": 0, + "message": { + "role": "assistant", + "content": "First message.Second message.", + "reasoning_content": "Checking the forecast.", + "reasoning_items": [_REASONING_ITEM], + "tool_calls": _TWO_CALLS_CHAT_SHAPE[:1], + }, + } + ], response.text + assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/responses")] + + +def test_chat_over_responses_deployment_folds_message_and_two_function_calls_into_one_choice( + gateway: Gateway, +) -> None: + identity: Final = "responses-bridge-two-calls-" + uuid.uuid4().hex + + def respond(request: Request) -> Reply: + if request.method == "GET" and request.target == "/v1/models": + return Reply(body=b'{"object":"list","data":[]}') + assert request.method == "POST" and request.target == "/responses", request.target + return Reply(body=_responses_body("resp_two_calls", _MESSAGE_THEN_TWO_CALLS)) + + with wire_server(respond) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model="openai/responses/gpt-6-sol", api_base=wire.url, api_key="synthetic-openai-key" + ) + response: Final = gateway.request( + "POST", + "/v1/chat/completions", + { + "model": model, + "messages": [{"role": "user", "content": f"Weather and time in Paris? {identity}"}], + "tools": [_WEATHER_TOOL, _TIME_TOOL], + "cache": {"no-cache": True}, + }, + ) + assert response.status_code == 200, response.text + body: Final = response.json() + assert body["choices"] == [ + { + "finish_reason": "tool_calls", + "index": 0, + "message": { + "role": "assistant", + "content": "Checking both.", + "tool_calls": _TWO_CALLS_CHAT_SHAPE, + }, + } + ], response.text + assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/responses")] + + def _responses_frame(event: str, payload: dict[str, JsonValue]) -> bytes: return f"event: {event}\ndata: {json.dumps(payload, separators=(',', ':'))}\n\n".encode() @@ -743,6 +958,34 @@ def _function_call_frames(output_index: int, item: dict[str, JsonValue]) -> tupl ) +def _reasoning_item_frames(output_index: int, item: dict[str, JsonValue]) -> tuple[bytes, ...]: + text: Final = object_value(list_value(item["summary"])[0])["text"] + return ( + _responses_frame( + "response.output_item.added", + { + "type": "response.output_item.added", + "output_index": output_index, + "item": {**item, "summary": []}, + }, + ), + _responses_frame( + "response.reasoning_summary_text.delta", + { + "type": "response.reasoning_summary_text.delta", + "item_id": item["id"], + "output_index": output_index, + "summary_index": 0, + "delta": text, + }, + ), + _responses_frame( + "response.output_item.done", + {"type": "response.output_item.done", "output_index": output_index, "item": item}, + ), + ) + + def _sse_payloads(lines: list[str]) -> list[dict[str, JsonValue]]: return [json.loads(line[6:]) for line in lines if line.startswith("data: ") and line != "data: [DONE]"] @@ -826,3 +1069,231 @@ def test_chat_stream_over_responses_deployment_folds_two_messages_and_function_c ), chunks assert [choice["finish_reason"] for choice in choices if choice.get("finish_reason")] == ["tool_calls"], chunks assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/responses")] + + +def test_chat_stream_over_responses_deployment_folds_commentary_and_final_answer_into_one_choice( + gateway: Gateway, +) -> None: + identity: Final = "responses-bridge-commentary-final-stream-" + uuid.uuid4().hex + response_payload: Final[dict[str, JsonValue]] = { + "id": "resp_commentary_final_stream", + "object": "response", + "created_at": 1789788253, + "model": "gpt-6-sol", + "status": "in_progress", + "output": [], + } + + def respond(request: Request) -> Reply: + if request.method == "GET" and request.target == "/v1/models": + return Reply(body=b'{"object":"list","data":[]}') + assert request.method == "POST" and request.target == "/responses", request.target + assert json.loads(request.body)["stream"] is True, request.body + return Reply( + content_type="text/event-stream", + chunks=( + _responses_frame("response.created", {"type": "response.created", "response": response_payload}), + *_message_item_frames(0, _COMMENTARY_THEN_FINAL_ANSWER[0]), + *_message_item_frames(1, _COMMENTARY_THEN_FINAL_ANSWER[1]), + _responses_frame( + "response.completed", + { + "type": "response.completed", + "response": { + **response_payload, + "status": "completed", + "output": list(_COMMENTARY_THEN_FINAL_ANSWER), + "usage": {"input_tokens": 10, "output_tokens": 5, "total_tokens": 15}, + }, + }, + ), + ), + ) + + with wire_server(respond) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model="openai/responses/gpt-6-sol", api_base=wire.url, api_key="synthetic-openai-key" + ) + response: Final = gateway.request( + "POST", + "/v1/chat/completions", + { + "model": model, + "messages": [{"role": "user", "content": f"Weather in Paris as JSON. {identity}"}], + "tools": [_WEATHER_TOOL], + "stream": True, + "cache": {"no-cache": True}, + }, + ) + assert response.status_code == 200, response.text + chunks: Final = _sse_payloads(list(response.iter_lines())) + choices: Final = [object_value(choice) for chunk in chunks for choice in list_value(chunk["choices"])] + assert [choice["index"] for choice in choices] == [0] * len(choices), chunks + deltas: Final = [object_value(choice["delta"]) for choice in choices] + assert ( + "".join(str(delta["content"]) for delta in deltas if delta.get("content")) + == 'Let me answer that.{"city": "Paris", "summary": "Sunny, 21C"}' + ), chunks + assert not [delta for delta in deltas if delta.get("tool_calls")], chunks + assert [choice["finish_reason"] for choice in choices if choice.get("finish_reason")] == ["stop"], chunks + assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/responses")] + + +def _stream_reply(response_id: str, output: tuple[dict[str, JsonValue], ...], frames: tuple[bytes, ...]) -> Reply: + response_payload: Final[dict[str, JsonValue]] = { + "id": response_id, + "object": "response", + "created_at": 1789788253, + "model": "gpt-6-sol", + "status": "in_progress", + "output": [], + } + return Reply( + content_type="text/event-stream", + chunks=( + _responses_frame("response.created", {"type": "response.created", "response": response_payload}), + *frames, + _responses_frame( + "response.completed", + { + "type": "response.completed", + "response": { + **response_payload, + "status": "completed", + "output": list(output), + "usage": {"input_tokens": 10, "output_tokens": 5, "total_tokens": 15}, + }, + }, + ), + ), + ) + + +def _stream_choices(response: httpx.Response) -> list[dict[str, JsonValue]]: + chunks: Final = _sse_payloads(list(response.iter_lines())) + return [object_value(choice) for chunk in chunks for choice in list_value(chunk["choices"])] + + +def _stream_tool_calls(deltas: list[dict[str, JsonValue]]) -> list[dict[str, JsonValue]]: + return [ + object_value(call) for delta in deltas if delta.get("tool_calls") for call in list_value(delta["tool_calls"]) + ] + + +def test_chat_stream_over_responses_deployment_folds_reasoning_two_messages_and_function_call_into_one_choice( + gateway: Gateway, +) -> None: + identity: Final = "responses-bridge-reasoning-two-messages-stream-" + uuid.uuid4().hex + + def respond(request: Request) -> Reply: + if request.method == "GET" and request.target == "/v1/models": + return Reply(body=b'{"object":"list","data":[]}') + assert request.method == "POST" and request.target == "/responses", request.target + assert json.loads(request.body)["stream"] is True, request.body + return _stream_reply( + "resp_reasoning_two_messages_stream", + _REASONING_THEN_TWO_MESSAGES_THEN_CALL, + ( + *_reasoning_item_frames(0, _REASONING_THEN_TWO_MESSAGES_THEN_CALL[0]), + *_message_item_frames(1, _REASONING_THEN_TWO_MESSAGES_THEN_CALL[1]), + *_message_item_frames(2, _REASONING_THEN_TWO_MESSAGES_THEN_CALL[2]), + *_function_call_frames(3, _REASONING_THEN_TWO_MESSAGES_THEN_CALL[3]), + ), + ) + + with wire_server(respond) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model="openai/responses/gpt-6-sol", api_base=wire.url, api_key="synthetic-openai-key" + ) + response: Final = gateway.request( + "POST", + "/v1/chat/completions", + { + "model": model, + "messages": [{"role": "user", "content": f"What is the weather in Paris? {identity}"}], + "tools": [_WEATHER_TOOL], + "stream": True, + "cache": {"no-cache": True}, + }, + ) + assert response.status_code == 200, response.text + choices: Final = _stream_choices(response) + assert [choice["index"] for choice in choices] == [0] * len(choices), choices + deltas: Final = [object_value(choice["delta"]) for choice in choices] + assert ( + "".join(str(delta["reasoning_content"]) for delta in deltas if delta.get("reasoning_content")) + == "Checking the forecast." + ), choices + assert ( + "".join(str(delta["content"]) for delta in deltas if delta.get("content")) + == "First message.Second message." + ), choices + tool_calls: Final = _stream_tool_calls(deltas) + assert [ + object_value(call["function"])["name"] for call in tool_calls if object_value(call["function"]).get("name") + ] == ["get_weather"], choices + assert ( + "".join(str(object_value(call["function"]).get("arguments") or "") for call in tool_calls) + == '{"city":"Paris"}' + ), choices + assert [choice["finish_reason"] for choice in choices if choice.get("finish_reason")] == ["tool_calls"], choices + assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/responses")] + + +def test_chat_stream_over_responses_deployment_folds_message_and_two_function_calls_into_one_choice( + gateway: Gateway, +) -> None: + identity: Final = "responses-bridge-two-calls-stream-" + uuid.uuid4().hex + + def respond(request: Request) -> Reply: + if request.method == "GET" and request.target == "/v1/models": + return Reply(body=b'{"object":"list","data":[]}') + assert request.method == "POST" and request.target == "/responses", request.target + assert json.loads(request.body)["stream"] is True, request.body + return _stream_reply( + "resp_two_calls_stream", + _MESSAGE_THEN_TWO_CALLS, + ( + *_message_item_frames(0, _MESSAGE_THEN_TWO_CALLS[0]), + *_function_call_frames(1, _MESSAGE_THEN_TWO_CALLS[1]), + *_function_call_frames(2, _MESSAGE_THEN_TWO_CALLS[2]), + ), + ) + + with wire_server(respond) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model="openai/responses/gpt-6-sol", api_base=wire.url, api_key="synthetic-openai-key" + ) + response: Final = gateway.request( + "POST", + "/v1/chat/completions", + { + "model": model, + "messages": [{"role": "user", "content": f"Weather and time in Paris? {identity}"}], + "tools": [_WEATHER_TOOL, _TIME_TOOL], + "stream": True, + "cache": {"no-cache": True}, + }, + ) + assert response.status_code == 200, response.text + choices: Final = _stream_choices(response) + assert [choice["index"] for choice in choices] == [0] * len(choices), choices + deltas: Final = [object_value(choice["delta"]) for choice in choices] + assert "".join(str(delta["content"]) for delta in deltas if delta.get("content")) == "Checking both.", choices + tool_calls: Final = _stream_tool_calls(deltas) + assert [ + (call["index"], object_value(call["function"])["name"]) + for call in tool_calls + if object_value(call["function"]).get("name") + ] == [(0, "get_weather"), (1, "get_time")], choices + arguments_by_index: Final = { + index: "".join( + str(object_value(call["function"]).get("arguments") or "") + for call in tool_calls + if call["index"] == index + ) + for index in (0, 1) + } + assert arguments_by_index == {0: '{"city":"Paris"}', 1: '{"city":"Paris","tz":"CET"}'}, choices + assert [choice["finish_reason"] for choice in choices if choice.get("finish_reason")] == ["tool_calls"], choices + assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/responses")] diff --git a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py index 294e2386fdd..8072919ed87 100644 --- a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py +++ b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py @@ -5197,3 +5197,73 @@ def test_convert_response_output_folds_two_messages_and_function_call_into_one_c "index": 0, } ] + + +def test_convert_response_output_folds_commentary_and_final_answer_into_one_choice() -> None: + commentary: Final = ResponseOutputMessage.model_validate( + { + "id": "msg_commentary", + "type": "message", + "role": "assistant", + "status": "completed", + "phase": "commentary", + "content": [{"type": "output_text", "text": "Let me answer that.", "annotations": []}], + } + ) + final_answer: Final = ResponseOutputMessage.model_validate( + { + "id": "msg_final", + "type": "message", + "role": "assistant", + "status": "completed", + "phase": "final_answer", + "content": [{"type": "output_text", "text": '{"city": "Paris"}', "annotations": []}], + } + ) + + choices: Final = LiteLLMResponsesTransformationHandler._convert_response_output_to_choices( # pyright: ignore[reportPrivateUsage] # exercises the converter directly + (commentary, final_answer) + ) + + assert len(choices) == 1 + assert choices[0].index == 0 + assert choices[0].finish_reason == "stop" + assert choices[0].message.content == 'Let me answer that.{"city": "Paris"}' + assert choices[0].message.tool_calls is None + + +def test_convert_response_output_numbers_two_function_calls_zero_and_one() -> None: + message: Final = ResponseOutputMessage.model_validate( + { + "id": "msg_only", + "type": "message", + "role": "assistant", + "status": "completed", + "content": [{"type": "output_text", "text": "Checking both.", "annotations": []}], + } + ) + weather: Final = ResponseFunctionToolCall( + id="fc_1", + type="function_call", + status="completed", + arguments='{"city":"Paris"}', + call_id="call_1", + name="get_weather", + ) + time: Final = ResponseFunctionToolCall( + id="fc_2", + type="function_call", + status="completed", + arguments='{"city":"Paris"}', + call_id="call_2", + name="get_time", + ) + + choices: Final = LiteLLMResponsesTransformationHandler._convert_response_output_to_choices( # pyright: ignore[reportPrivateUsage] # the fold is the unit under test + (message, weather, time) + ) + + assert len(choices) == 1 + tool_calls: Final = choices[0].message.tool_calls + assert tool_calls is not None + assert [(call.index, call.function.name) for call in tool_calls] == [(0, "get_weather"), (1, "get_time")] diff --git a/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py b/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py index ac02354e4d9..c7f20a1d959 100644 --- a/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py +++ b/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py @@ -3,6 +3,7 @@ from copy import deepcopy from typing import Final, Literal import pytest +from openai.types.responses import ResponseFunctionToolCall from openai.types.responses.response_function_web_search import ( ActionFind, ActionOpenPage, @@ -39,11 +40,7 @@ class TestLiteLLMCompletionResponsesConfig: input_item = {"type": "input_file", "file_id": "file-abc123xyz"} # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(input_item) # Assert expected = {"type": "file", "file": {"file_id": "file-abc123xyz"}} @@ -58,11 +55,7 @@ class TestLiteLLMCompletionResponsesConfig: input_item = {"type": "input_file", "file_data": file_data} # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(input_item) # Assert expected = {"type": "file", "file": {"file_data": file_data}} @@ -80,11 +73,7 @@ class TestLiteLLMCompletionResponsesConfig: } # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(input_item) # Assert expected = { @@ -102,11 +91,7 @@ class TestLiteLLMCompletionResponsesConfig: input_item = {"type": "input_file"} # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(input_item) # Assert expected = {"type": "file", "file": {}} @@ -125,11 +110,7 @@ class TestLiteLLMCompletionResponsesConfig: } # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(input_item) # Assert expected = {"type": "file", "file": {"file_id": "file-abc123xyz"}} @@ -139,14 +120,12 @@ class TestLiteLLMCompletionResponsesConfig: def test_transform_input_file_item_to_file_item_keeps_filename(self): """OpenAI rejects file_data with no filename beside it, so dropping it 400s the request""" - result = ( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( - { - "type": "input_file", - "filename": "report.pdf", - "file_data": "data:application/pdf;base64,JVBERi0=", - } - ) + result = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( + { + "type": "input_file", + "filename": "report.pdf", + "file_data": "data:application/pdf;base64,JVBERi0=", + } ) assert result == { "type": "file", @@ -158,10 +137,8 @@ class TestLiteLLMCompletionResponsesConfig: def test_transform_input_file_item_to_file_item_with_file_url(self): """file_url should be mapped to file_id for downstream URL handling""" - result = ( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( - {"type": "input_file", "file_url": "https://example.com/doc.pdf"} - ) + result = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( + {"type": "input_file", "file_url": "https://example.com/doc.pdf"} ) assert result == { "type": "file", @@ -170,14 +147,12 @@ class TestLiteLLMCompletionResponsesConfig: def test_transform_input_file_item_file_id_takes_precedence_over_file_url(self): """explicit file_id should not be overwritten by file_url""" - result = ( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( - { - "type": "input_file", - "file_id": "file-abc123", - "file_url": "https://example.com/doc.pdf", - } - ) + result = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item( + { + "type": "input_file", + "file_id": "file-abc123", + "file_url": "https://example.com/doc.pdf", + } ) assert result == {"type": "file", "file": {"file_id": "file-abc123"}} @@ -188,11 +163,7 @@ class TestLiteLLMCompletionResponsesConfig: input_item = {"type": "input_image", "image_url": image_url, "detail": "high"} # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(input_item) # Assert expected = { @@ -211,11 +182,7 @@ class TestLiteLLMCompletionResponsesConfig: input_item = {"type": "input_image", "image_url": image_url, "detail": "high"} # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(input_item) # Assert expected = { @@ -234,11 +201,7 @@ class TestLiteLLMCompletionResponsesConfig: input_item = {"type": "input_image", "image_url": image_url} # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(input_item) # Assert expected = { @@ -256,11 +219,7 @@ class TestLiteLLMCompletionResponsesConfig: input_item = {"type": "input_image"} # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(input_item) # Assert expected = {"type": "image_url", "image_url": {"url": "", "detail": "auto"}} @@ -280,11 +239,7 @@ class TestLiteLLMCompletionResponsesConfig: } # Execute - result = ( - LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item( - input_item - ) - ) + result = LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(input_item) # Assert expected = { @@ -320,28 +275,26 @@ class TestLiteLLMCompletionResponsesConfig: ) # Execute - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="What is the meaning of life?", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="What is the meaning of life?", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) # Assert assert hasattr(responses_api_response, "output") assert len(responses_api_response.output) >= 2 - reasoning_items = [ - item for item in responses_api_response.output if item.type == "reasoning" - ] + reasoning_items = [item for item in responses_api_response.output if item.type == "reasoning"] assert len(reasoning_items) == 1, "Should have exactly one reasoning item" reasoning_item = reasoning_items[0] # Note: ID auto-generation was disabled, so reasoning items may not have IDs # Only assert ID format if an ID is present if hasattr(reasoning_item, "id") and reasoning_item.id: - assert reasoning_item.id.startswith( - "rs_" - ), f"Expected ID to start with 'rs_', got: {reasoning_item.id}" + assert reasoning_item.id.startswith("rs_"), f"Expected ID to start with 'rs_', got: {reasoning_item.id}" assert reasoning_item.status == "completed" assert reasoning_item.role == "assistant" assert len(reasoning_item.content) == 1 @@ -349,9 +302,7 @@ class TestLiteLLMCompletionResponsesConfig: assert "step by step" in reasoning_item.content[0].text assert "42" in reasoning_item.content[0].text - message_items = [ - item for item in responses_api_response.output if item.type == "message" - ] + message_items = [item for item in responses_api_response.output if item.type == "message"] assert len(message_items) == 1, "Should have exactly one message item" message_item = message_items[0] @@ -378,21 +329,19 @@ class TestLiteLLMCompletionResponsesConfig: ) # Execute - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="A simple question?", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="A simple question?", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) # Assert - reasoning_items = [ - item for item in responses_api_response.output if item.type == "reasoning" - ] + reasoning_items = [item for item in responses_api_response.output if item.type == "reasoning"] assert len(reasoning_items) == 0, "Should have no reasoning items" - message_items = [ - item for item in responses_api_response.output if item.type == "message" - ] + message_items = [item for item in responses_api_response.output if item.type == "message"] assert len(message_items) == 1, "Should have exactly one message item" assert message_items[0].content[0].text == "Just a regular answer." assert responses_api_response.object == "response" @@ -428,22 +377,20 @@ class TestLiteLLMCompletionResponsesConfig: ) # Execute - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="A question with multiple answers?", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="A question with multiple answers?", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) # Assert - reasoning_items = [ - item for item in responses_api_response.output if item.type == "reasoning" - ] + reasoning_items = [item for item in responses_api_response.output if item.type == "reasoning"] assert len(reasoning_items) == 1, "Should have exactly one reasoning item" assert reasoning_items[0].content[0].text == "First reasoning process." - message_items = [ - item for item in responses_api_response.output if item.type == "message" - ] + message_items = [item for item in responses_api_response.output if item.type == "message"] assert len(message_items) == 2, "Should have two message items" def test_signature_only_thinking_block_still_emits_reasoning_item(self): @@ -460,23 +407,21 @@ class TestLiteLLMCompletionResponsesConfig: content="10", role="assistant", reasoning_content="", - thinking_blocks=[ - {"type": "thinking", "thinking": "", "signature": "signature-payload"} - ], + thinking_blocks=[{"type": "thinking", "thinking": "", "signature": "signature-payload"}], ), ) ], ) - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="Test input", - responses_api_request={}, - chat_completion_response=response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="Test input", + responses_api_request={}, + chat_completion_response=response, + ) ) - reasoning_items = [ - item for item in responses_api_response.output if item.type == "reasoning" - ] + reasoning_items = [item for item in responses_api_response.output if item.type == "reasoning"] assert len(reasoning_items) == 1, "Signature-only thinking should still surface a reasoning item" assert reasoning_items[0].content == [] assert "signature-payload" in reasoning_items[0].encrypted_content @@ -500,15 +445,15 @@ class TestLiteLLMCompletionResponsesConfig: ], ) - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="Test input", - responses_api_request={}, - chat_completion_response=response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="Test input", + responses_api_request={}, + chat_completion_response=response, + ) ) - reasoning_items = [ - item for item in responses_api_response.output if item.type == "reasoning" - ] + reasoning_items = [item for item in responses_api_response.output if item.type == "reasoning"] assert len(reasoning_items) == 1 assert "redacted-payload" in reasoning_items[0].encrypted_content @@ -526,23 +471,21 @@ class TestLiteLLMCompletionResponsesConfig: content="10", role="assistant", reasoning_content="counting the primes", - thinking_blocks=[ - {"type": "thinking", "thinking": "counting the primes", "signature": "sig"} - ], + thinking_blocks=[{"type": "thinking", "thinking": "counting the primes", "signature": "sig"}], ), ) ], ) - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="Test input", - responses_api_request={}, - chat_completion_response=response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="Test input", + responses_api_request={}, + chat_completion_response=response, + ) ) - reasoning_items = [ - item for item in responses_api_response.output if item.type == "reasoning" - ] + reasoning_items = [item for item in responses_api_response.output if item.type == "reasoning"] assert len(reasoning_items) == 1 assert reasoning_items[0].content[0].text == "counting the primes" assert "sig" in reasoning_items[0].encrypted_content @@ -571,10 +514,12 @@ class TestLiteLLMCompletionResponsesConfig: ], ) - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="this is a test", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="this is a test", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) assert responses_api_response.status == "completed" @@ -610,15 +555,15 @@ class TestLiteLLMCompletionResponsesConfig: ], ) - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="this is a test", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="this is a test", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) - message_items = [ - item for item in responses_api_response.output if item.type == "message" - ] + message_items = [item for item in responses_api_response.output if item.type == "message"] assert len(message_items) > 0 for item in message_items: @@ -653,10 +598,12 @@ class TestLiteLLMCompletionResponsesConfig: ], ) - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="this is a test", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="this is a test", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) assert responses_api_response.status == "incomplete" @@ -691,10 +638,12 @@ class TestLiteLLMCompletionResponsesConfig: ], ) - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="what's the weather in SF?", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="what's the weather in SF?", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) output_types = [item.type for item in responses_api_response.output] @@ -717,10 +666,12 @@ class TestLiteLLMCompletionResponsesConfig: ], ) - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="what's the weather in SF?", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="what's the weather in SF?", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) message_items = [item for item in responses_api_response.output if item.type == "message"] @@ -754,10 +705,12 @@ class TestLiteLLMCompletionResponsesConfig: } # Execute - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="Test", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="Test", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) # Assert @@ -826,10 +779,12 @@ class TestLiteLLMCompletionResponsesConfig: ) # Execute - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="Test", - responses_api_request={}, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="Test", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) ) # Assert - should default to empty dict @@ -866,36 +821,34 @@ class TestLiteLLMCompletionResponsesConfig: ) try: - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="Spawn an agent", - responses_api_request={ - "tools": [ - { - "type": "namespace", - "name": "collaboration", - "tools": [ - { - "type": "function", - "name": "spawn_agent", - "parameters": { - "type": "object", - "properties": {}, - }, - } - ], - } - ] - }, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="Spawn an agent", + responses_api_request={ + "tools": [ + { + "type": "namespace", + "name": "collaboration", + "tools": [ + { + "type": "function", + "name": "spawn_agent", + "parameters": { + "type": "object", + "properties": {}, + }, + } + ], + } + ] + }, + chat_completion_response=chat_completion_response, + ) ) finally: TOOL_CALLS_CACHE.delete_cache(key=tool_call_id) - tool_calls = [ - item - for item in responses_api_response.output - if item.type == "function_call" - ] + tool_calls = [item for item in responses_api_response.output if item.type == "function_call"] assert len(tool_calls) == 1 assert tool_calls[0].name == "spawn_agent" assert tool_calls[0].namespace == "collaboration" @@ -933,33 +886,30 @@ class TestLiteLLMCompletionResponsesConfig: ) try: - responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="What is the weather in Paris?", - responses_api_request={ - "tools": [ - { - "type": "function", - "name": "get_weather", - "parameters": {"type": "object", "properties": {}}, - } - ] - }, - chat_completion_response=chat_completion_response, + responses_api_response = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="What is the weather in Paris?", + responses_api_request={ + "tools": [ + { + "type": "function", + "name": "get_weather", + "parameters": {"type": "object", "properties": {}}, + } + ] + }, + chat_completion_response=chat_completion_response, + ) ) finally: TOOL_CALLS_CACHE.delete_cache(key=tool_call_id) - tool_calls = [ - item - for item in responses_api_response.output - if item.type == "function_call" - ] + tool_calls = [item for item in responses_api_response.output if item.type == "function_call"] assert len(tool_calls) == 1 assert tool_calls[0].name == "get_weather" assert tool_calls[0].namespace is None assert "namespace" not in tool_calls[0].model_fields_set - def test_transform_top_level_function_collision_stays_unnamespaced(self): tool_call_id = "call_top_level_collision" chat_completion_response = ModelResponse( @@ -1033,26 +983,14 @@ class TestFunctionCallTransformation: regular_message = {"type": "message", "role": "user", "content": "Hello"} # Test function_call detection - assert LiteLLMCompletionResponsesConfig._is_input_item_function_call( - function_call_item - ) - assert not LiteLLMCompletionResponsesConfig._is_input_item_function_call( - function_call_output_item - ) - assert not LiteLLMCompletionResponsesConfig._is_input_item_function_call( - regular_message - ) + assert LiteLLMCompletionResponsesConfig._is_input_item_function_call(function_call_item) + assert not LiteLLMCompletionResponsesConfig._is_input_item_function_call(function_call_output_item) + assert not LiteLLMCompletionResponsesConfig._is_input_item_function_call(regular_message) # Test function_call_output detection (should still work) - assert LiteLLMCompletionResponsesConfig._is_input_item_tool_call_output( - function_call_output_item - ) - assert not LiteLLMCompletionResponsesConfig._is_input_item_tool_call_output( - function_call_item - ) - assert not LiteLLMCompletionResponsesConfig._is_input_item_tool_call_output( - regular_message - ) + assert LiteLLMCompletionResponsesConfig._is_input_item_tool_call_output(function_call_output_item) + assert not LiteLLMCompletionResponsesConfig._is_input_item_tool_call_output(function_call_item) + assert not LiteLLMCompletionResponsesConfig._is_input_item_tool_call_output(regular_message) def test_function_call_transformation(self): """Test that function_call items are correctly transformed to assistant messages with tool calls""" @@ -1198,9 +1136,7 @@ class TestFunctionCallTransformation: tool_msg = messages[2] assert tool_msg.get("role") == "tool" assert tool_msg.get("content") == "Rainy" - assert ( - tool_msg.get("tool_call_id") == "call_1fe70e2a-a596-45ef-b72c-9b8567c460e5" - ) + assert tool_msg.get("tool_call_id") == "call_1fe70e2a-a596-45ef-b72c-9b8567c460e5" def test_complete_request_transformation_with_function_calls(self): """Test the complete request transformation that would be used by the responses API""" @@ -1466,9 +1402,7 @@ class TestToolChoiceTransformation: Test that {"type": "tool"} is transformed to "required". This fixes the Anthropic error: "tool_choice.tool.name: Field required" """ - result = LiteLLMCompletionResponsesConfig._transform_tool_choice( - {"type": "tool"} - ) + result = LiteLLMCompletionResponsesConfig._transform_tool_choice({"type": "tool"}) assert result == "required" def test_transform_tool_choice_preserves_function_with_name(self): @@ -1480,9 +1414,7 @@ class TestToolChoiceTransformation: def test_transform_tool_choice_responses_flat_function_name(self): """Responses-API forced-function with a top-level name maps to the nested Chat Completions shape instead of degrading to required and dropping the name""" - result = LiteLLMCompletionResponsesConfig._transform_tool_choice( - {"type": "function", "name": "get_weather"} - ) + result = LiteLLMCompletionResponsesConfig._transform_tool_choice({"type": "function", "name": "get_weather"}) assert result == {"type": "function", "function": {"name": "get_weather"}} def test_transform_tool_choice_custom_follows_function_downgrade(self): @@ -1492,9 +1424,7 @@ class TestToolChoiceTransformation: function tool_choice naming the same tool or it references a tool type absent from the converted request. """ - flat = LiteLLMCompletionResponsesConfig._transform_tool_choice( - {"type": "custom", "name": "ApplyPatch"} - ) + flat = LiteLLMCompletionResponsesConfig._transform_tool_choice({"type": "custom", "name": "ApplyPatch"}) assert flat == {"type": "function", "function": {"name": "ApplyPatch"}} nested = LiteLLMCompletionResponsesConfig._transform_tool_choice( @@ -1508,16 +1438,12 @@ class TestToolChoiceTransformation: def test_transform_tool_choice_function_without_name_falls_back_to_required(self): """A function-type dict with no name still falls back to required""" - result = LiteLLMCompletionResponsesConfig._transform_tool_choice( - {"type": "function"} - ) + result = LiteLLMCompletionResponsesConfig._transform_tool_choice({"type": "function"}) assert result == "required" def test_transform_tool_choice_function_empty_name_falls_back_to_required(self): """An empty top-level name is falsy and must not produce an empty function name""" - result = LiteLLMCompletionResponsesConfig._transform_tool_choice( - {"type": "function", "name": ""} - ) + result = LiteLLMCompletionResponsesConfig._transform_tool_choice({"type": "function", "name": ""}) assert result == "required" @pytest.mark.parametrize( @@ -1571,10 +1497,12 @@ class TestToolChoiceTransformation: ], ) - responses_api_response: Final = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="Run the command pwd.", - responses_api_request={"tool_choice": {"type": "function", "name": "run_command"}}, - chat_completion_response=chat_completion_response, + responses_api_response: Final = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="Run the command pwd.", + responses_api_request={"tool_choice": {"type": "function", "name": "run_command"}}, + chat_completion_response=chat_completion_response, + ) ) assert responses_api_response.tool_choice == {"type": "function", "name": "run_command"} @@ -1594,10 +1522,12 @@ class TestToolChoiceTransformation: ], ) - responses_api_response: Final = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - request_input="Run the command pwd.", - responses_api_request={"tool_choice": "any"}, - chat_completion_response=chat_completion_response, + responses_api_response: Final = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="Run the command pwd.", + responses_api_request={"tool_choice": "any"}, + chat_completion_response=chat_completion_response, + ) ) assert responses_api_response.tool_choice == "auto" @@ -1611,20 +1541,12 @@ class TestContentTypeTransformation: Test that 'tool_result' content type is transformed to 'text'. This fixes: Invalid user message - content type 'tool_result' not valid. """ - result = ( - LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type( - "tool_result" - ) - ) + result = LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type("tool_result") assert result == "text" def test_input_text_content_type_transformed_to_text(self): """Test that 'input_text' content type is transformed to 'text'""" - result = ( - LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type( - "input_text" - ) - ) + result = LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type("input_text") assert result == "text" def test_none_text_blocks_filtered_out(self): @@ -1638,9 +1560,7 @@ class TestContentTypeTransformation: {"type": "text", "text": None}, # Should be filtered out {"type": "text", "text": "another valid"}, ] - result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( - content - ) + result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content(content) assert len(result) == 2 assert result[0]["text"] == "valid text" assert result[1]["text"] == "another valid" @@ -1659,9 +1579,7 @@ class TestContentTypeTransformation: }, ] - result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( - content - ) + result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content(content) assert result == [ {"type": "text", "text": "Payload:\n"}, @@ -1685,9 +1603,7 @@ class TestToolTransformation: ( result_tools, web_search_options, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -1709,9 +1625,7 @@ class TestToolTransformation: ( result_tools, web_search_options, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -1739,9 +1653,7 @@ class TestToolTransformation: ( result_tools, web_search_options, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert - computer_use has no Chat Completions equivalent, so it is dropped assert len(result_tools) == 0 @@ -1842,9 +1754,7 @@ class TestToolTransformation: ( result_tools, web_search_options, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert - custom tool is converted to a function tool assert len(result_tools) == 1 @@ -1869,9 +1779,7 @@ class TestToolTransformation: ( result_tools, web_search_options, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -1895,9 +1803,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -1918,9 +1824,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -1939,9 +1843,7 @@ class TestToolTransformation: tools = [custom_tool] with pytest.raises(ValueError, match="allowed_callers must be a list of strings"): - LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) def test_transform_web_search_tools_to_web_search_options(self): """Test that web_search tools are converted to web_search_options""" @@ -1957,9 +1859,7 @@ class TestToolTransformation: ( result_tools, web_search_options, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 0 # Web search is not added to tools @@ -1990,9 +1890,7 @@ class TestToolTransformation: ( result_tools, web_search_options, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -2022,9 +1920,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -2050,9 +1946,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -2091,8 +1985,8 @@ class TestToolTransformation: "eager_input_streaming": eager_input_streaming, } - result_tools: Final = LiteLLMCompletionResponsesConfig.transform_chat_completion_tool_params_to_responses_api_tools( - [chat_tool] + result_tools: Final = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_tool_params_to_responses_api_tools([chat_tool]) ) assert result_tools[0]["eager_input_streaming"] is eager_input_streaming @@ -2110,9 +2004,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -2136,9 +2028,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 2 @@ -2170,14 +2060,10 @@ class TestToolTransformation: ( result_tools, web_search_options, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert - assert ( - len(result_tools) == 3 - ) # function, mcp, vertex (web_search becomes options) + assert len(result_tools) == 3 # function, mcp, vertex (web_search becomes options) assert web_search_options is not None # Check function tool @@ -2207,9 +2093,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -2219,7 +2103,11 @@ class TestToolTransformation: def test_transform_function_tools_parameters_keep_client_key_order(self): tools = [ - {"type": "function", "name": "a", "parameters": {"properties": {"arg": {"type": "string"}}, "required": ["arg"]}}, + { + "type": "function", + "name": "a", + "parameters": {"properties": {"arg": {"type": "string"}}, "required": ["arg"]}, + }, {"type": "function", "name": "b", "parameters": {"type": "object", "properties": {}}}, ] @@ -2245,9 +2133,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -2268,9 +2154,7 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 @@ -2292,19 +2176,14 @@ class TestToolTransformation: ( result_tools, _, - ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=tools - ) + ) = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) # Assert assert len(result_tools) == 1 result_tool = result_tools[0] assert result_tool["function"]["parameters"]["type"] == "object" assert "properties" in result_tool["function"]["parameters"] - assert ( - result_tool["function"]["parameters"]["properties"]["arg"]["type"] - == "string" - ) + assert result_tool["function"]["parameters"]["properties"]["arg"]["type"] == "string" def test_bedrock_anthropic_drops_derived_web_search_options(self): """ @@ -2512,7 +2391,6 @@ class TestToolTransformation: assert result_tools[0]["allowed_callers"] == ["code_execution_20250825"] - @pytest.mark.parametrize("nested", [True, False]) def test_transform_namespace_tools_rejects_invalid_allowed_callers(self, nested): function_tool = { @@ -2536,7 +2414,6 @@ class TestToolTransformation: tools=[namespace_tool] ) - def test_transform_flat_namespace_tools_to_function_tools(self): namespace_tool = { "type": "namespace", @@ -2582,9 +2459,7 @@ class TestToolTransformation: ], } - result = LiteLLMCompletionResponsesConfig.namespace_tool_name_map( - [namespace_tool] - ) + result = LiteLLMCompletionResponsesConfig.namespace_tool_name_map([namespace_tool]) assert result["collaboration__wait_agent"] == ("collaboration", "wait_agent") assert result["wait_agent"] == ("collaboration", "wait_agent") @@ -2628,9 +2503,7 @@ class TestToolTransformation: }, ] - result = LiteLLMCompletionResponsesConfig.namespace_tool_name_map( - tools - ) + result = LiteLLMCompletionResponsesConfig.namespace_tool_name_map(tools) assert result["alpha__run"] == ("alpha", "run") assert result["beta__run"] == ("beta", "run") @@ -2658,7 +2531,6 @@ class TestToolTransformation: assert result["admin__run"] == ("admin", "run") assert "run" not in result - def test_transform_tools_rejects_flattened_name_collision(self): tools = [ { @@ -2685,7 +2557,6 @@ class TestToolTransformation: ): LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools(tools=tools) - def test_restore_namespace_tool_name_leaves_unknown_tool_unchanged(self): tool_name, namespace = LiteLLMCompletionResponsesConfig._restore_namespace_tool_name( "mcp__node_repl", @@ -2711,10 +2582,8 @@ class TestToolTransformation: ], } - result_tools, _ = ( - LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=[namespace_tool] - ) + result_tools, _ = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( + tools=[namespace_tool] ) assert len(result_tools) == 1 @@ -2739,10 +2608,8 @@ class TestToolTransformation: ], } - result_tools, _ = ( - LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( - tools=[namespace_tool] - ) + result_tools, _ = LiteLLMCompletionResponsesConfig.transform_responses_api_tools_to_chat_completion_tools( + tools=[namespace_tool] ) assert len(result_tools) == 1 @@ -2964,11 +2831,7 @@ class TestToolTransformation: assert "web_search_options" not in result bedrock_tool_blocks = _bedrock_tools_pt(tools=result["tools"], model=model) - names = [ - block["toolSpec"]["name"] - for block in bedrock_tool_blocks - if "toolSpec" in block - ] + names = [block["toolSpec"]["name"] for block in bedrock_tool_blocks if "toolSpec" in block] assert names == ["noop"] assert not any(name.startswith("litellm_unnamed_tool_") for name in names) @@ -3303,9 +3166,7 @@ class TestUsageTransformation: Choices( finish_reason="stop", index=0, - message=Message( - content="Here is the generated image.", role="assistant" - ), + message=Message(content="Here is the generated image.", role="assistant"), ) ], ) @@ -3530,9 +3391,9 @@ class TestStreamingIDConsistency: # Verify the cached ID is set and matches assert iterator._cached_item_id is not None, "Iterator should cache the item_id" assert iterator._cached_item_id == item_id_1, "Cached ID should match event IDs" - assert iterator._cached_item_id.startswith( - "msg_" - ), "Message item IDs must use the Responses API msg_ prefix (issue #27333)" + assert iterator._cached_item_id.startswith("msg_"), ( + "Message item IDs must use the Responses API msg_ prefix (issue #27333)" + ) def test_streaming_iterator_initial_events_use_cached_id(self): """ @@ -3622,9 +3483,7 @@ class TestStreamingIDConsistency: # Create done events text_done_event = iterator.create_output_text_done_event(complete_response) - content_done_event = iterator.create_output_content_part_done_event( - complete_response - ) + content_done_event = iterator.create_output_content_part_done_event(complete_response) item_done_event = iterator.create_output_item_done_event(complete_response) # Extract IDs @@ -3684,27 +3543,19 @@ class TestStreamingIDConsistency: input=input_items ) - roles = [ - m.get("role") if isinstance(m, dict) else getattr(m, "role", None) - for m in messages - ] + roles = [m.get("role") if isinstance(m, dict) else getattr(m, "role", None) for m in messages] # Must not have two consecutive assistant messages for i in range(len(roles) - 1): - assert not ( - roles[i] == "assistant" and roles[i + 1] == "assistant" - ), f"Consecutive assistant messages at indices {i} and {i+1}: {roles}" + assert not (roles[i] == "assistant" and roles[i + 1] == "assistant"), ( + f"Consecutive assistant messages at indices {i} and {i + 1}: {roles}" + ) # The single assistant message must contain BOTH tool_calls assistant_messages = [ - m - for m in messages - if (m.get("role") if isinstance(m, dict) else getattr(m, "role", None)) - == "assistant" + m for m in messages if (m.get("role") if isinstance(m, dict) else getattr(m, "role", None)) == "assistant" ] - assert ( - len(assistant_messages) == 1 - ), f"Expected 1 assistant message, got {len(assistant_messages)}" + assert len(assistant_messages) == 1, f"Expected 1 assistant message, got {len(assistant_messages)}" assistant_msg = assistant_messages[0] tool_calls = ( @@ -3712,27 +3563,19 @@ class TestStreamingIDConsistency: if isinstance(assistant_msg, dict) else getattr(assistant_msg, "tool_calls", None) ) - assert ( - tool_calls is not None and len(tool_calls) == 2 - ), f"Expected 2 tool_calls in the merged assistant message, got: {tool_calls}" + assert tool_calls is not None and len(tool_calls) == 2, ( + f"Expected 2 tool_calls in the merged assistant message, got: {tool_calls}" + ) - call_ids = [ - (tc.get("id") if isinstance(tc, dict) else getattr(tc, "id", None)) - for tc in tool_calls - ] + call_ids = [(tc.get("id") if isinstance(tc, dict) else getattr(tc, "id", None)) for tc in tool_calls] assert "toolu_01" in call_ids, f"toolu_01 missing from tool_calls: {call_ids}" assert "toolu_02" in call_ids, f"toolu_02 missing from tool_calls: {call_ids}" # Both tool messages must be present tool_messages = [ - m - for m in messages - if (m.get("role") if isinstance(m, dict) else getattr(m, "role", None)) - == "tool" + m for m in messages if (m.get("role") if isinstance(m, dict) else getattr(m, "role", None)) == "tool" ] - assert ( - len(tool_messages) == 2 - ), f"Expected 2 tool messages, got {len(tool_messages)}" + assert len(tool_messages) == 2, f"Expected 2 tool messages, got {len(tool_messages)}" def test_single_tool_call_still_works_after_merge_fix(self): """ @@ -3754,20 +3597,14 @@ class TestStreamingIDConsistency: input=input_items ) - roles = [ - m.get("role") if isinstance(m, dict) else getattr(m, "role", None) - for m in messages - ] + roles = [m.get("role") if isinstance(m, dict) else getattr(m, "role", None) for m in messages] assert "user" in roles assert "assistant" in roles assert "tool" in roles assistant_messages = [ - m - for m in messages - if (m.get("role") if isinstance(m, dict) else getattr(m, "role", None)) - == "assistant" + m for m in messages if (m.get("role") if isinstance(m, dict) else getattr(m, "role", None)) == "assistant" ] assert len(assistant_messages) == 1 @@ -3938,9 +3775,7 @@ class TestEnsureOutputItemContentPartAdded: LiteLLMCompletionStreamingIterator, ) - iterator = LiteLLMCompletionStreamingIterator.__new__( - LiteLLMCompletionStreamingIterator - ) + iterator = LiteLLMCompletionStreamingIterator.__new__(LiteLLMCompletionStreamingIterator) iterator.sent_output_item_added_event = False iterator.sent_content_part_added_event = False iterator._sequence_number = 0 @@ -4181,7 +4016,9 @@ class TestEnsureOutputItemContentPartAdded: function_items = [item for item in items if item.call_id == "call_fn"] custom_items = [item for item in items if item.call_id == "call_custom"] assert len(function_items) == 2 and len(custom_items) == 2 - assert all((item.type, item.name, item.namespace) == ("function_call", "run", "beta") for item in function_items) + assert all( + (item.type, item.name, item.namespace) == ("function_call", "run", "beta") for item in function_items + ) assert function_items[-1].arguments == '{"job_id":"42"}' assert all(item.type == "custom_tool_call" and item.name == "run" for item in custom_items) assert all(getattr(item, "namespace", None) is None for item in custom_items) @@ -4380,7 +4217,6 @@ class TestEnsureOutputItemContentPartAdded: assert added.item.name == "run" assert getattr(added.item, "namespace", None) is None - def test_streaming_namespace_map_is_built_once(self): from unittest.mock import MagicMock, patch @@ -4422,7 +4258,6 @@ class TestEnsureOutputItemContentPartAdded: namespace_map.assert_called_once_with(request["tools"]) - def test_emit_terminal_event_uses_stream_finish_reason(self): """ The terminal stream event type must track the assembled response @@ -4512,9 +4347,7 @@ class TestCacheControlPreservation: "cache_control": {"type": "ephemeral"}, } ] - result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( - content - ) + result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content(content) assert isinstance(result, list) assert len(result) == 1 assert result[0]["cache_control"] == {"type": "ephemeral"} @@ -4522,9 +4355,7 @@ class TestCacheControlPreservation: def test_content_without_cache_control_unaffected(self): """Content blocks that don't have cache_control should be unaffected.""" content = [{"type": "text", "text": "hello"}] - result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( - content - ) + result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content(content) assert isinstance(result, list) assert len(result) == 1 assert "cache_control" not in result[0] @@ -4546,9 +4377,7 @@ class TestCacheControlPreservation: ) assert len(messages) == 1 msg_content = ( - messages[0].get("content") - if isinstance(messages[0], dict) - else getattr(messages[0], "content", None) + messages[0].get("content") if isinstance(messages[0], dict) else getattr(messages[0], "content", None) ) assert isinstance(msg_content, list) assert msg_content[0]["cache_control"] == {"type": "ephemeral"} @@ -4561,9 +4390,7 @@ class TestCacheControlPreservation: "cache_control": {"type": "ephemeral"}, } ] - result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( - content - ) + result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content(content) assert isinstance(result, list) assert len(result) == 1 assert result[0]["cache_control"] == {"type": "ephemeral"} @@ -4576,9 +4403,7 @@ class TestCacheControlPreservation: "cache_control": {"type": "ephemeral"}, } ] - result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( - content - ) + result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content(content) assert isinstance(result, list) assert len(result) == 1 assert result[0]["cache_control"] == {"type": "ephemeral"} @@ -4594,18 +4419,12 @@ def test_function_call_tool_id_falls_back_to_unique_id_for_degenerate_call_id(): for the bedrock-mantle gpt-5.5 non-streaming path.""" from types import SimpleNamespace - convert = ( - LiteLLMCompletionResponsesConfig.convert_response_function_tool_call_to_chat_completion_tool_call - ) + convert = LiteLLMCompletionResponsesConfig.convert_response_function_tool_call_to_chat_completion_tool_call - mantle = SimpleNamespace( - id="fc_unique_abc123", call_id="call_0", name="get_weather", arguments="{}" - ) + mantle = SimpleNamespace(id="fc_unique_abc123", call_id="call_0", name="get_weather", arguments="{}") assert convert(mantle)["id"] == "fc_unique_abc123" - openai = SimpleNamespace( - id="fc_2", call_id="call_tokyo", name="get_weather", arguments="{}" - ) + openai = SimpleNamespace(id="fc_2", call_id="call_tokyo", name="get_weather", arguments="{}") assert convert(openai)["id"] == "call_tokyo" @@ -4670,7 +4489,7 @@ class TestHostedWebSearchReplay: text_parts: Final = tuple(block["text"] for block in content if block.get("type") == "text") assert text_parts[0] == "I found a forecast source." replayed_searches: Final = tuple( - ResponseFunctionWebSearch.model_validate_json(text[text.index("{"):]) + ResponseFunctionWebSearch.model_validate_json(text[text.index("{") :]) for text in text_parts if "web_search_call" in text ) @@ -4705,8 +4524,10 @@ class TestHostedWebSearchReplay: input_item: Final = search.model_dump(exclude_none=True) original: Final = deepcopy(input_item) - messages: Final = LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( - input_item=input_item + messages: Final = ( + LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( + input_item=input_item + ) ) assert len(messages) == 1 @@ -4714,7 +4535,7 @@ class TestHostedWebSearchReplay: assert not messages[0].get("tool_calls") content: Final = messages[0].get("content") assert isinstance(content, str) - replayed: Final = ResponseFunctionWebSearch.model_validate_json(content[content.index("{"):]) + replayed: Final = ResponseFunctionWebSearch.model_validate_json(content[content.index("{") :]) assert replayed == search assert input_item == original @@ -4787,7 +4608,7 @@ class TestHostedWebSearchReplay: assert text_parts[0] == "I will check the forecast." assert text_parts[-1] == "I found two sources." replayed_searches: Final = tuple( - ResponseFunctionWebSearch.model_validate_json(text[text.index("{"):]) + ResponseFunctionWebSearch.model_validate_json(text[text.index("{") :]) for text in text_parts if "web_search_call" in text ) @@ -4802,13 +4623,16 @@ class TestHostedWebSearchReplay: assistant_blocks: Final = provider_messages[1]["content"] result_blocks: Final = provider_messages[2]["content"] assert [block["id"] for block in assistant_blocks if block.get("type") == "tool_use"] == [ - "call_weather", "call_time" + "call_weather", + "call_time", ] assert [block["tool_use_id"] for block in result_blocks if block.get("type") == "tool_result"] == [ - "call_weather", "call_time" + "call_weather", + "call_time", ] assert [block["content"] for block in result_blocks if block.get("type") == "tool_result"] == [ - "Paris is sunny.", "12:00" + "Paris is sunny.", + "12:00", ] assert [block["text"] for block in assistant_blocks if block.get("type") == "text"] == list(text_parts) assert history == original @@ -5046,9 +4870,7 @@ class TestStreamingSnapshotItemIds: def test_incremental_item_id_uses_msg_prefix(self): iterator = self._make_iterator() - event = iterator._transform_chat_completion_chunk_to_response_api_chunk( - self._make_chunk("apple") - ) + event = iterator._transform_chat_completion_chunk_to_response_api_chunk(self._make_chunk("apple")) assert event is not None assert event.item_id.startswith("msg_") @@ -5057,14 +4879,10 @@ class TestStreamingSnapshotItemIds: def test_completed_snapshot_reuses_streamed_item_id(self): iterator = self._make_iterator() - streamed_event = iterator._transform_chat_completion_chunk_to_response_api_chunk( - self._make_chunk("apple") - ) + streamed_event = iterator._transform_chat_completion_chunk_to_response_api_chunk(self._make_chunk("apple")) assert streamed_event is not None - completed_event = iterator._emit_terminal_response_event( - _bridged_chat_completion_response() - ) + completed_event = iterator._emit_terminal_response_event(_bridged_chat_completion_response()) assert completed_event is not None message_items = _bridged_output_items(completed_event.response, "message") @@ -5073,13 +4891,9 @@ class TestStreamingSnapshotItemIds: def test_completed_snapshot_item_id_is_replayable(self): iterator = self._make_iterator() - iterator._transform_chat_completion_chunk_to_response_api_chunk( - self._make_chunk("apple") - ) + iterator._transform_chat_completion_chunk_to_response_api_chunk(self._make_chunk("apple")) - completed_event = iterator._emit_terminal_response_event( - _bridged_chat_completion_response() - ) + completed_event = iterator._emit_terminal_response_event(_bridged_chat_completion_response()) assert completed_event is not None for item in _bridged_output_items(completed_event.response, "message"): @@ -5106,18 +4920,14 @@ class TestStreamingSnapshotItemIds: def _reasoning_chat_completion_response(self): message = Message(role="assistant", content="apple") message.reasoning_content = "thinking about fruit" - return _bridged_chat_completion_response( - choices=[Choices(index=0, finish_reason="stop", message=message)] - ) + return _bridged_chat_completion_response(choices=[Choices(index=0, finish_reason="stop", message=message)]) def test_reasoning_delta_events_share_one_item_id(self): """The old rs_{hash(text)} ID changed with every delta, so a client accumulating reasoning by item ID saw a new item per chunk.""" iterator = self._make_iterator() - first = iterator._transform_chat_completion_chunk_to_response_api_chunk( - self._make_reasoning_chunk("thinking ") - ) + first = iterator._transform_chat_completion_chunk_to_response_api_chunk(self._make_reasoning_chunk("thinking ")) second = iterator._transform_chat_completion_chunk_to_response_api_chunk( self._make_reasoning_chunk("about fruit") ) @@ -5134,9 +4944,7 @@ class TestStreamingSnapshotItemIds: ) assert streamed_event is not None - completed_event = iterator._emit_terminal_response_event( - self._reasoning_chat_completion_response() - ) + completed_event = iterator._emit_terminal_response_event(self._reasoning_chat_completion_response()) assert completed_event is not None reasoning_items = _bridged_output_items(completed_event.response, "reasoning") @@ -5352,3 +5160,23 @@ async def test_bridge_rejects_untranslatable_tool_choice_with_a_400(stream: bool ) assert exc_info.value.status_code == 400 assert "tool_choice={'type': 'file_search'}" in str(exc_info.value) + + +@pytest.mark.parametrize("index", [0, 1, 2]) +def test_convert_response_function_tool_call_keeps_the_index_it_is_given(index: int) -> None: + item: Final = ResponseFunctionToolCall( + id="fc_x", + type="function_call", + status="completed", + arguments='{"city":"Paris"}', + call_id="call_x", + name="get_weather", + ) + + tool_call: Final = ( + LiteLLMCompletionResponsesConfig.convert_response_function_tool_call_to_chat_completion_tool_call( + tool_call_item=item, index=index + ) + ) + + assert tool_call["index"] == index