Skip to content

Commit 3eac23c

Browse files
fix(integrations): openai-agents fix multi-patching of get_model function (#5195)
### Description When passing in an explicit model instance instead of a string the model was patched multiple times event if it was already patched before. This PR prohibits that. #### Issues Contributes to: https://linear.app/getsentry/issue/TET-1511/openai-agents-double-span-reporting
1 parent 9b80198 commit 3eac23c

File tree

1 file changed

+6
-2
lines changed
  • sentry_sdk/integrations/openai_agents/patches

1 file changed

+6
-2
lines changed

sentry_sdk/integrations/openai_agents/patches/models.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
from functools import wraps
23

34
from sentry_sdk.integrations import DidNotEnable
@@ -31,8 +32,9 @@ def _create_get_model_wrapper(original_get_model):
3132
def wrapped_get_model(cls, agent, run_config):
3233
# type: (agents.Runner, agents.Agent, agents.RunConfig) -> agents.Model
3334

34-
model = original_get_model(agent, run_config)
35-
original_get_response = model.get_response
35+
# copy the model to double patching its methods. We use copy on purpose here (instead of deepcopy)
36+
# because we only patch its direct methods, all underlying data can remain unchanged.
37+
model = copy.copy(original_get_model(agent, run_config))
3638

3739
# Wrap _fetch_response if it exists (for OpenAI models) to capture raw response model
3840
if hasattr(model, "_fetch_response"):
@@ -48,6 +50,8 @@ async def wrapped_fetch_response(*args, **kwargs):
4850

4951
model._fetch_response = wrapped_fetch_response
5052

53+
original_get_response = model.get_response
54+
5155
@wraps(original_get_response)
5256
async def wrapped_get_response(*args, **kwargs):
5357
# type: (*Any, **Any) -> Any

0 commit comments

Comments
 (0)