The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(streaming): keep response id stable across streamed chunks
Timik23202bb3f9310e6
diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.pyindex f6340426c1b..fb693943b2b 100644--- a/litellm/litellm_core_utils/streaming_handler.py+++ b/litellm/litellm_core_utils/streaming_handler.py@@ -816,6 +816,8 @@ class CustomStreamWrapper:model_response: Final = ModelResponseStream(**args)if self.response_id is not None:model_response.id = self.response_id+ elif model_response.id:+ self.response_id = model_response.idif self.system_fingerprint is not None:model_response.system_fingerprint = self.system_fingerprintdiff --git a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py b/tests/test_litellm/litellm_core_utils/test_streaming_handler.pyindex b5e33a4e421..a76d427495c 100644--- a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py+++ b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py@@ -4460,3 +4460,82 @@ def test_handle_stream_fallback_error_restores_context_only_after_exception_mappfinally:trace_id_var.set("")session_id_var.set("")+++class TestStableStreamingResponseId:+ """+ All chunks of one streamed response must share the same top-level id+ (OpenAI streaming contract). Providers streaming via GenericStreamingChunk+ (e.g. GigaChat) do not propagate an upstream response id, so+ CustomStreamWrapper must pin the id from the first chunk it creates,+ mirroring the existing `created` pinning (issue #11437).++ Clients such as goose merge streamed deltas into one assistant message by+ chunk id; per-chunk ids split a single reply into many messages.+ """++ def test_generic_chunks_share_one_id(self):+ def _generic_chunks():+ return iter(+ [+ {+ "text": "Hello",+ "tool_use": None,+ "is_finished": False,+ "finish_reason": "",+ "usage": None,+ "index": 0,+ },+ {+ "text": " world",+ "tool_use": None,+ "is_finished": False,+ "finish_reason": "",+ "usage": None,+ "index": 0,+ },+ {+ "text": "",+ "tool_use": None,+ "is_finished": True,+ "finish_reason": "stop",+ "usage": {+ "prompt_tokens": 1,+ "completion_tokens": 2,+ "total_tokens": 3,+ },+ "index": 0,+ },+ ]+ )++ wrapper = CustomStreamWrapper(+ completion_stream=_generic_chunks(),+ model="gigachat/GigaChat-2-Max",+ logging_obj=MagicMock(),+ custom_llm_provider="gigachat",+ )+ ids = [chunk.id for chunk in wrapper if chunk.id]+ assert ids, "no chunks emitted"+ assert len(set(ids)) == 1, f"chunk ids differ across one stream: {ids}"++ def test_creator_pins_id_from_first_chunk(self):+ wrapper = CustomStreamWrapper(+ completion_stream=iter([]),+ model="gigachat/GigaChat-2-Max",+ logging_obj=MagicMock(),+ custom_llm_provider="gigachat",+ )+ first = wrapper.model_response_creator()+ assert wrapper.response_id == first.id+ assert wrapper.model_response_creator().id == first.id++ def test_provider_supplied_id_still_wins(self):+ wrapper = CustomStreamWrapper(+ completion_stream=iter([]),+ model="gigachat/GigaChat-2-Max",+ logging_obj=MagicMock(),+ custom_llm_provider="gigachat",+ )+ wrapper.response_id = "chatcmpl-from-provider"+ assert wrapper.model_response_creator().id == "chatcmpl-from-provider"