Skip to content

Commit

Permalink
fix(openai): support Python 3.12, 3.13 [backport #12014 to 2.19] (#12083
Browse files Browse the repository at this point in the history
)

Backports #12014 to 2.19.

Resolves #11994.

We were previously not testing our OpenAI integration with Python
versions >= 3.12 (at the time, OpenAI did not support those newer Python
versions). We recently saw that our bytecode wrapping for OpenAI methods
broke in Python 3.13, so this PR attempts to address that:
- Switch out our bytecode wrapping with `wrapt.wrap_function_wrapper()`
to make our patching compatible with newer Python versions.
- Add Python 3.12 and 3.13 to tested versions for the OpenAI
integration.
- Implements unpatching and adds patching tests

We previously were using direct bytecode wrapping in the OpenAI
integration, which was not supported for Python 3.13, and instead
switched to using `wrapt.wrap_function_wrapper()` to wrap OpenAI
methods. This meant that we needed to change wrapping formats,
including:
- use function/attribute names (string) rather than references as inputs
to the wrapping functions
- replace nested functions with `@with_traced_module()` to pass the
traced OpenAI module reference between traced methods
- implement unpatching
- add patching tests (note this is messy because we support both v0 and
v1 openai versions which have separate method names)

**Note**: the issue in #11994 was only reproducible via the
`AsyncOpenAI.Moderations.create(), AsyncOpenAI.Embeddings.create()`
endpoints (chat and completions were not affected). However to avoid any
risk introduced by the now unsupported bytecode wrapping, we are
changing the entire OpenAI integration to use
`wrapt.wrap_function_wrapper()` instead.

Since ddtrace 3.0 will be removing support for Python 3.7, we are also
dropping support accordingly in this PR. This also coincides with
removing support for OpenAI v0 since v0 was last released more than a 16
months ago, and requires a large maintenance burden and pollutes the
codebase. Note that this PR will just drop testing support, but a future
PR will remove tracing support for v0 in the form of a refactor/cleanup
of the OpenAI integration.

Azure OpenAI requests are changed in latest versions of OpenAI, which
fail our cassette tests (recorded in previous versions of OpenAI).
However on manual testing, our openai integration is unchanged in
support for newer versions. With consideration for time/effort to
produce new cassette test files which is time consuming, we are going to
skip cassette tests for Azure endpoints with latest openai versions
until we improve our testing framework entirely (move off cassette
files). Here are the manual traces submitted with Azure OpenAI for
reference:
<img width="578" alt="Screenshot 2025-01-23 at 2 20 00 PM"
src="https://github.com/user-attachments/assets/19f34edc-4c20-4cfd-804f-3e7bce26f4df"
/>
<img width="545" alt="Screenshot 2025-01-23 at 2 20 14 PM"
src="https://github.com/user-attachments/assets/a58bc888-6bee-4d67-9fda-0661aed14423"
/>

## 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)
  • Loading branch information
Yun-Kim authored Jan 24, 2025
1 parent 87a5c75 commit 067d6a7
Show file tree
Hide file tree
Showing 33 changed files with 1,094 additions and 965 deletions.
54 changes: 54 additions & 0 deletions .riot/requirements/107d8f2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/107d8f2.in
#
annotated-types==0.7.0
anyio==4.8.0
attrs==24.3.0
certifi==2024.12.14
coverage[toml]==7.6.10
distro==1.9.0
exceptiongroup==1.2.2
h11==0.14.0
httpcore==1.0.7
httpx==0.27.2
hypothesis==6.45.0
idna==3.10
importlib-metadata==8.6.1
iniconfig==2.0.0
mock==5.1.0
multidict==6.1.0
numpy==2.0.2
openai[datalib,embeddings]==1.30.1
opentracing==2.4.0
packaging==24.2
pandas==2.2.3
pandas-stubs==2.2.2.240807
pillow==9.5.0
pluggy==1.5.0
propcache==0.2.1
pydantic==2.10.5
pydantic-core==2.27.2
pytest==8.3.4
pytest-asyncio==0.21.1
pytest-cov==6.0.0
pytest-mock==3.14.0
pytest-randomly==3.16.0
python-dateutil==2.9.0.post0
pytz==2024.2
pyyaml==6.0.2
six==1.17.0
sniffio==1.3.1
sortedcontainers==2.4.0
tomli==2.2.1
tqdm==4.67.1
types-pytz==2024.2.0.20241221
typing-extensions==4.12.2
tzdata==2025.1
urllib3==1.26.20
vcrpy==4.2.1
wrapt==1.17.2
yarl==1.18.3
zipp==3.21.0
48 changes: 48 additions & 0 deletions .riot/requirements/130158f.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile --no-annotate .riot/requirements/130158f.in
#
annotated-types==0.7.0
anyio==4.8.0
attrs==24.3.0
certifi==2024.12.14
charset-normalizer==3.4.1
coverage[toml]==7.6.10
distro==1.9.0
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
hypothesis==6.45.0
idna==3.10
iniconfig==2.0.0
jiter==0.8.2
mock==5.1.0
multidict==6.1.0
openai==1.60.0
opentracing==2.4.0
packaging==24.2
pillow==11.1.0
pluggy==1.5.0
propcache==0.2.1
pydantic==2.10.5
pydantic-core==2.27.2
pytest==8.3.4
pytest-asyncio==0.21.1
pytest-cov==6.0.0
pytest-mock==3.14.0
pytest-randomly==3.16.0
pyyaml==6.0.2
regex==2024.11.6
requests==2.32.3
six==1.17.0
sniffio==1.3.1
sortedcontainers==2.4.0
tiktoken==0.8.0
tqdm==4.67.1
typing-extensions==4.12.2
urllib3==1.26.20
vcrpy==4.2.1
wrapt==1.17.2
yarl==1.18.3
57 changes: 0 additions & 57 deletions .riot/requirements/13804af.txt

This file was deleted.

49 changes: 0 additions & 49 deletions .riot/requirements/13fec34.txt

This file was deleted.

12 changes: 6 additions & 6 deletions .riot/requirements/1825740.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ aiosignal==1.3.1
async-timeout==4.0.3
asynctest==0.13.0
attrs==24.2.0
certifi==2024.7.4
charset-normalizer==3.3.2
certifi==2024.12.14
charset-normalizer==3.4.1
coverage[toml]==7.2.7
exceptiongroup==1.2.2
frozenlist==1.3.3
hypothesis==6.45.0
idna==3.8
idna==3.10
importlib-metadata==6.7.0
iniconfig==2.0.0
joblib==1.3.2
Expand All @@ -36,13 +36,13 @@ pyyaml==6.0.1
requests==2.31.0
scikit-learn==1.0.2
scipy==1.7.3
six==1.16.0
six==1.17.0
sortedcontainers==2.4.0
threadpoolctl==3.1.0
tomli==2.0.1
tqdm==4.66.5
tqdm==4.67.1
typing-extensions==4.7.1
urllib3==1.26.19
urllib3==1.26.20
vcrpy==4.2.1
wrapt==1.16.0
yarl==1.9.4
Expand Down
52 changes: 52 additions & 0 deletions .riot/requirements/18de44f.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# This file is autogenerated by pip-compile with Python 3.8
# by the following command:
#
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/18de44f.in
#
annotated-types==0.7.0
anyio==4.5.2
attrs==24.3.0
certifi==2024.12.14
charset-normalizer==3.4.1
coverage[toml]==7.6.1
distro==1.9.0
exceptiongroup==1.2.2
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
hypothesis==6.45.0
idna==3.10
importlib-metadata==8.5.0
iniconfig==2.0.0
jiter==0.8.2
mock==5.1.0
multidict==6.1.0
openai==1.60.0
opentracing==2.4.0
packaging==24.2
pillow==10.4.0
pluggy==1.5.0
propcache==0.2.0
pydantic==2.10.5
pydantic-core==2.27.2
pytest==8.3.4
pytest-asyncio==0.21.1
pytest-cov==5.0.0
pytest-mock==3.14.0
pytest-randomly==3.15.0
pyyaml==6.0.2
regex==2024.11.6
requests==2.32.3
six==1.17.0
sniffio==1.3.1
sortedcontainers==2.4.0
tiktoken==0.7.0
tomli==2.2.1
tqdm==4.67.1
typing-extensions==4.12.2
urllib3==1.26.20
vcrpy==4.2.1
wrapt==1.17.2
yarl==1.15.2
zipp==3.20.2
50 changes: 50 additions & 0 deletions .riot/requirements/1ad89c5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --no-annotate .riot/requirements/1ad89c5.in
#
annotated-types==0.7.0
anyio==4.8.0
attrs==24.3.0
certifi==2024.12.14
charset-normalizer==3.4.1
coverage[toml]==7.6.10
distro==1.9.0
exceptiongroup==1.2.2
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
hypothesis==6.45.0
idna==3.10
iniconfig==2.0.0
jiter==0.8.2
mock==5.1.0
multidict==6.1.0
openai==1.60.0
opentracing==2.4.0
packaging==24.2
pillow==11.1.0
pluggy==1.5.0
propcache==0.2.1
pydantic==2.10.5
pydantic-core==2.27.2
pytest==8.3.4
pytest-asyncio==0.21.1
pytest-cov==6.0.0
pytest-mock==3.14.0
pytest-randomly==3.16.0
pyyaml==6.0.2
regex==2024.11.6
requests==2.32.3
six==1.17.0
sniffio==1.3.1
sortedcontainers==2.4.0
tiktoken==0.8.0
tomli==2.2.1
tqdm==4.67.1
typing-extensions==4.12.2
urllib3==1.26.20
vcrpy==4.2.1
wrapt==1.17.2
yarl==1.18.3
Loading

0 comments on commit 067d6a7

Please sign in to comment.