Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
1afe5d6
feat(openai): Set gen_ai.tool.definitions attribute
alexander-alderman-webb Jul 30, 2026
33faef1
add type
alexander-alderman-webb Jul 30, 2026
9ba36b2
feat(openai): Set attribute for the Responses API
alexander-alderman-webb Jul 30, 2026
7398e26
make iteration safer
alexander-alderman-webb Jul 30, 2026
0cd572b
clean up test
alexander-alderman-webb Jul 30, 2026
6d8546a
clean up
alexander-alderman-webb Jul 30, 2026
54ca1d9
higher version bound
alexander-alderman-webb Jul 30, 2026
f94e12e
fix import error
alexander-alderman-webb Jul 30, 2026
9f5b9ed
merge
alexander-alderman-webb Jul 30, 2026
8048f1d
fix import
alexander-alderman-webb Jul 30, 2026
e8f202e
Merge branch 'webb/openai-completions-gen-ai-tool-definitions' into w…
alexander-alderman-webb Jul 30, 2026
a9e263c
more import problems
alexander-alderman-webb Jul 30, 2026
2ceb5ef
restore import changes
alexander-alderman-webb Jul 30, 2026
ee8639e
Merge branch 'webb/openai-completions-gen-ai-tool-definitions' into w…
alexander-alderman-webb Jul 30, 2026
7c30a05
ref(openai-agents): Stop setting gen_ai.request.available_tools on Ex…
alexander-alderman-webb Jul 30, 2026
df36922
ref(openai-agents): Stop setting gen_ai.request.available_tools on In…
alexander-alderman-webb Jul 30, 2026
758d92a
feat(openai-agents): Set gen_ai.tool.definitions attribute
alexander-alderman-webb Jul 30, 2026
74b0823
type safety
alexander-alderman-webb Jul 30, 2026
7496d8e
Merge branch 'webb/openai-responses-gen-ai-tool-definitions' into web…
alexander-alderman-webb Jul 30, 2026
4e12293
Merge branch 'webb/openai-agents-tool-definitions' into webb/openai-a…
alexander-alderman-webb Jul 30, 2026
8b9e2e8
Merge branch 'webb/openai-agents-agent-tool-definitions' into webb/op…
alexander-alderman-webb Jul 30, 2026
a367940
make work
alexander-alderman-webb Jul 30, 2026
38d466f
make mypy happy
alexander-alderman-webb Jul 30, 2026
74cd1f1
test in progress
alexander-alderman-webb Jul 30, 2026
18a45a2
fix(openai-agents): Stop destructively wrapping tools
alexander-alderman-webb Jul 30, 2026
391dd4e
Merge branch 'webb/openai-agents-tool-overwrite' into webb/openai-age…
alexander-alderman-webb Jul 30, 2026
ce1c3fe
Merge branch 'webb/openai-agents-agent-tool-definitions' into webb/op…
alexander-alderman-webb Jul 30, 2026
ccf985c
finish
alexander-alderman-webb Jul 31, 2026
576cf6c
fix test
alexander-alderman-webb Jul 31, 2026
bf804f8
respect web_search_preview
alexander-alderman-webb Jul 31, 2026
89f78bf
add quotes around type annotation
alexander-alderman-webb Jul 31, 2026
a15f21b
Merge branch 'webb/openai-responses-gen-ai-tool-definitions' into web…
alexander-alderman-webb Jul 31, 2026
df909c3
Merge branch 'webb/openai-agents-tool-definitions' into webb/openai-a…
alexander-alderman-webb Jul 31, 2026
c140a3f
Merge branch 'webb/openai-agents-agent-tool-definitions' into webb/op…
alexander-alderman-webb Jul 31, 2026
5aacbc7
Merge branch 'webb/openai-agents-tool-overwrite' into webb/openai-age…
alexander-alderman-webb Jul 31, 2026
c21bbf4
flip version gate
alexander-alderman-webb Jul 31, 2026
4efb6fb
try out RunHooks
alexander-alderman-webb Jul 31, 2026
4eec0bb
Revert "try out RunHooks"
alexander-alderman-webb Jul 31, 2026
1a2a232
make idempotent
alexander-alderman-webb Jul 31, 2026
ddf2a2c
Merge branch 'webb/openai-agents-tool-overwrite' into webb/openai-age…
alexander-alderman-webb Jul 31, 2026
23696f3
gate custom access
alexander-alderman-webb Jul 31, 2026
53ae691
Merge branch 'webb/openai-completions-gen-ai-tool-definitions' into w…
alexander-alderman-webb Jul 31, 2026
adf5e38
Merge branch 'webb/openai-responses-gen-ai-tool-definitions' into web…
alexander-alderman-webb Jul 31, 2026
e89f581
Merge branch 'webb/openai-agents-tool-definitions' into webb/openai-a…
alexander-alderman-webb Jul 31, 2026
27d9ea6
Merge branch 'webb/openai-agents-agent-tool-definitions' into webb/op…
alexander-alderman-webb Jul 31, 2026
6f1611a
add programmatic tool call
alexander-alderman-webb Jul 31, 2026
a301748
merge master
alexander-alderman-webb Jul 31, 2026
08f26fe
remove attribute
alexander-alderman-webb Jul 31, 2026
f27ec10
Merge branch 'webb/openai-agents-agent-tool-definitions' into webb/op…
alexander-alderman-webb Jul 31, 2026
114f34c
Merge branch 'webb/openai-agents-tool-overwrite' into webb/openai-age…
alexander-alderman-webb Jul 31, 2026
b78dc21
merge
alexander-alderman-webb Jul 31, 2026
008ef51
merge
alexander-alderman-webb Jul 31, 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
189 changes: 182 additions & 7 deletions sentry_sdk/integrations/openai_agents/spans/ai_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
import json
from typing import TYPE_CHECKING

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.traces import StreamedSpan
from sentry_sdk.tracing_utils import has_span_streaming_enabled
from sentry_sdk.utils import safe_serialize

try:
from agents import (
CodeInterpreterTool,
ComputerTool,
FileSearchTool,
FunctionTool,
HostedMCPTool,
ImageGenerationTool,
LocalShellTool,
WebSearchTool,
)
except ImportError:
raise DidNotEnable("OpenAI Agents not installed")
Comment thread
cursor[bot] marked this conversation as resolved.

try:
from agents import ApplyPatchTool, ShellTool
except ImportError:
ShellTool = None
ApplyPatchTool = None

try:
from agents import ToolSearchTool
except ImportError:
ToolSearchTool = None

try:
from agents import CustomTool
except ImportError:
CustomTool = None

try:
from agents import ProgrammaticToolCallingTool
except ImportError:
ProgrammaticToolCallingTool = None


from ..consts import SPAN_ORIGIN
from ..utils import (
Expand All @@ -17,7 +54,146 @@
if TYPE_CHECKING:
from typing import Any, Optional, Union

from agents import Agent
from agents import Agent, Tool

from sentry_sdk._types import ToolDefinition


def _transform_tool_definitions(tools: "list[Tool]") -> "list[ToolDefinition]":
"""
Transform tool definitions to the schema used by the "gen_ai.tool.definitions" attribute.
Includes special handling for tools where the type includes a name, description or parameters.

Note: These are generated from the same OpenAPI spec as Responses API tools.
"""
if not isinstance(tools, list):
return []

tool_definitions: "list[ToolDefinition]" = []
for tool in tools:
if isinstance(tool, FunctionTool):
tool_definitions.append(
{
"type": "function",
"name": tool.name,
"description": tool.description,
"parameters": tool.params_json_schema,
}
)
continue

if isinstance(tool, WebSearchTool):
tool_definitions.append(
{
"type": "web_search",
"name": tool.name,
}
)
continue

if isinstance(tool, FileSearchTool):
tool_definitions.append(
{
"type": "file_search",
"name": tool.name,
}
)
continue

if isinstance(tool, ComputerTool):
tool_definitions.append(
{
"type": "computer",
"name": tool.name,
}
)
continue

if CustomTool is not None and isinstance(tool, CustomTool):
tool_definitions.append(
{
"type": "custom",
"name": tool.name,
"description": tool.description,
}
)
continue

if isinstance(tool, HostedMCPTool):
tool_definitions.append(
{
"type": "mcp",
"name": tool.name,
}
)
continue

if ApplyPatchTool is not None and isinstance(tool, ApplyPatchTool):
tool_definitions.append(
{
"type": "apply_patch",
"name": tool.name,
}
)
continue

if ShellTool is not None and isinstance(tool, ShellTool):
tool_definitions.append(
{
"type": "shell",
"name": tool.name,
}
)
continue

if isinstance(tool, ImageGenerationTool):
tool_definitions.append(
{
"type": "image_generation",
"name": tool.name,
}
)
continue

if isinstance(tool, CodeInterpreterTool):
tool_definitions.append(
{
"type": "code_interpreter",
"name": tool.name,
}
)
continue

if isinstance(tool, LocalShellTool):
tool_definitions.append(
{
"type": "local_shell",
"name": tool.name,
}
)
continue

if ToolSearchTool is not None and isinstance(tool, ToolSearchTool):
tool_definitions.append(
{
"type": "tool_search",
"name": tool.name,
}
)
continue

if ProgrammaticToolCallingTool is not None and isinstance(
tool, ProgrammaticToolCallingTool
):
tool_definitions.append(
{
"type": "programmatic_tool_calling",
"name": tool.name,
}
)
continue

return tool_definitions


def ai_client_span(
Expand Down Expand Up @@ -56,11 +232,10 @@ def ai_client_span(

_set_agent_data(span, agent)

if len(agent.tools) > 0:
set_on_span(
SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS,
safe_serialize([vars(tool) for tool in agent.tools]),
)
set_on_span(
SPANDATA.GEN_AI_TOOL_DEFINITIONS,
json.dumps(_transform_tool_definitions(agent.tools)),
Comment thread
sentry-warden[bot] marked this conversation as resolved.
Comment thread
alexander-alderman-webb marked this conversation as resolved.
)
Comment thread
alexander-alderman-webb marked this conversation as resolved.

_set_input_data(span, get_response_kwargs)

Expand Down
Loading
Loading