feat(pydantic-ai): Support ImageUrl content type in span instrumentation#5629
Conversation
Add handling for the pydantic-ai `ImageUrl` message content type in the pydantic-ai integration. For data URLs containing base64-encoded images, the content is redacted and replaced with a placeholder to avoid sending large binary payloads to Sentry. For regular HTTP URLs, the URL string is preserved as-is. Refactor binary content serialization into shared helper functions `_serialize_binary_content_item` and `_serialize_image_url_item` in `spans/utils.py` to remove duplication between `ai_client.py` and `invoke_agent.py`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Pydantic Ai
Other
Bug Fixes 🐛
Documentation 📚
Internal Changes 🔧Openai Agents
Other
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ 32 passed | Total: 32 | Pass Rate: 100% | Execution Time: 294ms All tests are passing successfully. ❌ Patch coverage is 0.00%. Project has 15215 uncovered lines. Files with missing lines (4)
Generated by Codecov Action |
The regex used to detect and redact base64 data URLs only allowed alphabetic characters in MIME types, causing it to fail for types like `image/svg+xml`, `application/vnd.ms-excel`, or `font/woff2`. When the match failed, the full raw data URL (including base64 content) was passed through to Sentry instead of being redacted with BLOB_DATA_SUBSTITUTE, resulting in unintended data leakage. Expand the MIME type character class to include digits, `.`, `+`, and `-` to match all common MIME types per RFC 2045. Co-Authored-By: Claude <noreply@anthropic.com>
Cover the case where data URLs include optional parameters between the MIME type and base64 encoding, e.g. `data:image/png;name=file.png;base64,...` and `data:text/plain;charset=utf-8;name=hello.txt;base64,...`. These should be matched and redacted by DATA_URL_BASE64_REGEX. Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Autofix Details
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Unused imports of BinaryContent and ImageUrl in utils
- Removed unused BinaryContent and ImageUrl imports from spans/utils.py as they were never referenced in the file.
Or push these changes by commenting:
@cursor push 85791c9981
Preview (85791c9981)
diff --git a/sentry_sdk/integrations/pydantic_ai/spans/utils.py b/sentry_sdk/integrations/pydantic_ai/spans/utils.py
--- a/sentry_sdk/integrations/pydantic_ai/spans/utils.py
+++ b/sentry_sdk/integrations/pydantic_ai/spans/utils.py
@@ -13,13 +13,7 @@
from typing import Union, Dict, Any, List, Optional
from pydantic_ai.usage import RequestUsage, RunUsage # type: ignore
-try:
- from pydantic_ai.messages import BinaryContent, ImageUrl # type: ignore
-except ImportError:
- BinaryContent = None
- ImageUrl = None
-
def _serialize_image_url_item(item: "Any") -> "Dict[str, Any]":
"""Serialize an ImageUrl content item for span data.This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.
Remove unused imports (BinaryContent, ImageUrl, Optional, List) from utils.py and add explicit assertion in test to ensure image content is actually found in messages data rather than silently passing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
alexander-alderman-webb
left a comment
There was a problem hiding this comment.
Looks good overall! Two points of feedback:
- We can remove another
mime_typefield, since there's not been a request for the SDK to send this information 😅 - We can write the tests in a way that reduces future work for us.
Could you also PR to the AI Agents Insight module devdocs to document the regex 🙏
Include a sentence about possible cases in image URLs and the regex that you suggest other SDKs to follow as well!
…idate tests Remove the mime_type field from ImageUrl serialization in spans since it is not needed for the base64 redaction use case. Update the regex to use non-capturing groups accordingly. Consolidate scattered image URL tests into two parameterized test functions. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
In Pydantic v2, ImageUrl.url is a Url object, not a string. Passing it directly to re.match() raises TypeError at runtime. Convert to string first, then reuse for both the regex match and the return value. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
alexander-alderman-webb
left a comment
There was a problem hiding this comment.
Very nice!
Just one line in the test that looks strange ...
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Fixes PY-2129 and #5627
Add handling for the pydantic-ai
ImageUrlmessage content type in span instrumentation.Previously, only
BinaryContentwas handled for non-text message parts. With recent pydantic-ai versions, users can passImageUrlobjects as part of their prompts. Without handling this type,ImageUrlitems would fall through tosafe_serialize, losing structured information about the content.