diff --git a/litellm/responses/litellm_completion_transformation/streaming_iterator.py b/litellm/responses/litellm_completion_transformation/streaming_iterator.py index ad6cf09aaae..f71ff3c6660 100644 --- a/litellm/responses/litellm_completion_transformation/streaming_iterator.py +++ b/litellm/responses/litellm_completion_transformation/streaming_iterator.py @@ -91,8 +91,10 @@ class LiteLLMCompletionStreamingIterator(ResponsesAPIStreamingIterator): self.custom_llm_provider: str | None = custom_llm_provider self.litellm_metadata: dict | None = litellm_metadata or {} _wrapper_hidden_params: Final = getattr(litellm_custom_stream_wrapper, "_hidden_params", None) - self._hidden_params: dict[str, object] = ( - dict(_wrapper_hidden_params) if isinstance(_wrapper_hidden_params, dict) else {} + self._hidden_params: dict[str, object] = ( # mutable-ok: downstream response metadata requires a mutable dict + dict(_wrapper_hidden_params) # mutable-ok: downstream response metadata requires a mutable dict + if isinstance(_wrapper_hidden_params, dict) + else {} # mutable-ok: downstream response metadata requires a mutable dict ) # Store lightweight dict snapshots for stream_chunk_builder to reduce # repeated Pydantic attribute access in end-of-stream assembly. diff --git a/tests/router_unit_tests/test_router_aresponses_streaming_fallback.py b/tests/router_unit_tests/test_router_aresponses_streaming_fallback.py index 08112c2c04c..a538db85146 100644 --- a/tests/router_unit_tests/test_router_aresponses_streaming_fallback.py +++ b/tests/router_unit_tests/test_router_aresponses_streaming_fallback.py @@ -533,6 +533,10 @@ def test_extract_partial_responses_usage_real_bridge_iterator_pre_first_chunk(): self.logging_obj = MagicMock() self._hidden_params = {"model_id": "deployment-123"} + class _FakeStreamWrapperWithoutHiddenParams: + def __init__(self) -> None: + self.logging_obj = MagicMock() + iterator = LiteLLMCompletionStreamingIterator( model="anthropic/claude-3-5-sonnet-latest", litellm_custom_stream_wrapper=_FakeStreamWrapper(), @@ -545,4 +549,12 @@ def test_extract_partial_responses_usage_real_bridge_iterator_pre_first_chunk(): assert iterator._hidden_params == {"model_id": "deployment-123"} assert not iterator.collected_chat_completion_chunks + iterator_without_hidden_params = LiteLLMCompletionStreamingIterator( + model="anthropic/claude-3-5-sonnet-latest", + litellm_custom_stream_wrapper=_FakeStreamWrapperWithoutHiddenParams(), + request_input="hi", + responses_api_request={}, + ) + + assert iterator_without_hidden_params._hidden_params == {} assert Router._extract_partial_responses_usage(iterator) is None