From 7f451939a8dc8be8597a635055eec76f67384d2e Mon Sep 17 00:00:00 2001 From: yucheng Date: Sat, 19 Sep 2026 08:01:33 +0000 Subject: [PATCH 1/3] fix(proxy): return 400 instead of 500 for /v1/responses without input Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/proxy/route_llm_request.py | 1 + .../proxy/test_route_llm_request.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/litellm/proxy/route_llm_request.py b/litellm/proxy/route_llm_request.py index 20b4708c193..536c58df65a 100644 --- a/litellm/proxy/route_llm_request.py +++ b/litellm/proxy/route_llm_request.py @@ -159,6 +159,7 @@ class ProxyModelNotFoundError(HTTPException): REQUIRED_BODY_PARAMS_BY_ROUTE: Final[Mapping[str, tuple[str, ...]]] = { "acompletion": ("messages",), "aembedding": ("input",), + "aresponses": ("input",), "acreate_batch": ("input_file_id", "endpoint", "completion_window"), } diff --git a/tests/test_litellm/proxy/test_route_llm_request.py b/tests/test_litellm/proxy/test_route_llm_request.py index f7021763a4d..6cbbc279748 100644 --- a/tests/test_litellm/proxy/test_route_llm_request.py +++ b/tests/test_litellm/proxy/test_route_llm_request.py @@ -1043,6 +1043,7 @@ async def test_route_request_override_enable_tag_filtering_beats_body_value(): [ ("acompletion", "messages", "/chat/completions"), ("aembedding", "input", "/embeddings"), + ("aresponses", "input", "/responses"), ("acreate_batch", "input_file_id", "/batches"), ], ) @@ -1090,6 +1091,8 @@ def test_raise_if_required_body_param_missing_names_first_missing_batch_param(da ("acompletion", {"model": "gpt-4o", "messages": []}), ("atext_completion", {"model": "gpt-4o"}), ("aembedding", {"model": "text-embedding-3-small", "input": "hi"}), + ("aresponses", {"model": "gpt-4o", "input": "hi"}), + ("aresponses", {"model": "gpt-4o", "input": []}), ("arerank", {"model": "rerank-model"}), ("aimage_generation", {"model": "dall-e-3"}), ( @@ -1120,6 +1123,20 @@ async def test_route_request_rejects_chat_completion_without_messages(): llm_router.acompletion.assert_not_called() +@pytest.mark.asyncio +async def test_route_request_rejects_responses_without_input(): + from litellm.proxy.route_llm_request import ProxyMissingRequiredParamError + + llm_router = MagicMock() + + with pytest.raises(ProxyMissingRequiredParamError) as exc_info: + await route_request({"model": "gpt-4o"}, llm_router, None, "aresponses") + + assert exc_info.value.code == "400" + assert exc_info.value.param == "input" + llm_router.aresponses.assert_not_called() + + class FakeProxyModelTable: def __init__(self, rows): self.rows = rows From 0fc6e7fd0845996349a1a47c48147e4a0a5c2059 Mon Sep 17 00:00:00 2001 From: yucheng Date: Sat, 19 Sep 2026 08:46:11 +0000 Subject: [PATCH 2/3] fix(proxy): validate input before starting background responses polling Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../proxy/response_api_endpoints/endpoints.py | 2 + .../response_api_endpoints/test_endpoints.py | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/litellm/proxy/response_api_endpoints/endpoints.py b/litellm/proxy/response_api_endpoints/endpoints.py index 3b6afc34063..a680e445a2c 100644 --- a/litellm/proxy/response_api_endpoints/endpoints.py +++ b/litellm/proxy/response_api_endpoints/endpoints.py @@ -34,6 +34,7 @@ from litellm.proxy.common_utils.http_parsing_utils import ( _read_request_body, _safe_set_request_parsed_body, ) +from litellm.proxy.route_llm_request import raise_if_required_body_param_missing from litellm.types.llms.openai import ( REASONING_EFFORT, ResponsesAPIOptionalRequestParams, @@ -280,6 +281,7 @@ async def responses_api( # instead of a polling ID that immediately fails in the background task. processor = ProxyBaseLLMRequestProcessing(data=data) try: + raise_if_required_body_param_missing(route_type="aresponses", data=data) data, _logging_obj = await processor.common_processing_pre_call_logic( request=request, general_settings=general_settings, diff --git a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py index f53fbde6b51..751d4753608 100644 --- a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py +++ b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py @@ -121,6 +121,71 @@ async def test_streaming_upstream_errors_keep_the_client_protocol( assert "error" in events[-1] +@pytest.mark.asyncio +async def test_responses_api_background_polling_rejects_missing_input(): + from fastapi import Response as FastAPIResponse + from starlette.requests import Request + + from litellm.proxy._types import ProxyException, UserAPIKeyAuth + from litellm.proxy.response_api_endpoints.endpoints import responses_api + + processor = MagicMock() + + async def return_exception(*, e: Exception, **kwargs: object) -> Exception: + return e + + processor._handle_llm_api_exception = AsyncMock(side_effect=return_exception) + processor.common_processing_pre_call_logic = AsyncMock(return_value=({"model": "gpt-4o"}, MagicMock())) + + async def receive(): + return { + "type": "http.request", + "body": b'{"model":"gpt-4o","background":true}', + "more_body": False, + } + + request = Request( + { + "type": "http", + "method": "POST", + "path": "/v1/responses", + "headers": [(b"content-type", b"application/json")], + }, + receive, + ) + + with ( + patch( # test-quality-ok: endpoint constructs the processor directly + "litellm.proxy.response_api_endpoints.endpoints.ProxyBaseLLMRequestProcessing", + return_value=processor, + ), + patch( # test-quality-ok: polling decision is imported inside the endpoint + "litellm.proxy.response_polling.polling_handler.should_use_polling_for_request", + return_value=True, + ), + patch( # test-quality-ok: background task is imported inside the endpoint + "litellm.proxy.response_polling.background_streaming.background_streaming_task", + new_callable=AsyncMock, + ) as mock_background_streaming_task, + patch( # test-quality-ok: polling handler is imported inside the endpoint + "litellm.proxy.response_polling.polling_handler.ResponsePollingHandler.create_initial_state", + new_callable=AsyncMock, + ) as mock_create_initial_state, + ): + with pytest.raises(ProxyException) as exc_info: + await responses_api( + request=request, + fastapi_response=FastAPIResponse(), + user_api_key_dict=UserAPIKeyAuth(api_key="sk-test"), + ) + + assert exc_info.value.code == "400" + assert exc_info.value.param == "input" + processor.common_processing_pre_call_logic.assert_not_awaited() + mock_background_streaming_task.assert_not_called() + mock_create_initial_state.assert_not_awaited() + + class TestResponsesAPIEndpoints(unittest.TestCase): @pytest.mark.asyncio @patch("litellm.proxy.proxy_server.llm_router") From 13b05af06e20b84158d41511efee690868dce288 Mon Sep 17 00:00:00 2001 From: yucheng Date: Sat, 19 Sep 2026 16:34:53 +0000 Subject: [PATCH 3/3] fix(proxy): validate responses input after prompt template expansion Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../proxy/response_api_endpoints/endpoints.py | 2 +- .../response_api_endpoints/test_endpoints.py | 68 ++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/response_api_endpoints/endpoints.py b/litellm/proxy/response_api_endpoints/endpoints.py index a680e445a2c..73ab7e5213f 100644 --- a/litellm/proxy/response_api_endpoints/endpoints.py +++ b/litellm/proxy/response_api_endpoints/endpoints.py @@ -281,7 +281,6 @@ async def responses_api( # instead of a polling ID that immediately fails in the background task. processor = ProxyBaseLLMRequestProcessing(data=data) try: - raise_if_required_body_param_missing(route_type="aresponses", data=data) data, _logging_obj = await processor.common_processing_pre_call_logic( request=request, general_settings=general_settings, @@ -298,6 +297,7 @@ async def responses_api( route_type="aresponses", llm_router=llm_router, ) + raise_if_required_body_param_missing(route_type="aresponses", data=data) except Exception as e: raise await processor._handle_llm_api_exception( e=e, diff --git a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py index 751d4753608..f7abb209015 100644 --- a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py +++ b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py @@ -181,11 +181,77 @@ async def test_responses_api_background_polling_rejects_missing_input(): assert exc_info.value.code == "400" assert exc_info.value.param == "input" - processor.common_processing_pre_call_logic.assert_not_awaited() + processor.common_processing_pre_call_logic.assert_awaited_once() mock_background_streaming_task.assert_not_called() mock_create_initial_state.assert_not_awaited() +@pytest.mark.asyncio +async def test_responses_api_background_polling_accepts_input_from_prompt_template(): + from fastapi import Response as FastAPIResponse + from starlette.requests import Request + + from litellm.proxy._types import UserAPIKeyAuth + from litellm.proxy.response_api_endpoints.endpoints import responses_api + + processor = MagicMock() + processor.common_processing_pre_call_logic = AsyncMock( + return_value=({"model": "gpt-4o", "input": "hello from prompt"}, MagicMock()) + ) + initial_state = MagicMock() + + async def receive(): + return { + "type": "http.request", + "body": b'{"model":"gpt-4o","prompt_id":"greeting","background":true}', + "more_body": False, + } + + request = Request( + { + "type": "http", + "method": "POST", + "path": "/v1/responses", + "headers": [(b"content-type", b"application/json")], + }, + receive, + ) + + with ( + patch( # test-quality-ok: endpoint constructs the processor directly + "litellm.proxy.response_api_endpoints.endpoints.ProxyBaseLLMRequestProcessing", + return_value=processor, + ), + patch( # test-quality-ok: polling decision is imported inside the endpoint + "litellm.proxy.response_polling.polling_handler.should_use_polling_for_request", + return_value=True, + ), + patch( # test-quality-ok: background task is imported inside the endpoint + "litellm.proxy.response_polling.background_streaming.background_streaming_task", + new_callable=AsyncMock, + ), + patch( # test-quality-ok: avoid scheduling a background task in this unit test + "litellm.proxy.response_api_endpoints.endpoints.asyncio.create_task", + ), + patch( # test-quality-ok: polling handler is imported inside the endpoint + "litellm.proxy.response_polling.polling_handler.ResponsePollingHandler.create_initial_state", + new_callable=AsyncMock, + ) as mock_create_initial_state, + ): + mock_create_initial_state.return_value = initial_state + result = await responses_api( + request=request, + fastapi_response=FastAPIResponse(), + user_api_key_dict=UserAPIKeyAuth(api_key="sk-test"), + ) + + assert result is initial_state + processor.common_processing_pre_call_logic.assert_awaited_once() + mock_create_initial_state.assert_awaited_once() + request_data = mock_create_initial_state.await_args.kwargs["request_data"] + assert request_data["input"] == "hello from prompt" + + class TestResponsesAPIEndpoints(unittest.TestCase): @pytest.mark.asyncio @patch("litellm.proxy.proxy_server.llm_router")