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

Updates OpenAI instrumentation to handle image_url as either a string or dict and adds tests to cover both formats.

  • OpenAI Instrumentation:
    • Update python/.../_request_attributes_extractor.py:
      • _get_attributes_from_image() now accepts Union[Mapping[str, Any], str] and extracts ImageAttributes.IMAGE_URL from either format.
      • Add Union import and adjust dict handling.
  • Tests:
    • Add parametrized test test_chat_completions_with_image_url_formats_issue_2188 to validate both image_url string and dict formats and resulting span attributes.

Written by Cursor Bugbot for commit c3fe35f. 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?

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: No status
Development

Successfully merging this pull request may close these issues.

[bug] OpenAI instrumentation error on message images
2 participants