Skip to content

fix(litellm): handle image_url given as a plain string - #4479

Open
1AbdullahButt wants to merge 1 commit into
Arize-ai:mainfrom
1AbdullahButt:fix/litellm-string-image-url
Open

1AbdullahButt wants to merge 1 commit into
Arize-ai:mainfrom
1AbdullahButt:fix/litellm-string-image-url

Conversation

@1AbdullahButt

Copy link
Copy Markdown

fixes #4478

The LiteLLM instrumentor drops most request attributes when a message has an image_url part given as a plain URL string.

Problem

LiteLLM accepts image_url as either {"url": "..."} or the URL string itself (ChatCompletionImageObject.image_url: Union[str, ChatCompletionImageUrlObject] in litellm/types/llms/openai.py). _get_attributes_from_image calls dict(image), which raises on a string:

ValueError: dictionary update sequence element #0 has length 1; 2 is required

_suppress_extractor_errors catches it, so the call still works. But _instrument_func_type_completion stops at that point, and the span is left without the rest of the input messages, input.value, llm.invocation_parameters, and llm.tools.

Repro:

litellm.completion(
    model="gpt-4o",
    messages=[{"role": "user", "content": [
        {"type": "text", "text": "What's in this image?"},
        {"type": "image_url", "image_url": "https://example.com/cat.jpg"},
    ]}],
    mock_response="A cat",
)

The openai instrumentor had the same bug (#2188) and fixed it in #2213. This PR makes the same change in the LiteLLM instrumentor.

Fix

_get_attributes_from_image now treats a string as the URL and reads url from a mapping with .get(). A mapping without url is now skipped instead of raising KeyError.

Tests

Added test_completion_image_url_as_string. It checks the image URL, input.value, and invocation parameters on the span.

  • Before the fix: 1 failed, 165 passed (the new test fails)
  • After the fix: 166 passed

ruff format and ruff check pass on the changed files.

I made this change with help from Claude Code, and I reviewed the diff and the test results myself.

🤖 Generated with Claude Code

LiteLLM types image_url as Union[str, ChatCompletionImageUrlObject], but
_get_attributes_from_image called dict(image), which raises ValueError on
a string. The error aborted the whole completion request extractor, so the
span lost the remaining input messages, input.value, invocation parameters
and tools. Record the string as the image URL instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@1AbdullahButt

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

litellm: string image_url breaks request attribute extraction

1 participant