From fd36719f47f0d265a15f0e2ad2690e81c9a588a1 Mon Sep 17 00:00:00 2001 From: Ritik Yadav Date: Wed, 16 Sep 2026 18:27:33 +0530 Subject: [PATCH 1/2] fix(proxy): handle ModelResponse in ResponsesIDSecurity streaming iterator hook When websearch_interception executes during a streaming request, the agentic-loop returns a non-streaming ModelResponse. Convert it to a streaming chunk using convert_model_response_to_streaming so the SSE generator yields chunks cleanly without raising TypeError. Fixes #41411 --- litellm/proxy/hooks/responses_id_security.py | 11 +++- .../test_responses_id_security.py | 50 ++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/hooks/responses_id_security.py b/litellm/proxy/hooks/responses_id_security.py index 7e7f70d6f7e..bd5c75b8040 100644 --- a/litellm/proxy/hooks/responses_id_security.py +++ b/litellm/proxy/hooks/responses_id_security.py @@ -13,6 +13,9 @@ from pydantic import TypeAdapter, ValidationError from litellm._logging import verbose_proxy_logger from litellm.integrations.custom_logger import CustomLogger +from litellm.llms.base_llm.base_model_iterator import ( + convert_model_response_to_streaming, +) from litellm.proxy._types import LitellmUserRoles from litellm.proxy.common_utils.encrypt_decrypt_utils import ( decrypt_value_helper, @@ -22,7 +25,7 @@ from litellm.types.llms.openai import ( BaseLiteLLMOpenAIResponseObject, ResponsesAPIResponse, ) -from litellm.types.utils import CallTypesLiteral, LLMResponseTypes, SpecialEnums +from litellm.types.utils import CallTypesLiteral, LLMResponseTypes, ModelResponse, SpecialEnums if TYPE_CHECKING: from litellm.caching.caching import DualCache @@ -328,12 +331,16 @@ class ResponsesIDSecurity(CustomLogger): async def async_post_call_streaming_iterator_hook( self, user_api_key_dict: "UserAPIKeyAuth", response: Any, request_data: dict - ) -> AsyncGenerator[BaseLiteLLMOpenAIResponseObject, None]: + ) -> AsyncGenerator[Any, None]: general_settings: Final = self._general_settings_reader() # Create a request-scoped cache for consistent encryption across streaming chunks. request_encryption_cache: Final[dict[str, str]] = {} + if isinstance(response, ModelResponse): + yield convert_model_response_to_streaming(response) + return + async for chunk in response: if ( isinstance(chunk, BaseLiteLLMOpenAIResponseObject) diff --git a/tests/test_litellm/test_responses_id_security.py b/tests/test_litellm/test_responses_id_security.py index a6081670172..69f236d1342 100644 --- a/tests/test_litellm/test_responses_id_security.py +++ b/tests/test_litellm/test_responses_id_security.py @@ -20,7 +20,13 @@ from litellm.types.llms.openai import ( ResponsesAPIResponse, ResponsesAPIStreamEvents, ) -from litellm.types.utils import SpecialEnums +from litellm.types.utils import ( + Choices, + Message, + ModelResponse, + ModelResponseStream, + SpecialEnums, +) @pytest.fixture @@ -692,6 +698,48 @@ class TestAsyncPostCallStreamingIteratorHook: assert streamed_id == "resp_rawprovider123" assert not responses_id_security._is_encrypted_response_id(streamed_id) + @pytest.mark.asyncio + async def test_model_response_converted_to_streaming_chunk( + self, responses_id_security, monkeypatch + ): + monkeypatch.setenv("LITELLM_SALT_KEY", "sk-test-salt-key-abcdefghij") + + mock_auth = MagicMock() + mock_auth.user_id = "user-a" + mock_auth.team_id = "team-a" + mock_auth.request_route = "/chat/completions" + + model_response = ModelResponse( + id="chatcmpl-test12345", + choices=[ + Choices( + finish_reason="stop", + index=0, + message=Message(content="Final search result", role="assistant"), + ) + ], + created=1677652288, + model="gpt-4o", + object="chat.completion", + ) + + collected = [ + out + async for out in responses_id_security.async_post_call_streaming_iterator_hook( + user_api_key_dict=mock_auth, + response=model_response, + request_data={}, + ) + ] + + assert len(collected) == 1 + chunk = collected[0] + assert isinstance(chunk, ModelResponseStream) + assert chunk.id == "chatcmpl-test12345" + assert chunk.object == "chat.completion.chunk" + assert chunk.choices[0].delta.content == "Final search result" + assert chunk.choices[0].finish_reason == "stop" + class TestStreamedGenericEventIdEncryption: """A background stream carries event types with no typed model, which arrive as From d85172250f8fd137c74a627709d648dd95943a8f Mon Sep 17 00:00:00 2001 From: Ritik Yadav Date: Fri, 18 Sep 2026 00:50:56 +0530 Subject: [PATCH 2/2] fix(proxy): strongly type streaming iterator hook return annotation --- litellm/proxy/hooks/responses_id_security.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/hooks/responses_id_security.py b/litellm/proxy/hooks/responses_id_security.py index bd5c75b8040..d8b93aee9d4 100644 --- a/litellm/proxy/hooks/responses_id_security.py +++ b/litellm/proxy/hooks/responses_id_security.py @@ -25,7 +25,13 @@ from litellm.types.llms.openai import ( BaseLiteLLMOpenAIResponseObject, ResponsesAPIResponse, ) -from litellm.types.utils import CallTypesLiteral, LLMResponseTypes, ModelResponse, SpecialEnums +from litellm.types.utils import ( + CallTypesLiteral, + LLMResponseTypes, + ModelResponse, + ModelResponseStream, + SpecialEnums, +) if TYPE_CHECKING: from litellm.caching.caching import DualCache @@ -331,7 +337,7 @@ class ResponsesIDSecurity(CustomLogger): async def async_post_call_streaming_iterator_hook( self, user_api_key_dict: "UserAPIKeyAuth", response: Any, request_data: dict - ) -> AsyncGenerator[Any, None]: + ) -> AsyncGenerator[BaseLiteLLMOpenAIResponseObject | ModelResponseStream, None]: general_settings: Final = self._general_settings_reader() # Create a request-scoped cache for consistent encryption across streaming chunks.