Skip to content

fix: patch litellm ImageURLListItem to make index field optional #384

@nabinchha

Description

@nabinchha

Summary

LiteLLM's ImageURLListItem TypedDict defines index as a required field, but some providers (e.g. OpenRouter) no longer include it in image responses. This causes a Pydantic validation error when LiteLLM constructs its Message object:

pydantic_core._pydantic_core.ValidationError: 1 validation error for Message
images.0.index
  Field required [type=missing, input_value={'type': 'image_url', 'im...}, input_type=dict]

Root cause

  1. litellm/types/llms/openai.pyImageURLListItem is a TypedDict with index: int as a required field (not total=False, no NotRequired)
  2. litellm/types/utils.pyMessage.images is typed as Optional[List[ImageURLListItem]]
  3. litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py — When constructing the Message, images from the provider response are passed through, but OpenRouter returns images like {"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}} without an index field
  4. Pydantic validates the ImageURLListItem TypedDict and fails because index is required but missing

Fix

Added patch_image_url_list_item() to litellm_overrides.py which monkey-patches index to NotRequired[int]. This is called during apply_litellm_patches().

Test plan

  • Unit test test_patch_image_url_list_item_makes_index_optional verifies Message can be constructed without index
  • E2E: run image generation with an OpenRouter image model

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions