From 6749981469f9a72c7f6ed3c11702ec5ae85c84b1 Mon Sep 17 00:00:00 2001 From: Yun Kim <35776586+Yun-Kim@users.noreply.github.com> Date: Wed, 13 Aug 2025 13:20:35 -0400 Subject: [PATCH] fix(openai): catch and propagate asyncio.CancelledError (#14290) Resolves an issue where our openai integration did not catch/propagate asyncio CancelledError cases. In OpenAI Agents SDK, the OpenAI library is used internally to make LLM calls. However in a somewhat convoluted scenario, OpenAI Agents makes two LLM calls concurrently via `asyncio.gather(...)`, in which the first one can raise an error, which results in the second LLM call being cancelled sometime later. Our OpenAI integration did not catch this and continued executing and returning the underlying LLM call instead of raising, which caused an exception in the OpenAI Agents SDK which did not expect a None response (this should've cancelled and raised before it got to this point). This fix addresses this by directly raising if we notice the asyncio task has been cancelled. We're avoiding a repro here because it's 1) difficult to simulate a non-standard exception, and 2) the repro case involves using asyncio to simulate a cancelled task, which is not trivial. - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) (cherry picked from commit da305388c961584f59bc097b78cc03c5a21c1527) --- ddtrace/contrib/internal/openai/patch.py | 4 ++-- .../fix-openai-catch-base-exception-a59c36fa8b0f40a0.yaml | 4 ++++ 2 files changed, 6 insertions(+), 2 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 f60c0a1a107..aeafdfde55b 100644 --- a/ddtrace/contrib/internal/openai/patch.py +++ b/ddtrace/contrib/internal/openai/patch.py @@ -262,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: @@ -296,7 +296,7 @@ async def patched_endpoint(openai, pin, func, instance, args, kwargs): try: resp = await func(*args, **kwargs) return resp - except Exception 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.