-
Notifications
You must be signed in to change notification settings - Fork 142
fix(openai): handle string format for image_url in chat completions #2213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(openai): handle string format for image_url in chat completions #2213
Conversation
There was a problem hiding this 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
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, | ||
}, | ||
) | ||
) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
Summary
Fixes issue #2188 where OpenAI instrumentation crashed with a
ValueError
when users passedimage_url
as a string instead of a dictionary.Changes
_get_attributes_from_image()
to handle both string and dict formats:"image_url": "https://example.com/image.png"
(was causing crash)"image_url": {"url": "https://example.com/image.png"}
(was working)Root Cause
The
dict(image)
call in_get_attributes_from_image()
failed whenimage
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.python/.../_request_attributes_extractor.py
:_get_attributes_from_image()
now acceptsUnion[Mapping[str, Any], str]
and extractsImageAttributes.IMAGE_URL
from either format.Union
import and adjust dict handling.test_chat_completions_with_image_url_formats_issue_2188
to validate bothimage_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.