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]

Fixes #14090

gotsysdbaa27604b67a98
diff --git a/litellm/types/llms/oci.py b/litellm/types/llms/oci.py
index 52c1b2943f7..e3ea07463e6 100644
--- a/litellm/types/llms/oci.py
+++ b/litellm/types/llms/oci.py
@@ -121,7 +121,7 @@ class OCICompletionTokenDetails(BaseModel):
reasoningTokens: int
-class OCIPropmtTokensDetails(BaseModel):
+class OCIPromptTokensDetails(BaseModel):
"""Prompt token details in the OCI response."""
cachedTokens: int
@@ -133,8 +133,8 @@ class OCIResponseUsage(BaseModel):
promptTokens: int
completionTokens: int
totalTokens: int
- completionTokensDetails: OCICompletionTokenDetails
- promptTokensDetails: OCIPropmtTokensDetails
+ completionTokensDetails: Optional[OCICompletionTokenDetails] = None
+ promptTokensDetails: Optional[OCIPromptTokensDetails] = None
class OCIResponseChoice(BaseModel):
diff --git a/tests/test_litellm/llms/oci/chat/test_oci_chat_transformation.py b/tests/test_litellm/llms/oci/chat/test_oci_chat_transformation.py
index 547d4bf807e..cbf2ca356e4 100644
--- a/tests/test_litellm/llms/oci/chat/test_oci_chat_transformation.py
+++ b/tests/test_litellm/llms/oci/chat/test_oci_chat_transformation.py
@@ -305,3 +305,79 @@ class TestOCIChatConfig:
assert usage.prompt_tokens == 10 # type: ignore
assert usage.completion_tokens == 20 # type: ignore
assert usage.total_tokens == 30 # type: ignore
+
+def test_transform_response_with_missing_usage_details(self):
+ """
+ Tests that usage details default to None if they are missing or null in the API response.
+ """
+ config = OCIChatConfig()
+ created_time = datetime.datetime.now(datetime.timezone.utc).isoformat().replace("+00:00", "Z")
+
+ # Case 1: usage fields completely missing
+ mock_oci_response_missing = {
+ "modelId": TEST_MODEL_NAME,
+ "modelVersion": "1.0",
+ "chatResponse": {
+ "apiFormat": "GENERIC",
+ "choices": [],
+ "timeCreated": created_time,
+ "usage": {
+ "promptTokens": 5,
+ "completionTokens": 8,
+ "totalTokens": 13,
+ # missing completionTokensDetails and promptTokensDetails
+ },
+ },
+ }
+
+ response_missing = httpx.Response(status_code=200, json=mock_oci_response_missing)
+ result_missing = config.transform_response(
+ model=TEST_MODEL_NAME,
+ raw_response=response_missing,
+ model_response=ModelResponse(),
+ logging_obj={},
+ request_data={},
+ messages=[],
+ optional_params={},
+ litellm_params={},
+ encoding={},
+ )
+
+ usage_missing = result_missing.usage # type: ignore
+ assert usage_missing.completionTokensDetails is None
+ assert usage_missing.promptTokensDetails is None
+
+ # Case 2: usage fields explicitly null
+ mock_oci_response_null = {
+ "modelId": TEST_MODEL_NAME,
+ "modelVersion": "1.0",
+ "chatResponse": {
+ "apiFormat": "GENERIC",
+ "choices": [],
+ "timeCreated": created_time,
+ "usage": {
+ "promptTokens": 5,
+ "completionTokens": 8,
+ "totalTokens": 13,
+ "completionTokensDetails": None,
+ "promptTokensDetails": None,
+ },
+ },
+ }
+
+ response_null = httpx.Response(status_code=200, json=mock_oci_response_null)
+ result_null = config.transform_response(
+ model=TEST_MODEL_NAME,
+ raw_response=response_null,
+ model_response=ModelResponse(),
+ logging_obj={},
+ request_data={},
+ messages=[],
+ optional_params={},
+ litellm_params={},
+ encoding={},
+ )
+
+ usage_null = result_null.usage # type: ignore
+ assert usage_null.completionTokensDetails is None
+ assert usage_null.promptTokensDetails is None
\ No newline at end of file