-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(core): Intercept .withResponse() to preserve OpenAI stream instru… #19122
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
Open
RulaKhaled
wants to merge
1
commit into
develop
Choose a base branch
from
rolaabuhasna/js-1594-sentry-openai-instrumentation-breaks-withresponse
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+257
−66
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
dev-packages/node-integration-tests/suites/tracing/openai/scenario-with-response.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
| import OpenAI from 'openai'; | ||
|
|
||
| function startMockServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/openai/chat/completions', (req, res) => { | ||
| const { model } = req.body; | ||
|
|
||
| res.set({ | ||
| 'x-request-id': 'req_withresponse_test', | ||
| 'openai-organization': 'test-org', | ||
| 'openai-processing-ms': '150', | ||
| 'openai-version': '2020-10-01', | ||
| }); | ||
|
|
||
| res.send({ | ||
| id: 'chatcmpl-withresponse', | ||
| object: 'chat.completion', | ||
| created: 1677652288, | ||
| model: model, | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { | ||
| role: 'assistant', | ||
| content: 'Testing .withResponse() method!', | ||
| }, | ||
| finish_reason: 'stop', | ||
| }, | ||
| ], | ||
| usage: { | ||
| prompt_tokens: 8, | ||
| completion_tokens: 12, | ||
| total_tokens: 20, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockServer(); | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const client = new OpenAI({ | ||
| baseURL: `http://localhost:${server.address().port}/openai`, | ||
| apiKey: 'mock-api-key', | ||
| }); | ||
|
|
||
| // Test 1: Verify .withResponse() method exists and can be called | ||
| const result = client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'Test withResponse' }], | ||
| }); | ||
|
|
||
| // Verify method exists | ||
| if (typeof result.withResponse !== 'function') { | ||
| throw new Error('.withResponse() method does not exist'); | ||
| } | ||
|
|
||
| // Call .withResponse() and verify structure | ||
| const { data } = await result.withResponse(); | ||
|
|
||
| if (!data) { | ||
| throw new Error('.withResponse() did not return data'); | ||
| } | ||
|
|
||
| // Test 2: Verify regular await still works | ||
| const result2 = await client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'Test regular await' }], | ||
| }); | ||
|
|
||
| if (!result2 || result2.id !== 'chatcmpl-withresponse') { | ||
| throw new Error('Regular await failed'); | ||
| } | ||
| }); | ||
|
|
||
| server.close(); | ||
| } | ||
|
|
||
| run(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test doesn't cover streaming
.withResponse()regressionLow Severity
The PR description states the fix is specifically for streaming calls where
.withResponse()returned the original uninstrumented stream. However, the test scenario only tests non-streaming calls (nostream: trueparameter). According to the review rules forfixPRs, tests should verify the specific regression being fixed. A streaming test case with.withResponse()would provide confidence that the core bug is actually fixed. Flagging this because the review rules file specifies that fix PRs should include tests for the specific regression.