mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-06 08:16:43 +00:00
fix(streaming): price proxy-aliased models from the model map in stream_chunk_builder
This commit is contained in:
parent
349a653c29
commit
a17bc1f22c
2 changed files with 30 additions and 0 deletions
|
|
@ -8602,6 +8602,18 @@ def _stream_builder_response_cost(response: ModelResponse, logging_obj: Optional
|
|||
try:
|
||||
return litellm.completion_cost(completion_response=response, custom_llm_provider=provider_hint)
|
||||
except Exception:
|
||||
return _stream_builder_model_map_cost(response)
|
||||
|
||||
|
||||
def _stream_builder_model_map_cost(response: ModelResponse) -> float | None:
|
||||
model_name: Final = getattr(response, "model", None)
|
||||
usage: Final = getattr(response, "usage", None)
|
||||
if not isinstance(model_name, str) or not model_name or not isinstance(usage, Usage):
|
||||
return None
|
||||
try:
|
||||
prompt_cost, completion_tokens_cost = litellm.cost_per_token(model=model_name, usage_object=usage)
|
||||
return prompt_cost + completion_tokens_cost
|
||||
except Exception: # noqa: BLE001 # cost_per_token raises bare Exception for unpriceable models
|
||||
return None
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3113,6 +3113,24 @@ def test_stream_chunk_builder_unknown_model_leaves_response_cost_unset():
|
|||
assert response.choices[0].message.content == "Hello world."
|
||||
|
||||
|
||||
def test_stream_chunk_builder_prices_proxy_alias_via_model_map():
|
||||
chunks: Final = [
|
||||
_stream_builder_text_chunk("claude-opus-5", "Hello "),
|
||||
_stream_builder_text_chunk("claude-opus-5", "world.", finish_reason="stop"),
|
||||
]
|
||||
for chunk in chunks:
|
||||
chunk._hidden_params = {"custom_llm_provider": "openai"}
|
||||
|
||||
response: Final = litellm.stream_chunk_builder(chunks=chunks, messages=[{"role": "user", "content": "hi"}])
|
||||
|
||||
assert response is not None
|
||||
assert response._hidden_params["custom_llm_provider"] == "openai"
|
||||
prompt_cost, completion_cost = litellm.cost_per_token(model="claude-opus-5", usage_object=response.usage)
|
||||
expected_cost: Final = prompt_cost + completion_cost
|
||||
assert expected_cost > 0
|
||||
assert response._hidden_params["response_cost"] == pytest.approx(expected_cost)
|
||||
|
||||
|
||||
def _stream_builder_logging_obj() -> LiteLLMLogging:
|
||||
logging_obj: Final = LiteLLMLogging(
|
||||
model="gpt-4o",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue