Skip to content

Commit e738f3d

Browse files
move end_invoke_agent_span
1 parent e5d5c52 commit e738f3d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

sentry_sdk/integrations/openai_agents/patches/agent_run.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
from sentry_sdk.integrations import DidNotEnable
44
from sentry_sdk.tracing_utils import set_span_errored
5-
from ..spans import invoke_agent_span, update_invoke_agent_span, handoff_span
5+
from ..spans import (
6+
invoke_agent_span,
7+
update_invoke_agent_span,
8+
end_invoke_agent_span,
9+
handoff_span,
10+
)
611
from ..utils import _capture_exception, _record_exception_on_span
712

813
from typing import TYPE_CHECKING
@@ -78,7 +83,7 @@ async def patched_run_single_turn(cls, *args, **kwargs):
7883
if _has_active_agent_span(context_wrapper):
7984
current_agent = _get_current_agent(context_wrapper)
8085
if current_agent and current_agent != agent:
81-
_end_invoke_agent_span(context_wrapper, current_agent)
86+
end_invoke_agent_span(context_wrapper, current_agent)
8287

8388
span = _start_invoke_agent_span(context_wrapper, agent, kwargs)
8489

@@ -88,7 +93,7 @@ async def patched_run_single_turn(cls, *args, **kwargs):
8893
except Exception as exc:
8994
if span is not None and span.timestamp is None:
9095
_record_exception_on_span(span, exc)
91-
_end_invoke_agent_span(context_wrapper, agent)
96+
end_invoke_agent_span(context_wrapper, agent)
9297

9398
_capture_exception(exc)
9499
raise exc from None

sentry_sdk/integrations/openai_agents/spans/invoke_agent.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ def update_invoke_agent_span(context, agent, output):
8585

8686
span.__exit__(None, None, None)
8787
delattr(context, "_sentry_agent_span")
88+
89+
90+
def end_invoke_agent_span(context_wrapper, agent, output=None):
91+
# type: (agents.RunContextWrapper, agents.Agent, Optional[Any]) -> None
92+
"""End the agent invocation span"""
93+
# Clear the stored agent
94+
if hasattr(context_wrapper, "_sentry_current_agent"):
95+
delattr(context_wrapper, "_sentry_current_agent")
96+
97+
update_invoke_agent_span(context_wrapper, agent, output)

0 commit comments

Comments
 (0)