Skip to content

Commit 4dbf22a

Browse files
committed
fix: always copy() the model we get from get_model() to avoid wrong agent reference in closure
1 parent 0bd8fba commit 4dbf22a

File tree

1 file changed

+4
-8
lines changed
  • sentry_sdk/integrations/openai_agents/patches

1 file changed

+4
-8
lines changed

sentry_sdk/integrations/openai_agents/patches/models.py

Lines changed: 4 additions & 8 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,11 +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-
36-
# check if we have already patched this model
37-
if getattr(model, "_sentry_wrapped_get_model", False):
38-
return model
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))
3938

4039
# Wrap _fetch_response if it exists (for OpenAI models) to capture raw response model
4140
if hasattr(model, "_fetch_response"):
@@ -75,9 +74,6 @@ async def wrapped_get_response(*args, **kwargs):
7574

7675
model.get_response = wrapped_get_response
7776

78-
# set marker that we have already patched this model
79-
model._sentry_wrapped_get_model = True
80-
8177
return model
8278

8379
return wrapped_get_model

0 commit comments

Comments
 (0)