From 4d677386b637b742275d4e4c4d5bb51459974c45 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Tue, 7 Apr 2026 18:13:03 +0530 Subject: [PATCH] fix(responses): gate response id update on parsed_chunk having response Delta stream events do not include a response body; Mock-based tests (and any truthy synthetic .response on transforms) must not trigger _update_responses_api_response_id_with_model_id. Fixes test_stop_async_iteration_not_logged_as_failure (TypeError: Mock not iterable). Made-with: Cursor --- litellm/responses/streaming_iterator.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py index 7d86c211ac0..2ecc95b7b32 100644 --- a/litellm/responses/streaming_iterator.py +++ b/litellm/responses/streaming_iterator.py @@ -130,15 +130,20 @@ class BaseResponsesAPIStreamingIterator: ) ) - # if "response" in parsed_chunk, then encode litellm specific information like custom_llm_provider - response_object = getattr(openai_responses_api_chunk, "response", None) - if response_object: - response = ResponsesAPIRequestUtils._update_responses_api_response_id_with_model_id( - responses_api_response=response_object, - litellm_metadata=self.litellm_metadata, - custom_llm_provider=self.custom_llm_provider, + # Only when the SSE JSON carries a response body (delta events do not). + # Using getattr(..., "response") alone is unsafe with Mocks: they synthesize a + # truthy child Mock for any attribute, which breaks tests and is wrong on stream. + if "response" in parsed_chunk: + response_object = getattr( + openai_responses_api_chunk, "response", None ) - setattr(openai_responses_api_chunk, "response", response) + if response_object is not None: + response = ResponsesAPIRequestUtils._update_responses_api_response_id_with_model_id( + responses_api_response=response_object, + litellm_metadata=self.litellm_metadata, + custom_llm_provider=self.custom_llm_provider, + ) + setattr(openai_responses_api_chunk, "response", response) # Encode container_id on streaming events so proxy/UI follow-ups route correctly _event_type = getattr(openai_responses_api_chunk, "type", None)