Repository navigation
fix(server-utils): Keep OpenAI streamed tool calls that have no index #25622
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
Draft
pcbeingused333
wants to merge
3
commits into
getsentry:develop
Choose a base branch
from
pcbeingused333:fix/openai-stream-tool-calls-without-index
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.
+273
−6
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f148c02
fix(server-utils): Keep OpenAI streamed tool calls that have no index
pcbeingused333 f0ac195
test(server-utils): cover an index-less OpenAI tool call followed by …
pcbeingused333 349e043
fix(server-utils): Handle null and reused indexes in OpenAI streamed …
pcbeingused333 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
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
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
150 changes: 150 additions & 0 deletions
150
packages/server-utils/test/ai/lib/tracing/openai-streaming-tool-calls.test.ts
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,150 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import type { Span } from '@sentry/core'; | ||
| import { GEN_AI_RESPONSE_TOOL_CALLS } from '@sentry/conventions/attributes'; | ||
| import { instrumentStream } from '../../../../src/ai/openai/streaming'; | ||
| import type { ChatCompletionChunk, ChatCompletionToolCall } from '../../../../src/ai/openai/types'; | ||
|
|
||
| function createMockSpan(): { span: Span; attributes: Record<string, unknown> } { | ||
| const attributes: Record<string, unknown> = {}; | ||
| let isEnded = false; | ||
| const span = { | ||
| isRecording: () => !isEnded, | ||
| setAttribute: (key: string, value: unknown) => { | ||
| attributes[key] = value; | ||
| }, | ||
| setAttributes: (attrs: Record<string, unknown>) => { | ||
| Object.assign(attributes, attrs); | ||
| }, | ||
| setStatus: () => {}, | ||
| end: () => { | ||
| isEnded = true; | ||
| }, | ||
| } as unknown as Span; | ||
| return { span, attributes }; | ||
| } | ||
|
|
||
| function chunkWith(toolCalls: Array<Partial<ChatCompletionToolCall>>): ChatCompletionChunk { | ||
| return { | ||
| id: 'chatcmpl-1', | ||
| object: 'chat.completion.chunk', | ||
| model: 'gemini-test', | ||
| choices: [{ index: 0, delta: { tool_calls: toolCalls } }], | ||
| } as unknown as ChatCompletionChunk; | ||
| } | ||
|
|
||
| async function recordedToolCalls(chunks: ChatCompletionChunk[]): Promise<Array<Record<string, unknown>>> { | ||
| const { span, attributes } = createMockSpan(); | ||
| async function* stream(): AsyncGenerator<ChatCompletionChunk> { | ||
| yield* chunks; | ||
| } | ||
| for await (const _ of instrumentStream(stream(), span, true)) { | ||
| void _; | ||
| } | ||
| return JSON.parse(attributes[GEN_AI_RESPONSE_TOOL_CALLS] as string) as Array<Record<string, unknown>>; | ||
| } | ||
|
|
||
| describe('instrumentStream (openai chat completion tool calls)', () => { | ||
| it('keeps whole tool calls that carry an id and no index', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([ | ||
| { id: 'call_a', type: 'function', function: { name: 'lookup', arguments: '{"q":"a"}' } }, | ||
| { id: 'call_b', type: 'function', function: { name: 'search', arguments: '{"q":"b"}' } }, | ||
| ]), | ||
| ]); | ||
|
|
||
| expect(toolCalls).toEqual([ | ||
| { id: 'call_a', type: 'function', function: { name: 'lookup', arguments: '{"q":"a"}' } }, | ||
| { id: 'call_b', type: 'function', function: { name: 'search', arguments: '{"q":"b"}' } }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('joins argument fragments by id, then onto the latest call when there is no id', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([{ id: 'call_a', type: 'function', function: { name: 'lookup', arguments: '{"q":' } }]), | ||
| chunkWith([{ id: 'call_a', function: { name: '', arguments: '"a"' } }]), | ||
| chunkWith([{ function: { name: '', arguments: '}' } }]), | ||
| ]); | ||
|
|
||
| expect(toolCalls).toEqual([ | ||
| { id: 'call_a', type: 'function', function: { name: 'lookup', arguments: '{"q":"a"}' } }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('keeps named calls that have neither id nor index', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([{ function: { name: 'lookup', arguments: '{}' } }]), | ||
| chunkWith([{ function: { name: 'search', arguments: '{}' } }]), | ||
| ]); | ||
|
|
||
| expect(toolCalls.map(call => (call.function as { name: string }).name)).toEqual(['lookup', 'search']); | ||
| }); | ||
|
|
||
| it('does not let a later real index overwrite an earlier call without one', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([{ id: 'call_b', type: 'function', function: { name: 'search', arguments: '{}' } }]), | ||
| chunkWith([{ index: 0, id: 'call_a', function: { name: 'lookup', arguments: '{}' } }]), | ||
| ]); | ||
|
|
||
| expect(toolCalls.map(call => call.id).sort()).toEqual(['call_a', 'call_b']); | ||
| }); | ||
|
|
||
| it('still groups indexed deltas by index', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([{ index: 1, id: 'call_b', function: { name: 'search', arguments: '{' } }]), | ||
| chunkWith([{ index: 0, id: 'call_a', function: { name: 'lookup', arguments: '{}' } }]), | ||
| chunkWith([{ index: 1, function: { name: '', arguments: '}' } }]), | ||
| ]); | ||
|
|
||
| expect(toolCalls).toEqual([ | ||
| expect.objectContaining({ id: 'call_a', function: { name: 'lookup', arguments: '{}' } }), | ||
| expect.objectContaining({ id: 'call_b', function: { name: 'search', arguments: '{}' } }), | ||
| ]); | ||
| }); | ||
|
|
||
| it('treats a null index like a missing one', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([ | ||
| { index: null, id: 'call_a', type: 'function', function: { name: 'lookup', arguments: '{"q":"a"}' } }, | ||
| { index: null, id: 'call_b', type: 'function', function: { name: 'search', arguments: '{"q":"b"}' } }, | ||
| ]), | ||
| chunkWith([{ index: null, function: { name: '', arguments: ' ' } }]), | ||
| ]); | ||
|
|
||
| expect(toolCalls.map(call => call.id)).toEqual(['call_a', 'call_b']); | ||
| expect((toolCalls[1]?.function as { arguments: string }).arguments).toBe('{"q":"b"} '); | ||
| }); | ||
|
|
||
| it('opens a new call when a different id lands on an index that is already taken', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([ | ||
| { index: 0, id: 'call_a', type: 'function', function: { name: 'lookup', arguments: '{"q":"a"}' } }, | ||
| { index: 0, id: 'call_b', type: 'function', function: { name: 'search', arguments: '{"q":"b"}' } }, | ||
| ]), | ||
| ]); | ||
|
|
||
| expect(toolCalls).toEqual([ | ||
| { index: 0, id: 'call_a', type: 'function', function: { name: 'lookup', arguments: '{"q":"a"}' } }, | ||
| { index: 0, id: 'call_b', type: 'function', function: { name: 'search', arguments: '{"q":"b"}' } }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('continues the latest call opened on a reused index when the next delta has no id', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([{ index: 0, id: 'call_a', function: { name: 'lookup', arguments: '{}' } }]), | ||
| chunkWith([{ index: 0, id: 'call_b', function: { name: 'search', arguments: '{"q":' } }]), | ||
| chunkWith([{ index: 0, function: { name: '', arguments: '"b"}' } }]), | ||
| ]); | ||
|
|
||
| expect(toolCalls.map(call => (call.function as { arguments: string }).arguments)).toEqual(['{}', '{"q":"b"}']); | ||
| }); | ||
|
|
||
| it('does not split a call whose id is repeated on every delta of its index', async () => { | ||
| const toolCalls = await recordedToolCalls([ | ||
| chunkWith([{ index: 0, id: 'call_a', function: { name: 'lookup', arguments: '{"q":' } }]), | ||
| chunkWith([{ index: 0, id: 'call_a', function: { name: '', arguments: '"a"}' } }]), | ||
| ]); | ||
|
|
||
| expect(toolCalls).toHaveLength(1); | ||
| expect((toolCalls[0]?.function as { arguments: string }).arguments).toBe('{"q":"a"}'); | ||
| }); | ||
| }); |
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.
could we also get a node-integration test for this? a Gemini style stream in dev-packages/node-integration-tests/suites/tracing/openai/openai-tool-calls/scenario.mjs (whole parallel calls, no index) so it goes through the real openai SDK, the unit tests call instrumentStream directly