Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(openai): wrap streamed response #9321

Merged
merged 6 commits into from
May 21, 2024
Merged

Conversation

Yun-Kim
Copy link
Contributor

@Yun-Kim Yun-Kim commented May 20, 2024

Fixes #9077.

This PR changes the way we patch streamed completion/chat responses in the OpenAI integration. Previously we had been returning generator functions that yielded the value of each chunk. However, this was incorrect as OpenAIStream/OpenAIAsyncStream objects can be used as context managers as of openai>=1.6.0, which means returning a generator function to replace OpenAIStream would break user applications that tried to access stream responses as context managers:

with openai.OpenAI().completions.create(..., stream=True) as response:
    for chunk in response:
        print(chunk)

There is no introduced/changed functionality other than the fact that we now returned a wrapped TracedOpenAIStream object instead of the previous traced generator function.

Note: This PR may seem much larger than it really is, we also updated the tested versions of OpenAI so the riot requirement lockfiles have been changed.

Checklist

  • Change(s) are motivated and described in the PR description
  • Testing strategy is described if automated tests are not included in the PR
  • Risks are described (performance impact, potential for breakage, maintainability)
  • Change is maintainable (easy to change, telemetry, documentation)
  • Library release note guidelines are followed or label changelog/no-changelog is set
  • Documentation is included (in-code, generated user docs, public corp docs)
  • Backport labels are set (if applicable)
  • If this PR changes the public interface, I've notified @DataDog/apm-tees.

Reviewer Checklist

  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Description motivates each change
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Change is maintainable (easy to change, telemetry, documentation)
  • Release note makes sense to a user of the library
  • 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

@Yun-Kim Yun-Kim requested review from a team as code owners May 20, 2024 20:52
@datadog-dd-trace-py-rkomorn
Copy link

datadog-dd-trace-py-rkomorn bot commented May 20, 2024

Datadog Report

Branch report: yunkim/openai-fix-streamed-responses
Commit report: 0182cc7
Test service: dd-trace-py

✅ 0 Failed, 118389 Passed, 57257 Skipped, 4h 26m 34.21s Total duration (4h 58m 54.85s time saved)
❄️ 1 New Flaky

New Flaky Tests (1)

  • test_tracer_hooks - test_processor.py - Last Failure

    Expand for error
     STDERR: Expected [] got [b'sent 389B in 2.34969s to http://localhost:9126/v0.5/traces\n']
    

@Yun-Kim Yun-Kim force-pushed the yunkim/openai-fix-streamed-responses branch from 0cfc888 to cd78037 Compare May 20, 2024 20:59
@Yun-Kim Yun-Kim requested a review from mabdinur May 20, 2024 21:08
@pr-commenter
Copy link

pr-commenter bot commented May 20, 2024

Benchmarks

Benchmark execution time: 2024-05-21 20:21:00

Comparing candidate commit 1fa5dcb in PR branch yunkim/openai-fix-streamed-responses with baseline commit 52b0c59 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 201 metrics, 9 unstable metrics.

@Yun-Kim Yun-Kim requested a review from a team as a code owner May 20, 2024 22:13
@Yun-Kim Yun-Kim requested a review from erikayasuda May 20, 2024 22:13
@Yun-Kim Yun-Kim force-pushed the yunkim/openai-fix-streamed-responses branch from b3f33b8 to 4603144 Compare May 20, 2024 22:19
@Yun-Kim Yun-Kim force-pushed the yunkim/openai-fix-streamed-responses branch from 4603144 to 5ec9cec Compare May 20, 2024 22:24
Copy link
Contributor

@lievan lievan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I think we also need to implement __next__ in TracedOpenAIStream/AsyncStream

Currently getting:

    chunk1 = next(resp)
TypeError: 'TracedOpenAIStream' object is not an iterator

ddtrace/contrib/openai/utils.py Show resolved Hide resolved
Yun-Kim added a commit that referenced this pull request May 21, 2024
Unblocks #9321.

This PR removes patching for the `edits` and `fine tunes` OpenAI API
endpoints. These endpoints have been removed entirely from the OpenAI
API as of January 2024, and are not available to users even when using
older versions of the OpenAI SDK / API.

Things removed by this PR:
- Patch code for removed endpoints
- Tests (`test_openai_v0.py`, `test_openai_v1.py`)
- Snapshot files for removed tests
- Cassette files for removed tests

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] 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)
github-actions bot pushed a commit that referenced this pull request May 21, 2024
Unblocks #9321.

This PR removes patching for the `edits` and `fine tunes` OpenAI API
endpoints. These endpoints have been removed entirely from the OpenAI
API as of January 2024, and are not available to users even when using
older versions of the OpenAI SDK / API.

Things removed by this PR:
- Patch code for removed endpoints
- Tests (`test_openai_v0.py`, `test_openai_v1.py`)
- Snapshot files for removed tests
- Cassette files for removed tests

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] 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 52b0c59)
github-actions bot pushed a commit that referenced this pull request May 21, 2024
Unblocks #9321.

This PR removes patching for the `edits` and `fine tunes` OpenAI API
endpoints. These endpoints have been removed entirely from the OpenAI
API as of January 2024, and are not available to users even when using
older versions of the OpenAI SDK / API.

Things removed by this PR:
- Patch code for removed endpoints
- Tests (`test_openai_v0.py`, `test_openai_v1.py`)
- Snapshot files for removed tests
- Cassette files for removed tests

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] 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 52b0c59)
@Yun-Kim Yun-Kim merged commit 0e8a257 into main May 21, 2024
193 of 197 checks passed
@Yun-Kim Yun-Kim deleted the yunkim/openai-fix-streamed-responses branch May 21, 2024 20:44
Copy link

The backport to 2.8 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.8 2.8
# Navigate to the new working tree
cd .worktrees/backport-2.8
# Create a new branch
git switch --create backport-9321-to-2.8
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 0e8a2579af9b8d28e7c8c39ece51c506417708b0
# Push it to GitHub
git push --set-upstream origin backport-9321-to-2.8
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.8

Then, create a pull request where the base branch is 2.8 and the compare/head branch is backport-9321-to-2.8.

Copy link

The backport to 2.9 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.9 2.9
# Navigate to the new working tree
cd .worktrees/backport-2.9
# Create a new branch
git switch --create backport-9321-to-2.9
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 0e8a2579af9b8d28e7c8c39ece51c506417708b0
# Push it to GitHub
git push --set-upstream origin backport-9321-to-2.9
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.9

Then, create a pull request where the base branch is 2.9 and the compare/head branch is backport-9321-to-2.9.

Yun-Kim added a commit that referenced this pull request May 21, 2024
…8] (#9337)

Backport 52b0c59 from #9331 to 2.8.

Unblocks #9321.

This PR removes patching for the `edits` and `fine tunes` OpenAI API
endpoints. These endpoints have been removed entirely from the OpenAI
API as of January 2024, and are not available to users even when using
older versions of the OpenAI SDK / API.

Things removed by this PR:
- Patch code for removed endpoints
- Tests (`test_openai_v0.py`, `test_openai_v1.py`)
- Snapshot files for removed tests
- Cassette files for removed tests

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] 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)

Co-authored-by: Yun Kim <[email protected]>
Yun-Kim added a commit that referenced this pull request May 21, 2024
…9] (#9338)

Backport 52b0c59 from #9331 to 2.9.

Unblocks #9321.

This PR removes patching for the `edits` and `fine tunes` OpenAI API
endpoints. These endpoints have been removed entirely from the OpenAI
API as of January 2024, and are not available to users even when using
older versions of the OpenAI SDK / API.

Things removed by this PR:
- Patch code for removed endpoints
- Tests (`test_openai_v0.py`, `test_openai_v1.py`)
- Snapshot files for removed tests
- Cassette files for removed tests

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] 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)

Co-authored-by: Yun Kim <[email protected]>
Copy link

The backport to 2.8 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.8 2.8
# Navigate to the new working tree
cd .worktrees/backport-2.8
# Create a new branch
git switch --create backport-9321-to-2.8
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 0e8a2579af9b8d28e7c8c39ece51c506417708b0
# Push it to GitHub
git push --set-upstream origin backport-9321-to-2.8
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.8

Then, create a pull request where the base branch is 2.8 and the compare/head branch is backport-9321-to-2.8.

Copy link

The backport to 2.9 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.9 2.9
# Navigate to the new working tree
cd .worktrees/backport-2.9
# Create a new branch
git switch --create backport-9321-to-2.9
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 0e8a2579af9b8d28e7c8c39ece51c506417708b0
# Push it to GitHub
git push --set-upstream origin backport-9321-to-2.9
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.9

Then, create a pull request where the base branch is 2.9 and the compare/head branch is backport-9321-to-2.9.

Yun-Kim added a commit that referenced this pull request May 22, 2024
Backport #9321 to 2.9

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] 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)

Co-authored-by: Yun Kim <[email protected]>
Yun-Kim added a commit that referenced this pull request May 22, 2024
Backport #9321 to 2.8

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] 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)

Co-authored-by: Yun Kim <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Asynchronous context manager crash with langchain-openai >= 0.1.2
3 participants