Skip to content

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]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_headroom_ccr_streaming_responses

mateo-berrie12e5e34f444
diff --cc tests/test_litellm/litellm_core_utils/test_streaming_handler.py
index 7caf401ce38,4c99bce2b0f..b3d371800ab
--- a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py
+++ b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py
@@@ -4694,38 -4694,80 +4694,117 @@@ async def test_async_stream_assembled_r
assert assembled._hidden_params["provider_specific_fields"]["traffic_type"] == "ON_DEMAND_FLEX"
+async def test_async_fake_stream_final_chunk_carries_hidden_usage(logging_obj: Logging):
+ from litellm.llms.base_llm.base_model_iterator import MockResponseIterator
+ from litellm.types.utils import ModelResponse
+
+ model_response = ModelResponse(
+ id="chatcmpl-fake-stream",
+ model="my-random-model",
+ choices=[
+ {
+ "index": 0,
+ "message": {"role": "assistant", "content": "hello world"},
+ "finish_reason": "stop",
+ }
+ ],
+ )
+ model_response.usage = Usage(prompt_tokens=1234, completion_tokens=7, total_tokens=1241)
+
+ wrapper = CustomStreamWrapper(
+ completion_stream=MockResponseIterator(model_response=model_response),
+ model="my-random-model",
+ custom_llm_provider="anthropic",
+ logging_obj=logging_obj,
+ )
+
+ final_chunk = None
+ async for chunk in wrapper:
+ final_chunk = chunk
+
+ assert final_chunk is not None
+ hidden_usage = final_chunk._hidden_params.get("usage")
+ assert hidden_usage is not None
+ assert hidden_usage.prompt_tokens == 1234
+ assert hidden_usage.completion_tokens == 7
+ assert hidden_usage.total_tokens == 1241
++
++
+ 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"