Skip to content

Commit 5ce67e7

Browse files
committed
use scopes, that what they are meant for.
1 parent 2558fd1 commit 5ce67e7

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

sentry_sdk/integrations/openai_agents.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ async def on_agent_start(self, context: RunContextWrapper, agent: Agent) -> None
4141
)
4242
span = sentry_sdk.start_span(op="gen_ai.agent_start", description=agent.name)
4343
span.__enter__()
44-
self.agent_span = span
4544

4645
async def on_agent_end(
4746
self, context: RunContextWrapper, agent: Agent, output: Any
@@ -50,9 +49,9 @@ async def on_agent_end(
5049
print(
5150
f"### {self.event_counter}: Agent '{agent.name}' ended with output {output}. Usage: {self._usage_to_str(context.usage)}"
5251
)
53-
if self.agent_span:
54-
self.agent_span.__exit__(None, None, None)
55-
self.agent_span = None
52+
current_span = sentry_sdk.get_current_span()
53+
if current_span:
54+
current_span.__exit__(None, None, None)
5655

5756
async def on_tool_start(
5857
self, context: RunContextWrapper, agent: Agent, tool: Tool
@@ -63,7 +62,6 @@ async def on_tool_start(
6362
)
6463
span = sentry_sdk.start_span(op="gen_ai.tool_start", description=tool.name)
6564
span.__enter__()
66-
self.tool_span = span
6765

6866
async def on_tool_end(
6967
self, context: RunContextWrapper, agent: Agent, tool: Tool, result: str
@@ -72,9 +70,9 @@ async def on_tool_end(
7270
print(
7371
f"### {self.event_counter}: Tool {tool.name} ended with result {result}. Usage: {self._usage_to_str(context.usage)}"
7472
)
75-
if self.tool_span:
76-
self.tool_span.__exit__(None, None, None)
77-
self.tool_span = None
73+
current_span = sentry_sdk.get_current_span()
74+
if current_span:
75+
current_span.__exit__(None, None, None)
7876

7977
async def on_handoff(
8078
self, context: RunContextWrapper, from_agent: Agent, to_agent: Agent
@@ -83,14 +81,13 @@ async def on_handoff(
8381
print(
8482
f"### {self.event_counter}: Handoff from '{from_agent.name}' to '{to_agent.name}'. Usage: {self._usage_to_str(context.usage)}"
8583
)
86-
if self.agent_span:
87-
with self.agent_span.start_child(
84+
current_span = sentry_sdk.get_current_span()
85+
if current_span:
86+
with current_span.start_child(
8887
op="gen_ai.handoff", description=f"{from_agent.name} > {to_agent.name}"
8988
):
9089
pass
91-
92-
self.agent_span.__exit__(None, None, None)
93-
self.agent_span = None
90+
current_span.__exit__(None, None, None)
9491

9592

9693
class OpenAIAgentsIntegration(Integration):

0 commit comments

Comments
 (0)