Skip to content

Commit 273a6af

Browse files
committed
Finish invoke span in case of errors
1 parent e7d1a74 commit 273a6af

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

sentry_sdk/integrations/openai_agents/patches/agent_run.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ async def patched_run_single_turn(
6868
cls: agents.Runner, *args: Any, **kwargs: Any
6969
) -> Any:
7070
"""Patched _run_single_turn that creates agent invocation spans"""
71-
7271
agent = kwargs.get("agent")
7372
context_wrapper = kwargs.get("context_wrapper")
7473
should_run_agent_start_hooks = kwargs.get("should_run_agent_start_hooks")
@@ -84,7 +83,11 @@ async def patched_run_single_turn(
8483
_start_invoke_agent_span(context_wrapper, agent)
8584

8685
# Call original method with all the correct parameters
87-
result = await original_run_single_turn(*args, **kwargs)
86+
try:
87+
result = await original_run_single_turn(*args, **kwargs)
88+
finally:
89+
if agent and context_wrapper and _has_active_agent_span(context_wrapper):
90+
_end_invoke_agent_span(context_wrapper, agent)
8891

8992
return result
9093

@@ -97,7 +100,6 @@ async def patched_execute_handoffs(
97100
cls: agents.Runner, *args: Any, **kwargs: Any
98101
) -> Any:
99102
"""Patched execute_handoffs that creates handoff spans and ends agent span for handoffs"""
100-
101103
context_wrapper = kwargs.get("context_wrapper")
102104
run_handoffs = kwargs.get("run_handoffs")
103105
agent = kwargs.get("agent")
@@ -128,7 +130,6 @@ async def patched_execute_final_output(
128130
cls: agents.Runner, *args: Any, **kwargs: Any
129131
) -> Any:
130132
"""Patched execute_final_output that ends agent span for final outputs"""
131-
132133
agent = kwargs.get("agent")
133134
context_wrapper = kwargs.get("context_wrapper")
134135
final_output = kwargs.get("final_output")

sentry_sdk/integrations/openai_agents/patches/runner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:
3333
_capture_exception(exc)
3434

3535
# It could be that there is a "invoke agent" span still open
36-
current_span = sentry_sdk.get_current_span()
37-
if current_span is not None and current_span.timestamp is None:
38-
current_span.__exit__(None, None, None)
36+
span = sentry_sdk.get_current_span()
37+
if span is not None and span.timestamp is None:
38+
span.__exit__(None, None, None)
3939

4040
raise exc from None
4141

sentry_sdk/integrations/openai_agents/spans/handoff.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313

1414
def handoff_span(
15-
context: agents.RunContextWrapper, from_agent: agents.Agent, to_agent_name: str
15+
context_wrapper: agents.RunContextWrapper,
16+
from_agent: agents.Agent,
17+
to_agent_name: str,
1618
) -> None:
1719
with sentry_sdk.start_span(
1820
op=OP.GEN_AI_HANDOFF,

sentry_sdk/integrations/openai_agents/spans/invoke_agent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
def invoke_agent_span(
17-
context: agents.RunContextWrapper, agent: agents.Agent
17+
context_wrapper: agents.RunContextWrapper, agent: agents.Agent
1818
) -> sentry_sdk.tracing.Span:
1919
span = sentry_sdk.start_span(
2020
op=OP.GEN_AI_INVOKE_AGENT,
@@ -27,15 +27,15 @@ def invoke_agent_span(
2727

2828
_set_agent_data(span, agent)
2929

30-
context._sentry_invoke_agent_span = span
30+
context_wrapper._sentry_invoke_agent_span = span
3131

3232
return span
3333

3434

3535
def update_invoke_agent_span(
36-
context: agents.RunContextWrapper, agent: agents.Agent, output: Any
36+
context_wrapper: agents.RunContextWrapper, agent: agents.Agent, output: Any
3737
) -> None:
38-
span = getattr(context, "_sentry_invoke_agent_span", None)
38+
span = getattr(context_wrapper, "_sentry_invoke_agent_span", None)
3939
if span is not None:
4040
span.__exit__(None, None, None)
41-
del context._sentry_invoke_agent_span
41+
del context_wrapper._sentry_invoke_agent_span

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,4 +586,4 @@ async def test_error_handling(sentry_init, capture_events, test_agent):
586586

587587
assert ai_client_span["description"] == "chat gpt-4"
588588
assert ai_client_span["origin"] == "auto.ai.openai_agents"
589-
assert ai_client_span["tags"]["status"] == "internal_error"
589+
assert ai_client_span["status"] == "internal_error"

0 commit comments

Comments
 (0)