Skip to content

fix(openai): handle string format for image_url in chat completions - #2213

Merged
caroger merged 6 commits into
mainfrom
2188-bug-openai-instrumentation-error-on-message-images
Oct 7, 2025
Merged

caroger merged 6 commits into
mainfrom
2188-bug-openai-instrumentation-error-on-message-images

Conversation

@caroger

@caroger caroger commented Sep 18, 2025 •

Copy link
Copy Markdown
Contributor

Summary

Fixes issue #2188 where OpenAI instrumentation crashed with a ValueError when users passed image_url as a string instead of a dictionary.

Changes

  • Fixed _get_attributes_from_image() to handle both string and dict formats:
    • String format: "image_url": "https://example.com/image.png" (was causing crash)
    • Dict format: "image_url": {"url": "https://example.com/image.png"} (was working)

Root Cause

The dict(image) call in _get_attributes_from_image() failed when image was a string, causing: ValueError: dictionary update sequence element #0 has length 1; 2 is required


Note

Support image_url provided as either a string or dict in chat completions and add tests to verify both formats.

  • Instrumentation (OpenAI request attributes):
    • Update _get_attributes_from_image to accept Union[Mapping[str, Any], str] and extract ImageAttributes.IMAGE_URL from either a string image_url or a dict with url.
  • Tests:
    • Add parametrized test to validate both image_url formats are captured correctly in span attributes.

Written by Cursor Bugbot for commit 0004b56. This will update automatically on new commits. Configure here.

@caroger
caroger requested a review from a team as a code owner September 18, 2025 16:18
@caroger caroger linked an issue Sep 18, 2025 that may be closed by this pull request
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Sep 18, 2025

@axiomofjoy axiomofjoy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments feel a bit verbose

Comment on lines +1186 to +1195
respx_mock.post(url).mock(
return_value=Response(
status_code=200,
json={
"choices": [{"index": 0, "message": output_messages[0], "finish_reason": "stop"}],
"model": model_name,
"usage": completion_usage,
},
)
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use vcrpy instead of a manual mock?

content_type_attr = attributes.get(content_type_key)
assert content_type_attr == "image"

# Clean up for other tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems wrong? What kind of clean up is this doing since this is the end of the test?

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

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[bug] OpenAI instrumentation error on message images

2 participants