diff --git a/litellm/llms/base_llm/responses/transformation.py b/litellm/llms/base_llm/responses/transformation.py index 14f00aaaa21..49ec0939932 100644 --- a/litellm/llms/base_llm/responses/transformation.py +++ b/litellm/llms/base_llm/responses/transformation.py @@ -331,6 +331,15 @@ class BaseResponsesAPIConfig(ABC): ########## END COMPACT RESPONSE API TRANSFORMATION ###### ######################################################### + @staticmethod + def normalize_responses_input(input: str | ResponseInputParam) -> ResponseInputParam: + if isinstance(input, str): + return cast( + "ResponseInputParam", + [{"role": "user", "content": input}], # mutable-ok: outbound Responses input item + ) # cast-ok: OpenAI string shorthand is one user item + return input + @staticmethod def strip_custom_tool_call_namespace_from_responses_input( input: str | ResponseInputParam, diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py index 49a332e62bb..b60c18b951e 100644 --- a/litellm/llms/custom_httpx/llm_http_handler.py +++ b/litellm/llms/custom_httpx/llm_http_handler.py @@ -2688,9 +2688,10 @@ class BaseLLMHTTPHandler: litellm_params=dict(litellm_params), ) + canonical_input: Final = BaseResponsesAPIConfig.normalize_responses_input(input) data = responses_api_provider_config.transform_responses_api_request( model=model, - input=input, + input=canonical_input, response_api_optional_request_params=response_api_optional_request_params, litellm_params=litellm_params, headers=headers, @@ -2876,9 +2877,10 @@ class BaseLLMHTTPHandler: litellm_params=dict(litellm_params), ) + canonical_input: Final = BaseResponsesAPIConfig.normalize_responses_input(input) data = responses_api_provider_config.transform_responses_api_request( model=model, - input=input, + input=canonical_input, response_api_optional_request_params=response_api_optional_request_params, litellm_params=litellm_params, headers=headers, diff --git a/tests/llm_responses_api_testing/test_openai_responses_api.py b/tests/llm_responses_api_testing/test_openai_responses_api.py index c7712d96969..a89ad2d90d9 100644 --- a/tests/llm_responses_api_testing/test_openai_responses_api.py +++ b/tests/llm_responses_api_testing/test_openai_responses_api.py @@ -1204,8 +1204,9 @@ def test_basic_computer_use_preview_tool_call(): assert request_body["truncation"] == "auto" # Validate the input format - assert isinstance(request_body["input"], str) - assert request_body["input"] == "Check the latest OpenAI news on bing.com." + assert request_body["input"] == [ + {"role": "user", "content": "Check the latest OpenAI news on bing.com."} + ] def test_mcp_tools_with_responses_api(): @@ -1500,7 +1501,9 @@ async def test_aresponses_service_tier_and_safety_identifier(): request_body["safety_identifier"] == "123" ), "safety_identifier should be '123' in request body" assert request_body["model"] == "gpt-5.5" - assert request_body["input"] == "Test with service tier and safety identifier" + assert request_body["input"] == [ + {"role": "user", "content": "Test with service tier and safety identifier"} + ] # Validate the response print("Response:", json.dumps(response, indent=4, default=str)) @@ -1596,7 +1599,9 @@ async def test_openai_gpt5_reasoning_effort_parameter(): request_body["reasoning"]["effort"] == "minimal" ), "reasoning_effort should be 'minimal' in request body" assert request_body["model"] == "gpt-5-mini" - assert request_body["input"] == "What is the capital of France?" + assert request_body["input"] == [ + {"role": "user", "content": "What is the capital of France?"} + ] # Validate the response print("Response:", json.dumps(response, indent=4, default=str)) @@ -1774,7 +1779,7 @@ async def test_aresponses_extra_body_params_passed(extra_body_mock_response_data assert "experimental_feature" in request_body assert request_body["experimental_feature"] is True assert request_body["model"] == "gpt-5.5" - assert request_body["input"] == "Test input" + assert request_body["input"] == [{"role": "user", "content": "Test input"}] def test_responses_extra_body_params_passed_sync(extra_body_mock_response_data): diff --git a/tests/test_litellm/expected_responses_api_request/azure_shell_tool.json b/tests/test_litellm/expected_responses_api_request/azure_shell_tool.json index b716c518106..48aaa842964 100644 --- a/tests/test_litellm/expected_responses_api_request/azure_shell_tool.json +++ b/tests/test_litellm/expected_responses_api_request/azure_shell_tool.json @@ -1,6 +1,11 @@ { "model": "gpt-5-mini", - "input": "List files in /mnt/data and run python --version.", + "input": [ + { + "role": "user", + "content": "List files in /mnt/data and run python --version." + } + ], "tools": [ { "type": "shell", diff --git a/tests/test_litellm/expected_responses_api_request/context_management_and_shell.json b/tests/test_litellm/expected_responses_api_request/context_management_and_shell.json index 1e34b230182..7dc5565bc1e 100644 --- a/tests/test_litellm/expected_responses_api_request/context_management_and_shell.json +++ b/tests/test_litellm/expected_responses_api_request/context_management_and_shell.json @@ -1,6 +1,11 @@ { "model": "gpt-4o", - "input": "List files in /mnt/data and run python --version.", + "input": [ + { + "role": "user", + "content": "List files in /mnt/data and run python --version." + } + ], "context_management": [ { "type": "compaction", diff --git a/tests/test_litellm/llms/azure_ai/responses/test_azure_ai_responses_transformation.py b/tests/test_litellm/llms/azure_ai/responses/test_azure_ai_responses_transformation.py index bae956eb061..8634093b9dc 100644 --- a/tests/test_litellm/llms/azure_ai/responses/test_azure_ai_responses_transformation.py +++ b/tests/test_litellm/llms/azure_ai/responses/test_azure_ai_responses_transformation.py @@ -227,7 +227,7 @@ def _assert_native_responses_request(route, expected_url, expected_model): assert f"{request.url.scheme}://{request.url.host}{request.url.path}" == expected_url assert request.headers["api-key"] == "fake-key" assert body["model"] == expected_model - assert body["input"] == "What is the weather in SF?" + assert body["input"] == [{"role": "user", "content": "What is the weather in SF?"}] assert "messages" not in body assert body["reasoning"] == {"effort": "high"} assert body["tools"] == [WEATHER_TOOL] diff --git a/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py b/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py index 9bf3eec61f9..3b930788104 100644 --- a/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py +++ b/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py @@ -11,6 +11,7 @@ import httpx import pytest import litellm +from litellm.llms.base_llm.responses.transformation import BaseResponsesAPIConfig from litellm.llms.chatgpt.responses.transformation import ChatGPTResponsesAPIConfig from litellm.llms.openai.common_utils import OpenAIError from litellm.main import responses_api_bridge_check @@ -114,6 +115,22 @@ class TestChatGPTResponsesAPITransformation: assert headers["accept"] == "text/event-stream" assert headers["session_id"] == "session-123" + def test_chatgpt_string_input_is_list_after_central_normalize(self): + config = ChatGPTResponsesAPIConfig() + request = config.transform_responses_api_request( + model="chatgpt/gpt-5.3-codex", + input=BaseResponsesAPIConfig.normalize_responses_input( + "Reply with exactly ROUTE_OK." + ), + response_api_optional_request_params={}, + litellm_params=GenericLiteLLMParams(), + headers={}, + ) + + assert request["input"] == [ + {"role": "user", "content": "Reply with exactly ROUTE_OK."} + ] + @pytest.mark.parametrize( "model_name", [ diff --git a/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py b/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py index 420adc9338e..7363346585c 100644 --- a/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py +++ b/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py @@ -259,6 +259,147 @@ def test_prepare_fake_stream_request(): assert result_data["messages"] == [{"role": "user", "content": "Hello"}] +def test_response_api_handler_passes_string_input_to_transform_as_user_list(): + handler = BaseLLMHTTPHandler() + config = Mock() + config.validate_environment.return_value = {} + config.get_complete_url.return_value = "https://chatgpt.example.com/responses" + config.sign_request.return_value = ({}, None) + config.transform_response_api_response.return_value = ResponsesAPIResponse( + id="resp_1", + created_at=0, + output=[], + status="completed", + model="gpt-5.3-codex", + ) + + def transform_responses_api_request(**kwargs): + return { + "model": kwargs["model"], + "input": kwargs["input"], + } + + config.transform_responses_api_request.side_effect = transform_responses_api_request + client = HTTPHandler(client=httpx.Client()) + client.post = Mock( + return_value=httpx.Response( + 200, + request=httpx.Request("POST", "https://chatgpt.example.com/responses"), + ) + ) + + handler.response_api_handler( + model="gpt-5.3-codex", + input="Reply with exactly ROUTE_OK.", + responses_api_provider_config=config, + response_api_optional_request_params={}, + custom_llm_provider="chatgpt", + litellm_params=GenericLiteLLMParams(), + logging_obj=Mock(), + client=client, + ) + + assert config.transform_responses_api_request.call_args.kwargs["input"] == [ + {"role": "user", "content": "Reply with exactly ROUTE_OK."} + ] + assert client.post.call_args.kwargs["json"]["input"] == [ + {"role": "user", "content": "Reply with exactly ROUTE_OK."} + ] + + +@pytest.mark.asyncio +async def test_async_response_api_handler_passes_string_input_to_transform_as_user_list(): + handler = BaseLLMHTTPHandler() + config = Mock() + config.validate_environment.return_value = {} + config.get_complete_url.return_value = "https://chatgpt.example.com/responses" + config.sign_request.return_value = ({}, None) + config.should_fake_stream.return_value = False + config.transform_response_api_response.return_value = ResponsesAPIResponse( + id="resp_1", + created_at=0, + output=[], + status="completed", + model="gpt-5.3-codex", + ) + + def transform_responses_api_request(**kwargs): + return { + "model": kwargs["model"], + "input": kwargs["input"], + } + + config.transform_responses_api_request.side_effect = transform_responses_api_request + client = AsyncHTTPHandler() + client.post = AsyncMock( + return_value=httpx.Response( + 200, + request=httpx.Request("POST", "https://chatgpt.example.com/responses"), + ) + ) + logging_obj = Mock() + logging_obj.dynamic_success_callbacks = [] + + await handler.async_response_api_handler( + model="gpt-5.3-codex", + input="Reply with exactly ROUTE_OK.", + responses_api_provider_config=config, + response_api_optional_request_params={}, + custom_llm_provider="chatgpt", + litellm_params=GenericLiteLLMParams(), + logging_obj=logging_obj, + client=client, + ) + + assert config.transform_responses_api_request.call_args.kwargs["input"] == [ + {"role": "user", "content": "Reply with exactly ROUTE_OK."} + ] + assert client.post.call_args.kwargs["json"]["input"] == [ + {"role": "user", "content": "Reply with exactly ROUTE_OK."} + ] + + +def test_response_api_handler_leaves_list_input_untouched(): + handler = BaseLLMHTTPHandler() + config = Mock() + config.validate_environment.return_value = {} + config.get_complete_url.return_value = "https://chatgpt.example.com/responses" + config.sign_request.return_value = ({}, None) + config.transform_response_api_response.return_value = ResponsesAPIResponse( + id="resp_1", + created_at=0, + output=[], + status="completed", + model="gpt-5.3-codex", + ) + list_input = [{"role": "user", "content": "Reply with exactly ROUTE_OK."}] + + def transform_responses_api_request(**kwargs): + return {"model": kwargs["model"], "input": kwargs["input"]} + + config.transform_responses_api_request.side_effect = transform_responses_api_request + client = HTTPHandler(client=httpx.Client()) + client.post = Mock( + return_value=httpx.Response( + 200, + request=httpx.Request("POST", "https://chatgpt.example.com/responses"), + ) + ) + + handler.response_api_handler( + model="gpt-5.3-codex", + input=list_input, + responses_api_provider_config=config, + response_api_optional_request_params={}, + custom_llm_provider="chatgpt", + litellm_params=GenericLiteLLMParams(), + logging_obj=Mock(), + client=client, + ) + + assert config.transform_responses_api_request.call_args.kwargs["input"] is list_input + + def test_response_api_handler_streams_when_provider_transform_adds_stream(): handler = BaseLLMHTTPHandler() config = Mock() diff --git a/tests/test_litellm/llms/fireworks_ai/responses/test_fireworks_ai_responses_transformation.py b/tests/test_litellm/llms/fireworks_ai/responses/test_fireworks_ai_responses_transformation.py index d0697ca9b0e..aa704d2db5d 100644 --- a/tests/test_litellm/llms/fireworks_ai/responses/test_fireworks_ai_responses_transformation.py +++ b/tests/test_litellm/llms/fireworks_ai/responses/test_fireworks_ai_responses_transformation.py @@ -321,7 +321,7 @@ def test_responses_call_keeps_a_developer_item_with_non_text_parts_in_place_as_a ) -def test_responses_call_forwards_string_input_and_instructions_unchanged() -> None: +def test_responses_call_normalizes_string_input_and_forwards_instructions() -> None: client: Final = _mock_http_client(_fireworks_response("accounts/fireworks/models/kimi-k3")) with patch(HTTPX_CLIENT_FACTORY, return_value=client): litellm.responses( @@ -332,7 +332,7 @@ def test_responses_call_forwards_string_input_and_instructions_unchanged() -> No ) _, _, body = _sent_request(client) assert body["instructions"] == "Answer with exactly one word." - assert body["input"] == "What is the capital of France?" + assert body["input"] == [{"role": "user", "content": "What is the capital of France?"}] def test_transform_request_forwards_non_string_instructions_and_input_untouched() -> None: diff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py index 0ef45501d91..b0916e4fb02 100644 --- a/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py +++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py @@ -885,6 +885,15 @@ class TestOpenAIResponsesAPIConfig: assert result.output_index == 0 assert result.content_index == 0 + def test_normalize_responses_input_wraps_string_as_user_item(self): + assert BaseResponsesAPIConfig.normalize_responses_input("Reply with exactly ROUTE_OK.") == [ + {"role": "user", "content": "Reply with exactly ROUTE_OK."} + ] + + def test_normalize_responses_input_leaves_list_untouched(self): + items = [{"role": "user", "content": "Reply with exactly ROUTE_OK."}] + assert BaseResponsesAPIConfig.normalize_responses_input(items) is items + def test_base_strip_custom_tool_call_namespace_all_providers(self): """Base helper strips ``namespace`` from custom_tool_call for every provider path.""" inp = [ diff --git a/tests/test_litellm/responses/test_responses_api_request_body.py b/tests/test_litellm/responses/test_responses_api_request_body.py index 6b5aab932ec..e67708b70d3 100644 --- a/tests/test_litellm/responses/test_responses_api_request_body.py +++ b/tests/test_litellm/responses/test_responses_api_request_body.py @@ -219,6 +219,7 @@ async def test_aresponses_drops_stream_options(): post_kwargs = mock_post.call_args.kwargs request_body = post_kwargs["json"] if "json" in post_kwargs else json.loads(post_kwargs["data"]) assert "stream_options" not in request_body + assert request_body["input"] == [{"role": "user", "content": "hi"}] @pytest.mark.asyncio diff --git a/tests/test_litellm/responses/test_responses_supported_endpoints_passthrough.py b/tests/test_litellm/responses/test_responses_supported_endpoints_passthrough.py index 4ff4963423e..bb917b59f09 100644 --- a/tests/test_litellm/responses/test_responses_supported_endpoints_passthrough.py +++ b/tests/test_litellm/responses/test_responses_supported_endpoints_passthrough.py @@ -141,7 +141,7 @@ async def test_opt_in_forwards_responses_natively(): assert chat_route.call_count == 0 request = responses_route.calls.last.request assert request.headers["authorization"] == "Bearer sk-backend" - assert json.loads(request.content)["input"] == "hi" + assert json.loads(request.content)["input"] == [{"role": "user", "content": "hi"}] assert isinstance(result, ResponsesAPIResponse) assert result.output[0].content[0].text == "native"