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
fix(cost_calculator.py): handle openai usage pydantic object
Krrish Dholakia22e2840daabf
diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.pyindex 5545b842809..6eec8d3cd5c 100644--- a/litellm/cost_calculator.py+++ b/litellm/cost_calculator.py@@ -490,6 +490,10 @@ def completion_cost(isinstance(completion_response, BaseModel)or isinstance(completion_response, dict)): # tts returns a custom class+ if isinstance(completion_response, BaseModel) and not isinstance(+ completion_response, litellm.Usage+ ):+ completion_response = litellm.Usage(**completion_response.model_dump())# get input/output tokens from completion_responseprompt_tokens = completion_response.get("usage", {}).get("prompt_tokens", 0)completion_tokens = completion_response.get("usage", {}).get(diff --git a/litellm/tests/test_completion_cost.py b/litellm/tests/test_completion_cost.pyindex a33f9b6ef33..8df4791c71b 100644--- a/litellm/tests/test_completion_cost.py+++ b/litellm/tests/test_completion_cost.py@@ -918,9 +918,18 @@ def test_vertex_ai_llama_predict_cost():assert predictive_cost == 0-def test_vertex_ai_mistral_predict_cost():[email protected]("usage", ["litellm_usage", "openai_usage"])+def test_vertex_ai_mistral_predict_cost(usage):from litellm.types.utils import Choices, Message, ModelResponse, Usage+ if usage == "litellm_usage":+ response_usage = Usage(prompt_tokens=32, completion_tokens=55, total_tokens=87)+ else:+ from openai.types.completion_usage import CompletionUsage++ response_usage = CompletionUsage(+ prompt_tokens=32, completion_tokens=55, total_tokens=87+ )response_object = ModelResponse(id="26c0ef045020429d9c5c9b078c01e564",choices=[@@ -939,7 +948,7 @@ def test_vertex_ai_mistral_predict_cost():model="vertex_ai/mistral-large",object="chat.completion",system_fingerprint=None,- usage=Usage(prompt_tokens=32, completion_tokens=55, total_tokens=87),+ usage=response_usage,)model = "mistral-large@2407"messages = [{"role": "user", "content": "Hey, hows it going???"}]