-
Notifications
You must be signed in to change notification settings - Fork 572
fix(integrations): openai/openai-agents: convert input message format #5248
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: master
Are you sure you want to change the base?
fix(integrations): openai/openai-agents: convert input message format #5248
Conversation
…he schema we expect for the `gen_ai.request.messages`
…ntent and update message handling
…nAI message handling
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛
Documentation 📚
Internal Changes 🔧Release
Other
🤖 This preview updates automatically when you update the PR. |
sentrivana
left a comment
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.
Looks ok to me, two things:
- Can we add a check to the tests that we're not modifying the user's messages? Either as a new test or just adding an assert to the tests added in this PR
- I assume there is no way to dedupe some of the trimming logic between OpenAI agents and OpenAI because the format is different?
| if item.get("type") == "image_url": | ||
| image_url = item.get("image_url") or {} | ||
| url = image_url.get("url", "") |
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.
Bug: The code will raise an AttributeError if item.get("image_url") returns a string, because the or {} fallback is not triggered and .get() is called on a string.
Severity: HIGH
Suggested Fix
Add a check to ensure image_url is a dictionary before calling .get() on it. A similar pattern is used elsewhere in the codebase: url = image_url.get("url", "") if isinstance(image_url, dict) else str(image_url). This will handle both dictionary and string formats gracefully.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: sentry_sdk/integrations/openai.py#L226-L228
Potential issue: In the `_convert_message_parts` function, the code processes message
parts to extract an `image_url`. The line `image_url = item.get("image_url") or {}` does
not correctly handle cases where the value of `image_url` is a string instead of a
dictionary. If a string is provided (e.g., `{"type": "image_url", "image_url":
"https://..."}`), the subsequent call to `image_url.get("url", "")` will raise an
`AttributeError`, as strings do not have a `.get()` method. This causes an unhandled
exception within the Sentry integration, preventing the span from being processed
correctly.
Did we get this right? 👍 / 👎 to inform future reviews.
Done
Looking into that. Cursor says no. But I'm not sure tbh |
| content = _transform_openai_agents_message_content(original_input) | ||
| if not isinstance(content, list): | ||
| content = [{"text": str(content), "type": "text"}] | ||
| messages.append({"content": content, "role": "user"}) |
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.
Non-dict content items produce invalid message structure
Low Severity
When original_input is a list containing non-dict items (like strings or numbers), _transform_openai_agents_message_content returns them unchanged. The calling code only wraps the result in text format when it's NOT a list, so lists with non-dict items like ["hello", "world"] become invalid content structures instead of proper [{"text": "hello", "type": "text"}, ...] format. The old code used safe_serialize() to handle any input type safely, producing valid message content for all cases.
Description
Convert messages to common
gen_ai.request.messagesstructureIssues
Closes https://linear.app/getsentry/issue/TET-1633/redact-images-openai-openai-agents