11from sentry_sdk .integrations import DidNotEnable , Integration
2+ from sentry_sdk .utils import parse_version
23
34from .patches import (
4- _create_get_model_wrapper ,
5- _create_get_all_tools_wrapper ,
5+ _create_runner_get_model_wrapper ,
6+ _create_run_internal_get_model_wrapper ,
7+ _create_runner_get_all_tools_wrapper ,
8+ _create_run_internal_get_all_tools_wrapper ,
69 _create_run_wrapper ,
710 _create_run_streamed_wrapper ,
8- _patch_agent_run ,
11+ _patch_agent_runner_run_single_turn ,
12+ _patch_run_loop_run_single_turn ,
13+ _patch_agent_runner_run_single_turn_streamed ,
14+ _patch_run_loop_run_single_turn_streamed ,
15+ _patch_run_impl_execute_handoffs ,
16+ _patch_turn_resolution_execute_handoffs ,
17+ _patch_run_impl_execute_final_output ,
18+ _patch_turn_resolution_execute_final_output ,
919 _patch_error_tracing ,
1020)
1121
1727 # after it, even if we don't use it.
1828 import agents
1929 from agents .run import DEFAULT_AGENT_RUNNER
30+ from agents .version import __version__ as OPENAI_AGENTS_VERSION
2031
2132except ImportError :
2233 raise DidNotEnable ("OpenAI Agents not installed" )
2334
2435
25- def _patch_runner () -> None :
26- # Create the root span for one full agent run (including eventual handoffs)
27- # Note agents.run.DEFAULT_AGENT_RUNNER.run_sync is a wrapper around
28- # agents.run.DEFAULT_AGENT_RUNNER.run. It does not need to be wrapped separately.
29- agents . run . DEFAULT_AGENT_RUNNER . run = _create_run_wrapper (
30- agents . run . DEFAULT_AGENT_RUNNER . run
31- )
36+ try :
37+ # AgentRunner methods moved in v0.8
38+ # https://github.com/openai/openai-agents-python/commit/3ce7c24d349b77bb750062b7e0e856d9ff48a5d5#diff-7470b3a5c5cbe2fcbb2703dc24f326f45a5819d853be2b1f395d122d278cd911
39+ from agents .run_internal import run_loop , turn_preparation
40+ except ImportError :
41+ run_loop = None
42+ turn_preparation = None
3243
33- # Patch streaming runner
34- agents .run .DEFAULT_AGENT_RUNNER .run_streamed = _create_run_streamed_wrapper (
35- agents .run .DEFAULT_AGENT_RUNNER .run_streamed
36- )
3744
38- # Creating the actual spans for each agent run (works for both streaming and non-streaming).
39- _patch_agent_run ()
45+ def _patch_agent_runner_get_model () -> None :
46+ agents .run .AgentRunner ._get_model = classmethod (
47+ _create_runner_get_model_wrapper (agents .run .AgentRunner ._get_model ),
48+ )
4049
4150
42- def _patch_model () -> None :
43- agents .run . AgentRunner . _get_model = classmethod (
44- _create_get_model_wrapper ( agents . run . AgentRunner . _get_model ),
51+ def _patch_run_internal_get_model () :
52+ agents .run_internal . run_loop . get_model = _create_run_internal_get_model_wrapper (
53+ turn_preparation . get_model
4554 )
4655
4756
48- def _patch_tools () -> None :
57+ def _patch_agent_runner_get_all_tools () -> None :
4958 agents .run .AgentRunner ._get_all_tools = classmethod (
50- _create_get_all_tools_wrapper (agents .run .AgentRunner ._get_all_tools ),
59+ _create_runner_get_all_tools_wrapper (agents .run .AgentRunner ._get_all_tools ),
60+ )
61+
62+
63+ def _patch_run_get_all_tools () -> None :
64+ agents .run .get_all_tools = _create_run_internal_get_all_tools_wrapper (
65+ run_loop .get_all_tools
5166 )
5267
5368
@@ -56,7 +71,38 @@ class OpenAIAgentsIntegration(Integration):
5671
5772 @staticmethod
5873 def setup_once () -> None :
74+ # Create the root span for one full agent run (including eventual handoffs)
75+ # Note agents.run.DEFAULT_AGENT_RUNNER.run_sync is a wrapper around
76+ # agents.run.DEFAULT_AGENT_RUNNER.run. It does not need to be wrapped separately.
77+ agents .run .DEFAULT_AGENT_RUNNER .run = _create_run_wrapper (
78+ agents .run .DEFAULT_AGENT_RUNNER .run
79+ )
80+
81+ # Patch streaming runner
82+ agents .run .DEFAULT_AGENT_RUNNER .run_streamed = _create_run_streamed_wrapper (
83+ agents .run .DEFAULT_AGENT_RUNNER .run_streamed
84+ )
85+
86+ if parse_version (OPENAI_AGENTS_VERSION ) >= (
87+ 0 ,
88+ 8 ,
89+ ):
90+ _patch_error_tracing ()
91+ _patch_run_get_all_tools ()
92+ _patch_run_internal_get_model ()
93+
94+ _patch_run_loop_run_single_turn ()
95+ _patch_run_loop_run_single_turn_streamed ()
96+ _patch_turn_resolution_execute_handoffs ()
97+ _patch_turn_resolution_execute_final_output ()
98+
99+ return
100+
59101 _patch_error_tracing ()
60- _patch_tools ()
61- _patch_model ()
62- _patch_runner ()
102+ _patch_agent_runner_get_all_tools ()
103+ _patch_agent_runner_get_model ()
104+
105+ _patch_agent_runner_run_single_turn ()
106+ _patch_agent_runner_run_single_turn_streamed ()
107+ _patch_run_impl_execute_handoffs ()
108+ _patch_run_impl_execute_final_output ()
0 commit comments