_get_model_from_chunks

This commit is contained in:
Ishaan Jaffer 2026-01-13 17:50:15 -08:00
parent 1193279bc8
commit a1667f2190
2 changed files with 23 additions and 2 deletions

View file

@ -17,8 +17,8 @@ from litellm.types.utils import (
ModelResponse,
ModelResponseStream,
PromptTokensDetailsWrapper,
ServerToolUse,
Usage,
ServerToolUse
)
from litellm.utils import print_verbose, token_counter
@ -68,12 +68,31 @@ class ChunkProcessor:
return chunk["id"]
return ""
@staticmethod
def _get_model_from_chunks(chunks: List[Dict[str, Any]], first_chunk_model: str) -> str:
"""
Get the actual model from chunks, preferring a model that differs from the first chunk.
For Azure Model Router, the first chunk may have the request model (e.g., 'azure-model-router')
while subsequent chunks have the actual model (e.g., 'gpt-4.1-nano-2025-04-14').
This method finds the actual model for accurate cost calculation.
"""
# Look for a model in chunks that differs from the first chunk's model
for chunk in chunks:
chunk_model = chunk.get("model")
if chunk_model and chunk_model != first_chunk_model:
return chunk_model
# Fall back to first chunk's model if no different model found
return first_chunk_model
def build_base_response(self, chunks: List[Dict[str, Any]]) -> ModelResponse:
chunk = self.first_chunk
id = ChunkProcessor._get_chunk_id(chunks)
object = chunk["object"]
created = chunk["created"]
model = chunk["model"]
first_chunk_model = chunk["model"]
# Get the actual model - for Azure Model Router, this finds the real model from later chunks
model = ChunkProcessor._get_model_from_chunks(chunks, first_chunk_model)
system_fingerprint = chunk.get("system_fingerprint", None)
role = chunk["choices"][0]["delta"]["role"]

View file

@ -6,3 +6,5 @@ model_list:
litellm_params:
model: openai/*
general_settings:
store_prompts_in_spend_logs: true