diff --git a/sentry_sdk/integrations/openai_agents/patches/models.py b/sentry_sdk/integrations/openai_agents/patches/models.py index feaa0c33d2..f13ad8c862 100644 --- a/sentry_sdk/integrations/openai_agents/patches/models.py +++ b/sentry_sdk/integrations/openai_agents/patches/models.py @@ -32,7 +32,10 @@ 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 + + # check if we have already patched this model + if getattr(model, "_sentry_wrapped_get_model", False): + return model # Wrap _fetch_response if it exists (for OpenAI models) to capture raw response model if hasattr(model, "_fetch_response"): @@ -48,6 +51,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 @@ -70,6 +75,9 @@ async def wrapped_get_response(*args, **kwargs): model.get_response = wrapped_get_response + # set marker that we have already patched this model + model._sentry_wrapped_get_model = True + return model return wrapped_get_model