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 test_anthropic_messages_openai_model_streaming_cost_injection & openrouter image gen

Sameer Kankuteb20c0afb64b0
diff --git a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py
index ae11b57a98f..4bc9f0c835a 100644
--- a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py
+++ b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py
@@ -2,7 +2,7 @@ import asyncio
import json
import time
import traceback
-from typing import Dict, Iterable, List, Literal, Optional, Tuple, Union
+from typing import Dict, Iterable, List, Literal, Optional, Tuple, Union, cast
import litellm
from litellm._logging import verbose_logger
@@ -13,6 +13,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (
from litellm.types.llms.databricks import DatabricksTool
from litellm.types.llms.openai import (
ChatCompletionThinkingBlock,
+ ImageURLListItem,
OpenAIModerationResponse,
)
from litellm.types.utils import (
@@ -26,13 +27,13 @@ from litellm.types.utils import (
Function,
HiddenParams,
ImageResponse,
- PromptTokensDetailsWrapper,
)
from litellm.types.utils import Logprobs as TextCompletionLogprobs
from litellm.types.utils import (
Message,
ModelResponse,
ModelResponseStream,
+ PromptTokensDetailsWrapper,
RerankResponse,
StreamingChoices,
TextChoices,
@@ -52,6 +53,24 @@ _MODEL_RESPONSE_FIELDS: frozenset = frozenset(ModelResponse.model_fields.keys())
}
+def _normalize_images_for_message(
+ images: Optional[List[dict]],
+) -> Optional[List[ImageURLListItem]]:
+ """
+ Ensure each image has an 'index' field, as required by ImageURLListItem.
+ Some providers (e.g. OpenRouter) return images without index.
+ """
+ if not images:
+ return cast(Optional[List[ImageURLListItem]], images)
+ normalized: List[ImageURLListItem] = []
+ for i, img in enumerate(images):
+ if isinstance(img, dict) and "index" not in img:
+ normalized.append(cast(ImageURLListItem, {**img, "index": i}))
+ else:
+ normalized.append(cast(ImageURLListItem, img))
+ return normalized
+
+
def _safe_convert_created_field(created_value) -> int:
"""
Safely convert a 'created' field value to an integer.
@@ -591,7 +610,9 @@ def convert_to_model_response_object( # noqa: PLR0915
reasoning_content=reasoning_content,
thinking_blocks=thinking_blocks,
annotations=choice["message"].get("annotations", None),
- images=choice["message"].get("images", None),
+ images=_normalize_images_for_message(
+ choice["message"].get("images", None)
+ ),
)
finish_reason = choice.get("finish_reason", None)
if finish_reason is None:
diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json
index 900894f74d6..ae256ed0784 100644
--- a/litellm/model_prices_and_context_window_backup.json
+++ b/litellm/model_prices_and_context_window_backup.json
@@ -16799,6 +16799,42 @@
"supports_vision": true,
"supports_web_search": true
},
+ "gemini/gemini-3.1-flash-image-preview": {
+ "input_cost_per_token": 2.5e-07,
+ "input_cost_per_token_batches": 1.25e-07,
+ "litellm_provider": "gemini",
+ "max_input_tokens": 65536,
+ "max_output_tokens": 32768,
+ "max_tokens": 32768,
+ "mode": "image_generation",
+ "output_cost_per_image": 0.045,
+ "output_cost_per_image_token": 6e-05,
+ "output_cost_per_image_token_batches": 3e-05,
+ "output_cost_per_token": 1.5e-06,
+ "output_cost_per_token_batches": 7.5e-07,
+ "rpm": 1000,
+ "tpm": 4000000,
+ "source": "https://ai.google.dev/gemini-api/docs/pricing#gemini-3.1-flash-image-preview",
+ "supported_endpoints": [
+ "/v1/chat/completions",
+ "/v1/completions",
+ "/v1/batch"
+ ],
+ "supported_modalities": [
+ "text",
+ "image"
+ ],
+ "supported_output_modalities": [
+ "text",
+ "image"
+ ],
+ "supports_function_calling": false,
+ "supports_prompt_caching": true,
+ "supports_response_schema": true,
+ "supports_system_messages": true,
+ "supports_vision": true,
+ "supports_web_search": true
+ },
"gemini/deep-research-pro-preview-12-2025": {
"input_cost_per_image": 0.0011,
"input_cost_per_token": 2e-06,
diff --git a/tests/pass_through_tests/test_anthropic_passthrough.py b/tests/pass_through_tests/test_anthropic_passthrough.py
index 5d30338b041..74829a21a05 100644
--- a/tests/pass_through_tests/test_anthropic_passthrough.py
+++ b/tests/pass_through_tests/test_anthropic_passthrough.py
@@ -395,7 +395,7 @@ async def test_anthropic_messages_openai_model_streaming_cost_injection():
payload = {
"model": "openai/gpt-4o",
- "max_tokens": 10,
+ "max_tokens": 20,
"stream": True,
"messages": [{"role": "user", "content": "Say 'Hi'"}],
}