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]

fix(perplexity): remove redundant dict-cost check in Usage and add streaming cost test

Chesars636b474a8ae2
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index 9a0272c8667..3c818387744 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -1580,13 +1580,7 @@ class Usage(SafeAttributeModel, CompletionUsage):
del self.server_tool_use
if cost is not None:
- # Some providers (e.g. Perplexity) return cost as a dict with total_cost
- if isinstance(cost, dict):
- cost = cost.get("total_cost")
- if cost is not None:
- self.cost = cost
- else:
- del self.cost
+ self.cost = cost
else:
del self.cost
diff --git a/tests/test_litellm/llms/perplexity/responses/test_perplexity_responses_transformation.py b/tests/test_litellm/llms/perplexity/responses/test_perplexity_responses_transformation.py
index 7a3c0316d48..84299b5bb28 100644
--- a/tests/test_litellm/llms/perplexity/responses/test_perplexity_responses_transformation.py
+++ b/tests/test_litellm/llms/perplexity/responses/test_perplexity_responses_transformation.py
@@ -452,3 +452,62 @@ class TestPerplexityResponsesTransformation:
assert response.id == "resp_123"
assert response.status == "completed"
+
+ def test_streaming_cost_dict_to_float_via_validator(self):
+ """Cost dict in a streaming response.completed chunk is converted to float
+ end-to-end through transform_streaming_response via pydantic's recursive
+ construction of ResponsesAPIResponse → ResponseAPIUsage.parse_cost."""
+ config = PerplexityResponsesConfig()
+
+ completed_chunk = {
+ "type": "response.completed",
+ "response": {
+ "id": "resp_streaming_123",
+ "object": "response",
+ "created_at": 1700000000,
+ "status": "completed",
+ "model": "openai/gpt-5.2",
+ "output": [
+ {
+ "type": "message",
+ "id": "msg_123",
+ "role": "assistant",
+ "status": "completed",
+ "content": [
+ {"type": "output_text", "text": "Hello!", "annotations": []}
+ ],
+ }
+ ],
+ "usage": {
+ "input_tokens": 100,
+ "output_tokens": 200,
+ "total_tokens": 300,
+ "cost": {
+ "currency": "USD",
+ "input_cost": 0.0001,
+ "output_cost": 0.0002,
+ "total_cost": 0.0003,
+ },
+ },
+ },
+ }
+
+ logging_obj = LiteLLMLoggingObj(
+ model="perplexity/openai/gpt-5.2",
+ messages=[],
+ stream=True,
+ call_type="responses",
+ start_time=None,
+ litellm_call_id="test",
+ function_id="test",
+ )
+
+ result = config.transform_streaming_response(
+ model="perplexity/openai/gpt-5.2",
+ parsed_chunk=completed_chunk,
+ logging_obj=logging_obj,
+ )
+
+ assert result.type == "response.completed"
+ assert result.response.usage.cost == 0.0003
+ assert isinstance(result.response.usage.cost, float)