Skip to content

Conversation

caroger
Copy link
Collaborator

@caroger caroger commented Sep 18, 2025

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
Copy link
Contributor

@axiomofjoy axiomofjoy left a comment

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
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
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?

@caroger caroger merged commit 4d09a7e into main Oct 7, 2025
17 checks passed
@caroger caroger deleted the 2188-bug-openai-instrumentation-error-on-message-images branch October 7, 2025 17:29
@github-actions github-actions bot mentioned this pull request Oct 7, 2025
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