Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sentry_sdk.utils import safe_serialize

from ..consts import SPAN_ORIGIN
from ..utils import _set_agent_data
from ..utils import _set_agent_data, _set_output_data

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -75,4 +75,7 @@ def update_invoke_agent_span(context, agent, output):
span, SPANDATA.GEN_AI_RESPONSE_TEXT, output, unpack=False
)

# Capture tool calls from the output if available
_set_output_data(span, output)

span.__exit__(None, None, None)
53 changes: 42 additions & 11 deletions sentry_sdk/integrations/openai_agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import event_from_exception, safe_serialize
from sentry_sdk.utils import event_from_exception

from typing import TYPE_CHECKING

Expand All @@ -28,6 +28,35 @@ def _capture_exception(exc):
sentry_sdk.capture_event(event, hint=hint)


def _simplify_openai_agent_tools(tools):
# type: (Any) -> list[dict[str, Any]] | None
"""Parse and simplify OpenAI agent tools into a cleaner format."""
if not tools:
return None

if not isinstance(tools, (list, tuple)):
return None

simplified_tools = []
for tool in tools:
try:
simplified_tool = {
"name": getattr(tool, "name", None),
"description": getattr(tool, "description", None),
}

tool_type = getattr(tool, "__class__", None)
if tool_type:
simplified_tool["type"] = tool_type.__name__.lower().replace("tool", "")

if simplified_tool["name"]:
simplified_tools.append(simplified_tool)
except Exception:
continue

return simplified_tools if simplified_tools else None


def _set_agent_data(span, agent):
# type: (sentry_sdk.tracing.Span, agents.Agent) -> None
span.set_data(
Expand Down Expand Up @@ -66,10 +95,9 @@ def _set_agent_data(span, agent):
)

if len(agent.tools) > 0:
span.set_data(
SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS,
safe_serialize([vars(tool) for tool in agent.tools]),
)
simplified_tools = _simplify_openai_agent_tools(agent.tools)
if simplified_tools:
span.set_data(SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, simplified_tools)


def _set_usage_data(span, usage):
Expand Down Expand Up @@ -135,19 +163,22 @@ def _set_output_data(span, result):

for output in result.output:
if output.type == "function_call":
output_messages["tool"].append(output.dict())
if hasattr(output, "model_dump"):
output_messages["tool"].append(output.model_dump())
else:
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 hasattr(output_message, "model_dump"):
output_messages["response"].append(output_message.model_dump())
else:
output_messages["response"].append(output_message.dict())

if len(output_messages["tool"]) > 0:
span.set_data(
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, safe_serialize(output_messages["tool"])
)
span.set_data(SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, output_messages["tool"])

if len(output_messages["response"]) > 0:
set_data_normalized(
Expand Down
Loading