From 05ba2dfeaafaa4d5858d22f55fffc66a09c0028f Mon Sep 17 00:00:00 2001 From: Yun Kim Date: Tue, 12 Aug 2025 20:43:11 -0400 Subject: [PATCH 1/2] Fix for agent cancel error --- ddtrace/contrib/internal/openai/patch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ddtrace/contrib/internal/openai/patch.py b/ddtrace/contrib/internal/openai/patch.py index f60c0a1a107..69eb78bc26b 100644 --- a/ddtrace/contrib/internal/openai/patch.py +++ b/ddtrace/contrib/internal/openai/patch.py @@ -1,3 +1,4 @@ +import asyncio import os import sys from typing import Dict @@ -296,7 +297,7 @@ async def patched_endpoint(openai, pin, func, instance, args, kwargs): try: resp = await func(*args, **kwargs) return resp - except Exception as e: + except (Exception, asyncio.CancelledError) as e: err = e raise finally: From 36e414c833f821bfd3f0957d27e9b4c68c6e2341 Mon Sep 17 00:00:00 2001 From: Yun Kim Date: Wed, 13 Aug 2025 12:23:49 -0400 Subject: [PATCH 2/2] Catch and raise all baseexception --- ddtrace/contrib/internal/openai/patch.py | 5 ++--- .../fix-openai-catch-base-exception-a59c36fa8b0f40a0.yaml | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/fix-openai-catch-base-exception-a59c36fa8b0f40a0.yaml diff --git a/ddtrace/contrib/internal/openai/patch.py b/ddtrace/contrib/internal/openai/patch.py index 69eb78bc26b..aeafdfde55b 100644 --- a/ddtrace/contrib/internal/openai/patch.py +++ b/ddtrace/contrib/internal/openai/patch.py @@ -1,4 +1,3 @@ -import asyncio import os import sys from typing import Dict @@ -263,7 +262,7 @@ def patched_endpoint(openai, pin, func, instance, args, kwargs): resp, err = None, None try: resp = func(*args, **kwargs) - except Exception as e: + except BaseException as e: err = e raise finally: @@ -297,7 +296,7 @@ async def patched_endpoint(openai, pin, func, instance, args, kwargs): try: resp = await func(*args, **kwargs) return resp - except (Exception, asyncio.CancelledError) as e: + except BaseException as e: err = e raise finally: diff --git a/releasenotes/notes/fix-openai-catch-base-exception-a59c36fa8b0f40a0.yaml b/releasenotes/notes/fix-openai-catch-base-exception-a59c36fa8b0f40a0.yaml new file mode 100644 index 00000000000..da0266e9984 --- /dev/null +++ b/releasenotes/notes/fix-openai-catch-base-exception-a59c36fa8b0f40a0.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + openai: Resolves an issue in the OpenAI integration where ``asyncio.CancelledError`` was not caught or re-raised.