Skip to content

Commit ff760d1

Browse files
committed
more resilient monkey patching
1 parent efe9e39 commit ff760d1

File tree

1 file changed

+21
-75
lines changed

1 file changed

+21
-75
lines changed

sentry_sdk/integrations/openai_agents/patches/agent_run.py

Lines changed: 21 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,14 @@ def _get_current_agent(context_wrapper):
5959
if hasattr(original_run_single_turn, "__func__")
6060
else original_run_single_turn
6161
)
62-
async def patched_run_single_turn(
63-
cls,
64-
*,
65-
agent,
66-
all_tools,
67-
original_input,
68-
generated_items,
69-
hooks,
70-
context_wrapper,
71-
run_config,
72-
should_run_agent_start_hooks,
73-
tool_use_tracker,
74-
previous_response_id,
75-
):
76-
# type: (agents.Agent, list[agents.Tool], Any, list[Any], list[Any], agents.RunContextWrapper, agents.RunConfig, bool, Any, Any, Optional[str]) -> Any
62+
async def patched_run_single_turn(cls, *args, **kwargs):
63+
# type: (agents.Runner, *Any, **Any) -> Any
7764
"""Patched _run_single_turn that creates agent invocation spans"""
7865

66+
agent = kwargs.get("agent")
67+
context_wrapper = kwargs.get("context_wrapper")
68+
should_run_agent_start_hooks = kwargs.get("should_run_agent_start_hooks")
69+
7970
# Start agent span when agent starts (but only once per agent)
8071
if should_run_agent_start_hooks and agent and context_wrapper:
8172
# End any existing span for a different agent
@@ -87,18 +78,7 @@ async def patched_run_single_turn(
8778
_start_invoke_agent_span(context_wrapper, agent)
8879

8980
# Call original method with all the correct parameters
90-
result = await original_run_single_turn(
91-
agent=agent,
92-
all_tools=all_tools,
93-
original_input=original_input,
94-
generated_items=generated_items,
95-
hooks=hooks,
96-
context_wrapper=context_wrapper,
97-
run_config=run_config,
98-
should_run_agent_start_hooks=should_run_agent_start_hooks,
99-
tool_use_tracker=tool_use_tracker,
100-
previous_response_id=previous_response_id,
101-
)
81+
result = await original_run_single_turn(*args, **kwargs)
10282

10383
return result
10484

@@ -107,40 +87,22 @@ async def patched_run_single_turn(
10787
if hasattr(original_execute_handoffs, "__func__")
10888
else original_execute_handoffs
10989
)
110-
async def patched_execute_handoffs(
111-
cls,
112-
*,
113-
agent,
114-
original_input,
115-
pre_step_items,
116-
new_step_items,
117-
new_response,
118-
run_handoffs,
119-
hooks,
120-
context_wrapper,
121-
run_config,
122-
):
123-
# type: (Any, agents.Agent, Any, list[Any], list[Any], Any, list[Any], list[Any], agents.RunContextWrapper, agents.RunConfig) -> Any
90+
async def patched_execute_handoffs(cls, *args, **kwargs):
91+
# type: (agents.Runner, *Any, **Any) -> Any
12492
"""Patched execute_handoffs that creates handoff spans and ends agent span for handoffs"""
12593

94+
context_wrapper = kwargs.get("context_wrapper")
95+
run_handoffs = kwargs.get("run_handoffs")
96+
agent = kwargs.get("agent")
97+
12698
# Create Sentry handoff span for the first handoff (agents library only processes the first one)
12799
if run_handoffs:
128100
first_handoff = run_handoffs[0]
129101
handoff_agent_name = first_handoff.handoff.agent_name
130102
handoff_span(context_wrapper, agent, handoff_agent_name)
131103

132104
# Call original method with all parameters
133-
result = await original_execute_handoffs(
134-
agent=agent,
135-
original_input=original_input,
136-
pre_step_items=pre_step_items,
137-
new_step_items=new_step_items,
138-
new_response=new_response,
139-
run_handoffs=run_handoffs,
140-
hooks=hooks,
141-
context_wrapper=context_wrapper,
142-
run_config=run_config,
143-
)
105+
result = await original_execute_handoffs(*args, **kwargs)
144106

145107
# End span for current agent after handoff processing is complete
146108
if agent and context_wrapper and _has_active_agent_span(context_wrapper):
@@ -153,32 +115,16 @@ async def patched_execute_handoffs(
153115
if hasattr(original_execute_final_output, "__func__")
154116
else original_execute_final_output
155117
)
156-
async def patched_execute_final_output(
157-
cls,
158-
*,
159-
agent,
160-
original_input,
161-
new_response,
162-
pre_step_items,
163-
new_step_items,
164-
final_output,
165-
hooks,
166-
context_wrapper,
167-
):
168-
# type: (Any, agents.Agent, Any, Any, list[Any], list[Any], Any, list[Any], agents.RunContextWrapper) -> Any
118+
async def patched_execute_final_output(cls, *args, **kwargs):
119+
# type: (agents.Runner, *Any, **Any) -> Any
169120
"""Patched execute_final_output that ends agent span for final outputs"""
170121

122+
agent = kwargs.get("agent")
123+
context_wrapper = kwargs.get("context_wrapper")
124+
final_output = kwargs.get("final_output")
125+
171126
# Call original method with all parameters
172-
result = await original_execute_final_output(
173-
agent=agent,
174-
original_input=original_input,
175-
new_response=new_response,
176-
pre_step_items=pre_step_items,
177-
new_step_items=new_step_items,
178-
final_output=final_output,
179-
hooks=hooks,
180-
context_wrapper=context_wrapper,
181-
)
127+
result = await original_execute_final_output(*args, **kwargs)
182128

183129
# End span for current agent after final output processing is complete
184130
if agent and context_wrapper and _has_active_agent_span(context_wrapper):

0 commit comments

Comments
 (0)