diff --git a/sentry_sdk/integrations/openai_agents/patches/models.py b/sentry_sdk/integrations/openai_agents/patches/models.py index feaa0c33d2..8431ff3237 100644 --- a/sentry_sdk/integrations/openai_agents/patches/models.py +++ b/sentry_sdk/integrations/openai_agents/patches/models.py @@ -1,3 +1,4 @@ +import copy from functools import wraps from sentry_sdk.integrations import DidNotEnable @@ -31,8 +32,9 @@ def _create_get_model_wrapper(original_get_model): def wrapped_get_model(cls, agent, run_config): # type: (agents.Runner, agents.Agent, agents.RunConfig) -> agents.Model - model = original_get_model(agent, run_config) - original_get_response = model.get_response + # copy the model to double patching its methods. We use copy on purpose here (instead of deepcopy) + # because we only patch its direct methods, all underlying data can remain unchanged. + model = copy.copy(original_get_model(agent, run_config)) # Wrap _fetch_response if it exists (for OpenAI models) to capture raw response model if hasattr(model, "_fetch_response"): @@ -48,6 +50,8 @@ async def wrapped_fetch_response(*args, **kwargs): model._fetch_response = wrapped_fetch_response + original_get_response = model.get_response + @wraps(original_get_response) async def wrapped_get_response(*args, **kwargs): # type: (*Any, **Any) -> Any