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
Keep the model Azure Model Router recovered from later chunks
mateo-berri03a253a1f987
diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.pyindex 72cc298d37d..7333111b6b4 100644--- a/litellm/proxy/common_request_processing.py+++ b/litellm/proxy/common_request_processing.py@@ -277,6 +277,26 @@ def _deferred_stream_logging_is_armed(request_data: dict) -> bool:)+def _assembled_model_came_from_a_later_chunk(chunks: list, assembled_model: object) -> bool:+ """Report whether stream_chunk_builder picked a model the first chunk did not carry.++ Azure Model Router puts the routed model on the chunks after the first one, and the+ proxy deliberately leaves those chunks unrestamped so the builder can recover it. The+ assembled model is then more specific than the wrapper's, so the caller has to leave+ it alone rather than stamping the wrapper's model over it.+ """+ first_chunk: Final = chunks[0]+ first_chunk_model: Final = (+ first_chunk.get("model") if isinstance(first_chunk, dict) else getattr(first_chunk, "model", None)+ )+ return (+ isinstance(first_chunk_model, str)+ and isinstance(assembled_model, str)+ and bool(assembled_model)+ and assembled_model != first_chunk_model+ )++async def _bill_partial_streamed_spend_on_disconnect(request_data: dict, response: object) -> bool:"""A client disconnect throws GeneratorExit/CancelledError into the streaming@@ -328,7 +348,11 @@ async def _bill_partial_streamed_spend_on_disconnect(request_data: dict, responsif partial_response is None:return Falsewrapper_model: Final = getattr(response, "model", None)- if isinstance(wrapper_model, str) and wrapper_model:+ if (+ isinstance(wrapper_model, str)+ and wrapper_model+ and not _assembled_model_came_from_a_later_chunk(chunks, partial_response.model)+ ):partial_response.model = wrapper_modelpartial_usage: Final = getattr(partial_response, "usage", None)if isinstance(partial_usage, Usage):diff --git a/tests/test_litellm/proxy/test_common_request_processing.py b/tests/test_litellm/proxy/test_common_request_processing.pyindex 4d78bf164b1..fd48d62a651 100644--- a/tests/test_litellm/proxy/test_common_request_processing.py+++ b/tests/test_litellm/proxy/test_common_request_processing.py@@ -5554,6 +5554,19 @@ class TestStreamingClientDisconnectBilling:standard_logging_object = event["kwargs"]["standard_logging_object"]assert standard_logging_object["response_cost"] > 0.0+ @pytest.mark.asyncio+ async def test_disconnect_billing_keeps_the_model_azure_model_router_picked(self):+ def restamp_like_azure_model_router(response):+ response.chunks[0].model = "azure-model-router"+ for chunk in response.chunks[1:]:+ chunk.model = "gpt-4.1-nano-2025-04-14"++ event = await self._bill_and_collect_success_event(restamp_like_azure_model_router)++ assert event["response_obj"].model == "gpt-4.1-nano-2025-04-14"+ standard_logging_object = event["kwargs"]["standard_logging_object"]+ assert standard_logging_object["response_cost"] > 0.0+@pytest.mark.asyncioasync def test_disconnect_billing_backfills_missing_cache_fields(self):event = await self._bill_and_collect_success_event()