Commit 5316f0d
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)
(cherry picked from commit da30538)1 parent ba9a459 commit 5316f0d
File tree
2 files changed
+6
-2
lines changed- ddtrace/contrib/internal/openai
- releasenotes/notes
2 files changed
+6
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
262 | 262 | | |
263 | 263 | | |
264 | 264 | | |
265 | | - | |
| 265 | + | |
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
| |||
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
299 | | - | |
| 299 | + | |
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
0 commit comments