Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1f32952
fix(ai): redact message parts content of type blob
constantinius Dec 17, 2025
795bcea
fix(ai): skip non dict messages
constantinius Dec 17, 2025
a623e13
fix(ai): typing
constantinius Dec 17, 2025
3d3ce5b
fix(ai): content items may not be dicts
constantinius Dec 17, 2025
ce29e47
fix(integrations): OpenAI input messages are now being converted to t…
constantinius Dec 17, 2025
7074f0b
test(integrations): add test for message conversion
constantinius Dec 17, 2025
e8a1adc
feat(integrations): add transformation functions for OpenAI Agents co…
constantinius Jan 8, 2026
c1a2239
feat(ai): implement parse_data_uri function and integrate it into Ope…
constantinius Jan 8, 2026
bd46a6a
Merge branch 'master' into constantinius/fix/integrations/openai-repo…
constantinius Jan 13, 2026
04b27f4
fix: review comment
constantinius Jan 13, 2026
f8345d0
Merge branch 'master' into constantinius/fix/integrations/openai-repo…
constantinius Jan 14, 2026
b74bdb9
fix(integrations): addressing review comments
constantinius Jan 14, 2026
8080904
fix: review comment
constantinius Jan 15, 2026
05b1a79
fix(integrations): extract text content from OpenAI responses instead…
constantinius Jan 15, 2026
bd78165
feat(ai): Add shared content transformation functions for multimodal …
constantinius Jan 15, 2026
4795c3b
Merge shared content transformation functions
constantinius Jan 15, 2026
df59f49
refactor(openai): Use shared transform_message_content from ai/utils
constantinius Jan 15, 2026
412b93e
refactor(ai): split transform_content_part into SDK-specific functions
constantinius Jan 15, 2026
b99640e
Merge SDK-specific transform functions
constantinius Jan 15, 2026
4fba982
refactor(openai): use transform_openai_content_part directly
constantinius Jan 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions sentry_sdk/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from sys import getsizeof
from typing import TYPE_CHECKING

from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE

if TYPE_CHECKING:
from typing import Any, Callable, Dict, List, Optional, Tuple

Expand Down Expand Up @@ -141,6 +143,57 @@ def _find_truncation_index(messages: "List[Dict[str, Any]]", max_bytes: int) ->
return 0


def redact_blob_message_parts(
messages: "List[Dict[str, Any]]",
) -> "List[Dict[str, Any]]":
"""
Redact blob message parts from the messages, by removing the "content" key.
e.g:
{
"role": "user",
"content": [
{
"text": "How many ponies do you see in the image?",
"type": "text"
},
{
"type": "blob",
"modality": "image",
"mime_type": "image/jpeg",
"content": "data:image/jpeg;base64,..."
}
]
}
becomes:
{
"role": "user",
"content": [
{
"text": "How many ponies do you see in the image?",
"type": "text"
},
{
"type": "blob",
"modality": "image",
"mime_type": "image/jpeg",
"content": "[Filtered]"
}
]
}
"""

for message in messages:
if not isinstance(message, dict):
continue

content = message.get("content")
if isinstance(content, list):
for item in content:
if isinstance(item, dict) and item.get("type") == "blob":
item["content"] = SENSITIVE_DATA_SUBSTITUTE
return messages


def truncate_messages_by_size(
messages: "List[Dict[str, Any]]",
max_bytes: int = MAX_GEN_AI_MESSAGE_BYTES,
Expand Down Expand Up @@ -186,6 +239,8 @@ def truncate_and_annotate_messages(
if not messages:
return None

messages = redact_blob_message_parts(messages)

truncated_messages, removed_count = truncate_messages_by_size(messages, max_bytes)
if removed_count > 0:
scope._gen_ai_original_message_count[span.span_id] = len(messages)
Expand Down
66 changes: 65 additions & 1 deletion sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
safe_serialize,
)

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict

if TYPE_CHECKING:
from typing import Any, Iterable, List, Optional, Callable, AsyncIterator, Iterator
Expand Down Expand Up @@ -177,6 +177,68 @@ def _calculate_token_usage(
)


def _convert_message_parts(messages: "List[Dict[str, Any]]") -> "List[Dict[str, Any]]":
"""
Convert the message parts from OpenAI format to the `gen_ai.request.messages` format.
e.g:
{
"role": "user",
"content": [
{
"text": "How many ponies do you see in the image?",
"type": "text"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,...",
"detail": "high"
}
}
]
}
becomes:
{
"role": "user",
"content": [
{
"text": "How many ponies do you see in the image?",
"type": "text"
},
{
"type": "blob",
"modality": "image",
"mime_type": "image/jpeg",
"content": "data:image/jpeg;base64,..."
}
]
}
"""

def _map_item(item: "Dict[str, Any]") -> "Dict[str, Any]":
if item.get("type") == "image_url":
image_url = item.get("image_url") or {}
if image_url.get("url", "").startswith("data:"):
return {
"type": "blob",
"modality": "image",
"mime_type": item["image_url"]["url"].split(";base64,")[0],
"content": item["image_url"]["url"].split(";base64,")[1],
}
else:
return {
"type": "uri",
"uri": item["image_url"]["url"],
}
return item

for message in messages:
content = message.get("content")
if isinstance(content, list):
message["content"] = [_map_item(item) for item in content]
return messages


def _set_input_data(
span: "Span",
kwargs: "dict[str, Any]",
Expand All @@ -198,6 +260,8 @@ def _set_input_data(
and integration.include_prompts
):
normalized_messages = normalize_message_roles(messages)
normalized_messages = _convert_message_parts(normalized_messages)

scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
if messages_data is not None:
Expand Down
52 changes: 40 additions & 12 deletions sentry_sdk/integrations/openai_agents/spans/invoke_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
get_start_span_function,
set_data_normalized,
normalize_message_roles,
normalize_message_role,
truncate_and_annotate_messages,
)
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import safe_serialize

from ..consts import SPAN_ORIGIN
from ..utils import _set_agent_data, _set_usage_data
from ..utils import (
_set_agent_data,
_set_usage_data,
_transform_openai_agents_message_content,
)

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -49,17 +54,40 @@ def invoke_agent_span(

original_input = kwargs.get("original_input")
if original_input is not None:
message = (
original_input
if isinstance(original_input, str)
else safe_serialize(original_input)
)
messages.append(
{
"content": [{"text": message, "type": "text"}],
"role": "user",
}
)
if isinstance(original_input, str):
# String input: wrap in text block
messages.append(
{
"content": [{"text": original_input, "type": "text"}],
"role": "user",
}
)
elif isinstance(original_input, list) and len(original_input) > 0:
# Check if list contains message objects (with type="message")
# or content parts (input_text, input_image, etc.)
first_item = original_input[0]
if isinstance(first_item, dict) and first_item.get("type") == "message":
# List of message objects - process each individually
for msg in original_input:
if isinstance(msg, dict) and msg.get("type") == "message":
role = normalize_message_role(msg.get("role", "user"))
content = msg.get("content")
transformed = _transform_openai_agents_message_content(
content
)
if isinstance(transformed, str):
transformed = [{"text": transformed, "type": "text"}]
elif not isinstance(transformed, list):
transformed = [
{"text": str(transformed), "type": "text"}
]
messages.append({"content": transformed, "role": role})
else:
# List of content parts - transform and wrap as user message
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"})

if len(messages) > 0:
normalized_messages = normalize_message_roles(messages)
Expand Down
128 changes: 125 additions & 3 deletions sentry_sdk/integrations/openai_agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,126 @@
raise DidNotEnable("OpenAI Agents not installed")


def _transform_openai_agents_content_part(
content_part: "dict[str, Any]",
) -> "dict[str, Any]":
"""
Transform an OpenAI Agents content part to Sentry-compatible format.

Handles multimodal content (images, audio, files) by converting them
to the standardized format:
- base64 encoded data -> type: "blob"
- URL references -> type: "uri"
- file_id references -> type: "file"
"""
if not isinstance(content_part, dict):
return content_part

part_type = content_part.get("type")

# Handle input_text (OpenAI Agents SDK text format) -> normalize to standard text format
if part_type == "input_text":
return {
"type": "text",
"text": content_part.get("text", ""),
}

# Handle image_url (OpenAI vision format) and input_image (OpenAI Agents SDK format)
if part_type in ("image_url", "input_image"):
# Get URL from either format
if part_type == "image_url":
image_url = content_part.get("image_url", {})
url = (
image_url.get("url", "")
if isinstance(image_url, dict)
else str(image_url)
)
else:
# input_image format has image_url directly
url = content_part.get("image_url", "")

if url.startswith("data:"):
# Parse data URI: data:image/jpeg;base64,/9j/4AAQ...
try:
header, content = url.split(",", 1)
mime_type = header.split(":")[1].split(";")[0] if ":" in header else ""
return {
"type": "blob",
"modality": "image",
"mime_type": mime_type,
"content": content,
}
except (ValueError, IndexError):
# If parsing fails, return as URI
return {
"type": "uri",
"modality": "image",
"mime_type": "",
"uri": url,
}
else:
return {
"type": "uri",
"modality": "image",
"mime_type": "",
"uri": url,
}

# Handle input_audio (OpenAI audio input format)
if part_type == "input_audio":
input_audio = content_part.get("input_audio", {})
audio_format = input_audio.get("format", "")
mime_type = f"audio/{audio_format}" if audio_format else ""
return {
"type": "blob",
"modality": "audio",
"mime_type": mime_type,
"content": input_audio.get("data", ""),
}

# Handle image_file (Assistants API file-based images)
if part_type == "image_file":
image_file = content_part.get("image_file", {})
return {
"type": "file",
"modality": "image",
"mime_type": "",
"file_id": image_file.get("file_id", ""),
}

# Handle file (document attachments)
if part_type == "file":
file_data = content_part.get("file", {})
return {
"type": "file",
"modality": "document",
"mime_type": "",
"file_id": file_data.get("file_id", ""),
}

return content_part


def _transform_openai_agents_message_content(content: "Any") -> "Any":
"""
Transform OpenAI Agents message content, handling both string content and
list of content parts.
"""
if isinstance(content, str):
return content

if isinstance(content, (list, tuple)):
transformed = []
for item in content:
if isinstance(item, dict):
transformed.append(_transform_openai_agents_content_part(item))
else:
transformed.append(item)
return transformed

return content


def _capture_exception(exc: "Any") -> None:
set_span_errored()

Expand Down Expand Up @@ -128,13 +248,15 @@ def _set_input_data(
if "role" in message:
normalized_role = normalize_message_role(message.get("role"))
content = message.get("content")
# Transform content to handle multimodal data (images, audio, files)
transformed_content = _transform_openai_agents_message_content(content)
request_messages.append(
{
"role": normalized_role,
"content": (
[{"type": "text", "text": content}]
if isinstance(content, str)
else content
[{"type": "text", "text": transformed_content}]
if isinstance(transformed_content, str)
else transformed_content
),
}
)
Expand Down
Loading
Loading