#23269·llama_index

from_openai_message_dict raises KeyError on image_url parts missing the optional detail key

Author: Harsh23KashyapCreated Sep 27, 2026Updated Sep 27, 2026

Bug Description

When converting an OpenAI-format chat message that contains an image part, from_openai_message_dict reads the detail field with a hard subscript:

python
detail = elem["image_url"]["detail"]

The detail key is optional in the OpenAI API. The reference documents it as one of low, high, or auto, and the model uses low when the parameter is not provided, so providers and callers may omit it. llama-index's own serializer already treats it as optional: to_openai_message_dict includes the key only when the ImageBlock has a detail value. The write side was fixed in #20609, but the read side still assumes the key is always present.

Two real paths break:

  1. Provider responses. Integrations that reuse this converter on provider-returned dicts (llama-index-llms-litellm and llama-index-llms-konko both call from_openai_message_dict) crash when a provider omits detail on an image part.
  2. Internal round-trip. Serializing a chat message whose ImageBlock has no detail with to_openai_message_dict and converting it back raises KeyError: 'detail', even though llama-index produced the dict itself.

Version

llama-index-llms-openai 0.8.2, reproduced on current main (169e450aa54d).

Steps to Reproduce

python
"""from_openai_message_dict crashes on image_url parts without 'detail'."""
from llama_index.core.base.llms.types import ChatMessage, ImageBlock
from llama_index.llms.openai.utils import (
    from_openai_message_dict,
    to_openai_message_dict,
)

# Case 1: provider-style dict (what LiteLLM/Konko pass in). 'detail' is
# optional per the OpenAI API spec and routinely absent.
message_dict = {
    "role": "user",
    "content": [
        {"type": "text", "text": "what is in this image?"},
        {"type": "image_url", "image_url": {"url": "https://example.com/cat.png"}},
    ],
}
back = from_openai_message_dict(message_dict)

# Case 2: internal round-trip - to_openai_message_dict omits 'detail' when
# the ImageBlock has none, and from_openai_message_dict cannot read it back.
msg = ChatMessage(
    role="user", blocks=[ImageBlock(url="https://example.com/cat.png")]
)
d = to_openai_message_dict(msg)
from_openai_message_dict(d)

Both cases raise KeyError: 'detail'. I ran the script twice; the output was identical.

Relevant Logs/Tracebacks

Traceback (most recent call last):
  File "/tmp/repro/image_detail.py", line 17, in <module>
    back = from_openai_message_dict(message_dict)
  File "/home/sandbox/llama_index/llama-index-integrations/llms/llama-index-llms-openai/llama_index/llms/openai/utils.py", line 976, in from_openai_message_dict
    detail = elem["image_url"]["detail"]
KeyError: 'detail'