-
Notifications
You must be signed in to change notification settings - Fork 148
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
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
Support
image_url
provided as either a string or dict in chat completions and add tests to verify both formats._get_attributes_from_image
to acceptUnion[Mapping[str, Any], str]
and extractImageAttributes.IMAGE_URL
from either a stringimage_url
or a dict withurl
.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.