Skip to content

Commit da30538

Browse files
authored
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. ## Checklist - [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)) ## Reviewer Checklist - [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)
1 parent adc56bd commit da30538

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

ddtrace/contrib/internal/openai/patch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def patched_endpoint(openai, pin, func, instance, args, kwargs):
262262
resp, err = None, None
263263
try:
264264
resp = func(*args, **kwargs)
265-
except Exception as e:
265+
except BaseException as e:
266266
err = e
267267
raise
268268
finally:
@@ -296,7 +296,7 @@ async def patched_endpoint(openai, pin, func, instance, args, kwargs):
296296
try:
297297
resp = await func(*args, **kwargs)
298298
return resp
299-
except Exception as e:
299+
except BaseException as e:
300300
err = e
301301
raise
302302
finally:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
openai: Resolves an issue in the OpenAI integration where ``asyncio.CancelledError`` was not caught or re-raised.

0 commit comments

Comments
 (0)