Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 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
a2565c1
fix: Delete uv.lock
constantinius Jan 16, 2026
2c030cf
test: skip tests if `chat_completion_message_tool_call` is not available
constantinius Jan 22, 2026
6fb6def
Merge branch 'master' into constantinius/fix/integrations/openai-repo…
constantinius Jan 22, 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
40 changes: 40 additions & 0 deletions sentry_sdk/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,46 @@ def set_data_normalized(
span.set_data(key, json.dumps(normalized))


def extract_response_output(
output_items: "Any",
) -> "Tuple[List[Any], List[Dict[str, Any]]]":
"""
Extract response text and tool calls from OpenAI Responses API output.

This handles the output format from OpenAI's Responses API where each output
item has a `type` field that can be "message" or "function_call".

Args:
output_items: Iterable of output items from the response

Returns:
Tuple of (response_texts, tool_calls) where:
- response_texts: List of text strings or dicts for unknown message types
- tool_calls: List of tool call dicts
"""
response_texts = [] # type: List[Any]
tool_calls = [] # type: List[Dict[str, Any]]

for output in output_items:
if output.type == "function_call":
if hasattr(output, "model_dump"):
tool_calls.append(output.model_dump())
elif hasattr(output, "dict"):
tool_calls.append(output.dict())
elif output.type == "message":
for output_message in output.content:
try:
response_texts.append(output_message.text)
except AttributeError:
# Unknown output message type, just return the json
if hasattr(output_message, "model_dump"):
response_texts.append(output_message.model_dump())
elif hasattr(output_message, "dict"):
response_texts.append(output_message.dict())

return response_texts, tool_calls


def normalize_message_role(role: str) -> str:
"""
Normalize a message role to one of the 4 allowed gen_ai role values.
Expand Down
86 changes: 59 additions & 27 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from sentry_sdk import consts
from sentry_sdk.ai.monitoring import record_token_usage
from sentry_sdk.ai.utils import (
extract_response_output,
set_data_normalized,
normalize_message_roles,
transform_openai_content_part,
truncate_and_annotate_messages,
)
from sentry_sdk.consts import SPANDATA
Expand Down Expand Up @@ -203,6 +205,21 @@ def _set_input_data(
and integration.include_prompts
):
normalized_messages = normalize_message_roles(messages)
# Transform content parts to standardized format using OpenAI-specific transformer
for message in normalized_messages:
if isinstance(message, dict) and "content" in message:
content = message["content"]
if isinstance(content, (list, tuple)):
transformed = []
for item in content:
if isinstance(item, dict):
result = transform_openai_content_part(item)
# If transformation succeeded, use the result; otherwise keep original
transformed.append(result if result is not None else item)
else:
transformed.append(item)
message["content"] = transformed

scope = sentry_sdk.get_current_scope()
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
if messages_data is not None:
Expand Down Expand Up @@ -265,49 +282,64 @@ def _set_output_data(

if hasattr(response, "choices"):
if should_send_default_pii() and integration.include_prompts:
response_text = [
choice.message.model_dump()
for choice in response.choices
if choice.message is not None
]
response_text = [] # type: list[str]
tool_calls = [] # type: list[Any]

for choice in response.choices:
if choice.message is None:
continue

# Extract text content
content = getattr(choice.message, "content", None)
if content is not None:
response_text.append(content)

# Extract audio transcript if available
audio = getattr(choice.message, "audio", None)
if audio is not None:
transcript = getattr(audio, "transcript", None)
if transcript is not None:
response_text.append(transcript)

# Extract tool calls
message_tool_calls = getattr(choice.message, "tool_calls", None)
if message_tool_calls is not None:
for tool_call in message_tool_calls:
if hasattr(tool_call, "model_dump"):
tool_calls.append(tool_call.model_dump())
elif hasattr(tool_call, "dict"):
tool_calls.append(tool_call.dict())

if len(response_text) > 0:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, response_text)

if len(tool_calls) > 0:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
tool_calls,
unpack=False,
)

_calculate_token_usage(messages, response, span, None, integration.count_tokens)

if finish_span:
span.__exit__(None, None, None)

elif hasattr(response, "output"):
if should_send_default_pii() and integration.include_prompts:
output_messages: "dict[str, list[Any]]" = {
"response": [],
"tool": [],
}

for output in response.output:
if output.type == "function_call":
output_messages["tool"].append(output.dict())
elif output.type == "message":
for output_message in output.content:
try:
output_messages["response"].append(output_message.text)
except AttributeError:
# Unknown output message type, just return the json
output_messages["response"].append(output_message.dict())

if len(output_messages["tool"]) > 0:
response_texts, tool_calls = extract_response_output(response.output)

if len(tool_calls) > 0:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
output_messages["tool"],
tool_calls,
unpack=False,
)

if len(output_messages["response"]) > 0:
set_data_normalized(
span, SPANDATA.GEN_AI_RESPONSE_TEXT, output_messages["response"]
)
if len(response_texts) > 0:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, response_texts)

_calculate_token_usage(messages, response, span, None, integration.count_tokens)

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
Loading
Loading