Repository navigation
fix(core): preserve legacy OpenAI function calls #6240
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
Closed
VectorPeak
wants to merge
11
commits into
QwenLM:main
from
VectorPeak:codex/legacy-function-call-conversion
Closed
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f2f6f8a
fix(core): preserve legacy OpenAI function calls
VectorPeak 4129bad
fix(core): refine legacy function call streaming
VectorPeak 5e011d7
test(core): cover legacy streaming edge cases
VectorPeak 84cdf81
fix(core): suppress truncated legacy streaming calls
VectorPeak 7697e36
fix(core): harden legacy function call args
VectorPeak 97f43e6
fix(core): align legacy function call truncation
VectorPeak 7ac5fbe
fix(core): refine legacy function call diagnostics
VectorPeak 5d633d8
Merge branch 'main' into codex/legacy-function-call-conversion
VectorPeak 2a82dec
fix(core): reset stale legacy function call parser state
VectorPeak f1ae573
fix(core): preserve modern tool call args
VectorPeak 506e92d
Merge branch 'main' into codex/legacy-function-call-conversion
VectorPeak 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,19 @@ import { | |
| import type OpenAI from 'openai'; | ||
| import { convertToFunctionResponse } from '../coreToolScheduler.js'; | ||
|
|
||
| function legacyFunctionCallPart( | ||
| name = 'read_file', | ||
| args: Record<string, unknown> = { path: 'README.md' }, | ||
| ) { | ||
| return { | ||
| functionCall: { | ||
| id: expect.any(String), | ||
| name, | ||
| args, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe('OpenAIContentConverter', () => { | ||
| let converter: typeof OpenAIContentConverter; | ||
| let requestContext: RequestContext; | ||
|
|
@@ -2677,6 +2690,13 @@ describe('OpenAIContentConverter', () => { | |
| }); | ||
|
|
||
| describe('convertOpenAIResponseToGemini', () => { | ||
| type LegacyFunctionCallMessage = Omit< | ||
| Partial<OpenAI.Chat.ChatCompletionMessage>, | ||
| 'function_call' | ||
| > & { | ||
| function_call?: { name: string; arguments?: string }; | ||
| }; | ||
|
|
||
| it('should handle empty choices array without crashing', () => { | ||
| const response = converter.convertOpenAIResponseToGemini( | ||
| { | ||
|
|
@@ -2692,6 +2712,105 @@ describe('OpenAIContentConverter', () => { | |
| expect(response.candidates).toEqual([]); | ||
| }); | ||
|
|
||
| function convertLegacyFunctionCall(message: LegacyFunctionCallMessage) { | ||
| return converter.convertOpenAIResponseToGemini( | ||
| { | ||
| object: 'chat.completion', | ||
| id: 'chatcmpl-legacy-function-call', | ||
| created: 123, | ||
| model: 'test-model', | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { role: 'assistant', content: null, ...message }, | ||
| finish_reason: 'function_call', | ||
| logprobs: null, | ||
| }, | ||
| ], | ||
| } as unknown as OpenAI.Chat.ChatCompletion, | ||
| requestContext, | ||
| ); | ||
| } | ||
|
|
||
| it.each([ | ||
| { | ||
| name: 'legacy function_call responses', | ||
| message: { | ||
| function_call: { | ||
| name: 'read_file', | ||
| arguments: '{"path":"README.md"}', | ||
| }, | ||
| }, | ||
| expectedParts: [legacyFunctionCallPart()], | ||
| }, | ||
| { | ||
| name: 'legacy function_call when tool_calls is empty', | ||
| message: { | ||
| tool_calls: [], | ||
| function_call: { | ||
| name: 'read_file', | ||
| arguments: '{"path":"README.md"}', | ||
| }, | ||
| }, | ||
| expectedParts: [legacyFunctionCallPart()], | ||
| }, | ||
| { | ||
| name: 'modern tool_calls over legacy function_call', | ||
| message: { | ||
| tool_calls: [ | ||
| { | ||
| id: 'call_modern', | ||
| type: 'function' as const, | ||
| function: { | ||
| name: 'read_file', | ||
| arguments: '{"path":"modern.md"}', | ||
| }, | ||
| }, | ||
| ], | ||
| function_call: { | ||
| name: 'read_file', | ||
| arguments: '{"path":"legacy.md"}', | ||
| }, | ||
| }, | ||
| expectedParts: [ | ||
| legacyFunctionCallPart('read_file', { path: 'modern.md' }), | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid legacy function_call arguments', | ||
| message: { | ||
| function_call: { name: 'read_file', arguments: 'not-json' }, | ||
| }, | ||
| expectedParts: [legacyFunctionCallPart('read_file', {})], | ||
| }, | ||
| { | ||
| name: 'legacy function_call without arguments', | ||
| message: { | ||
| function_call: { name: 'ping' }, | ||
| }, | ||
| expectedParts: [legacyFunctionCallPart('ping', {})], | ||
| }, | ||
| { | ||
| name: 'text content plus legacy function_call', | ||
| message: { | ||
| content: 'Let me read that file.', | ||
| function_call: { | ||
| name: 'read_file', | ||
| arguments: '{"path":"README.md"}', | ||
| }, | ||
| }, | ||
| expectedParts: [ | ||
| { text: 'Let me read that file.' }, | ||
| legacyFunctionCallPart(), | ||
| ], | ||
| }, | ||
| ])('should convert $name', ({ message, expectedParts }) => { | ||
| const response = convertLegacyFunctionCall(message); | ||
|
|
||
| expect(response.candidates?.[0]?.content?.parts).toEqual(expectedParts); | ||
| expect(response.candidates?.[0]?.finishReason).toBe(FinishReason.STOP); | ||
| }); | ||
|
|
||
| it('keeps the estimated prompt/completion split summing to total tokens', () => { | ||
| // When a provider reports only total_tokens, the 70/30 estimate must | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in dfde5a0. I added explicit non-streaming and streaming regression coverage for the precedence invariant: when modern |
||
| // still add back up to the total instead of rounding each half on its | ||
|
|
@@ -5205,6 +5324,186 @@ describe('Truncated tool call detection in streaming', () => { | |
|
|
||
| expect(result.candidates?.[0]?.finishReason).toBe(FinishReason.MAX_TOKENS); | ||
| }); | ||
|
|
||
| function feedLegacyFunctionCallChunks({ | ||
| chunks, | ||
| finishReason = 'function_call', | ||
| includeEmptyToolCallsOnFirstChunk = false, | ||
| includeModernToolCallOnFirstChunk = false, | ||
| }: { | ||
| chunks: Array<{ name?: string; arguments?: string }>; | ||
| finishReason?: string; | ||
| includeEmptyToolCallsOnFirstChunk?: boolean; | ||
| includeModernToolCallOnFirstChunk?: boolean; | ||
| }) { | ||
| const ctx = createStreamingRequestContext(); | ||
|
|
||
| for (const [index, functionCall] of chunks.entries()) { | ||
| converter.convertOpenAIChunkToGemini( | ||
| { | ||
| object: 'chat.completion.chunk', | ||
| id: `legacy-c${index + 1}`, | ||
| created: 100, | ||
| model: 'test-model', | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| delta: { | ||
| ...(index === 0 && includeEmptyToolCallsOnFirstChunk | ||
| ? { tool_calls: [] } | ||
| : {}), | ||
| ...(index === 0 && includeModernToolCallOnFirstChunk | ||
| ? { | ||
| tool_calls: [ | ||
| { | ||
| index: 0, | ||
| id: 'call_modern', | ||
| type: 'function' as const, | ||
| function: { | ||
| name: 'read_file', | ||
| arguments: '{"path":"modern.md"}', | ||
| }, | ||
| }, | ||
| ], | ||
| } | ||
| : {}), | ||
| function_call: functionCall, | ||
| }, | ||
| finish_reason: null, | ||
| logprobs: null, | ||
| }, | ||
| ], | ||
| } as unknown as OpenAI.Chat.ChatCompletionChunk, | ||
| ctx, | ||
| ); | ||
| } | ||
|
|
||
| return converter.convertOpenAIChunkToGemini( | ||
| { | ||
| object: 'chat.completion.chunk', | ||
| id: 'legacy-final', | ||
| created: 101, | ||
| model: 'test-model', | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| delta: {}, | ||
| finish_reason: finishReason, | ||
| logprobs: null, | ||
| }, | ||
| ], | ||
| } as unknown as OpenAI.Chat.ChatCompletionChunk, | ||
| ctx, | ||
| ); | ||
| } | ||
|
|
||
| it.each([ | ||
| { | ||
| name: 'legacy streaming function_call chunks', | ||
| chunks: [ | ||
| { name: 'read_file' }, | ||
| { arguments: '{"path"' }, | ||
| { arguments: ':"README.md"}' }, | ||
| ], | ||
| expectedFinishReason: FinishReason.STOP, | ||
| expectedParts: [legacyFunctionCallPart()], | ||
| }, | ||
| { | ||
| name: 'legacy streaming function_call when tool_calls is empty', | ||
| chunks: [ | ||
| { name: 'read_file' }, | ||
| { arguments: '{"path"' }, | ||
| { arguments: ':"README.md"}' }, | ||
| ], | ||
| includeEmptyToolCallsOnFirstChunk: true, | ||
| expectedFinishReason: FinishReason.STOP, | ||
| expectedParts: [legacyFunctionCallPart()], | ||
| }, | ||
| { | ||
| name: 'modern streaming tool_calls over legacy function_call', | ||
| chunks: [{ name: 'read_file', arguments: '{"path":"legacy.md"}' }], | ||
| includeModernToolCallOnFirstChunk: true, | ||
| finishReason: 'tool_calls', | ||
| expectedFinishReason: FinishReason.STOP, | ||
| expectedParts: [ | ||
| legacyFunctionCallPart('read_file', { path: 'modern.md' }), | ||
| ], | ||
| }, | ||
| { | ||
| name: 'legacy streaming function_call scalar arguments', | ||
| chunks: [{ name: 'read_file', arguments: '"not-object"' }], | ||
| expectedFinishReason: FinishReason.STOP, | ||
| expectedParts: [legacyFunctionCallPart('read_file', {})], | ||
| }, | ||
| { | ||
| name: 'truncated legacy streaming function_call', | ||
| chunks: [{ name: 'read_file', arguments: '{"path"' }], | ||
| expectedFinishReason: FinishReason.MAX_TOKENS, | ||
| expectedParts: [], | ||
| }, | ||
| { | ||
| name: 'legacy streaming function_call without arguments', | ||
| chunks: [{ name: 'ping' }], | ||
| expectedFinishReason: FinishReason.STOP, | ||
| expectedParts: [legacyFunctionCallPart('ping', {})], | ||
| }, | ||
| ])( | ||
| 'should convert $name', | ||
| ({ | ||
| chunks, | ||
| includeEmptyToolCallsOnFirstChunk, | ||
| includeModernToolCallOnFirstChunk, | ||
| finishReason, | ||
| expectedFinishReason, | ||
| expectedParts, | ||
| }) => { | ||
| const result = feedLegacyFunctionCallChunks({ | ||
| chunks, | ||
| finishReason, | ||
| includeEmptyToolCallsOnFirstChunk, | ||
| includeModernToolCallOnFirstChunk, | ||
| }); | ||
|
|
||
| expect(result.candidates?.[0]?.finishReason).toBe(expectedFinishReason); | ||
| if (expectedParts) { | ||
| expect(result.candidates?.[0]?.content?.parts).toEqual(expectedParts); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| it('should convert legacy streaming content plus function_call in the same chunk', () => { | ||
| const ctx = createStreamingRequestContext(); | ||
|
|
||
| const result = converter.convertOpenAIChunkToGemini( | ||
| { | ||
| object: 'chat.completion.chunk', | ||
| id: 'legacy-content-and-function-call', | ||
| created: 102, | ||
| model: 'test-model', | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| delta: { | ||
| content: 'Let me read that file.', | ||
| function_call: { | ||
| name: 'read_file', | ||
| arguments: '{"path":"README.md"}', | ||
| }, | ||
| }, | ||
| finish_reason: 'function_call', | ||
| logprobs: null, | ||
| }, | ||
| ], | ||
| } as unknown as OpenAI.Chat.ChatCompletionChunk, | ||
| ctx, | ||
| ); | ||
|
|
||
| expect(result.candidates?.[0]?.finishReason).toBe(FinishReason.STOP); | ||
| expect(result.candidates?.[0]?.content?.parts).toEqual([ | ||
| { text: 'Let me read that file.' }, | ||
| legacyFunctionCallPart(), | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('mapGeminiFinishReasonToOpenAI', () => { | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.