diff --git a/packages/llm-providers/src/openai-compatible/chat/openai-compatible-chat-language-model.test.ts b/packages/llm-providers/src/openai-compatible/chat/openai-compatible-chat-language-model.test.ts index 1db22d5214..b106bc09ab 100644 --- a/packages/llm-providers/src/openai-compatible/chat/openai-compatible-chat-language-model.test.ts +++ b/packages/llm-providers/src/openai-compatible/chat/openai-compatible-chat-language-model.test.ts @@ -149,6 +149,43 @@ describe('OpenAICompatibleChatLanguageModel doStream', () => { expect(finishPartOf(parts).finishReason).toBe('tool-calls') }) + it('accepts functionless continuations of an in-flight tool call', async () => { + // The Chat Completions wire type makes function optional on each delta. + for (const ref of [{ index: 0 }, { id: 'call_1' }, {}]) { + const parts = await streamParts( + sseResponse([ + chunk({ + tool_calls: [ + { + ...ref, + id: 'call_1', + function: { name: 'read_files', arguments: '{"paths":' }, + }, + ], + }), + chunk({ tool_calls: [ref] }), + chunk({ tool_calls: [{ ...ref, function: null }] }), + chunk({ + tool_calls: [{ ...ref, function: { arguments: '["a.ts"]}' } }], + }), + chunk({}, { finish_reason: 'tool_calls' }), + '[DONE]', + ]), + ) + + expect(parts.some((part) => part.type === 'error')).toBe(false) + expect(parts.filter((part) => part.type === 'tool-call')).toEqual([ + { + type: 'tool-call', + toolCallId: 'call_1', + toolName: 'read_files', + input: '{"paths":["a.ts"]}', + }, + ]) + expect(finishPartOf(parts).finishReason).toBe('tool-calls') + } + }) + it('keeps honoring an explicit index', async () => { const parts = await streamParts( sseResponse([ diff --git a/packages/llm-providers/src/openai-compatible/chat/openai-compatible-chat-language-model.ts b/packages/llm-providers/src/openai-compatible/chat/openai-compatible-chat-language-model.ts index ec4999b13b..e88b73e30d 100644 --- a/packages/llm-providers/src/openai-compatible/chat/openai-compatible-chat-language-model.ts +++ b/packages/llm-providers/src/openai-compatible/chat/openai-compatible-chat-language-model.ts @@ -677,7 +677,7 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV2 { controller.enqueue({ type: 'tool-input-delta', id: toolCall.id, - delta: toolCallDelta.function.arguments ?? '', + delta: toolCallDelta.function?.arguments ?? '', }) // check if tool call is complete @@ -841,7 +841,7 @@ export function inferToolCallIndex( | { id: string; function: { name: string }; hasFinished: boolean } | undefined >, - delta: { id?: string | null; function: { name?: string | null } }, + delta: { id?: string | null; function?: { name?: string | null } | null }, ): number { if (delta.id != null) { const existing = toolCalls.findIndex((call) => call?.id === delta.id) @@ -852,10 +852,11 @@ export function inferToolCallIndex( if ( last && !last.hasFinished && - (delta.function.name == null || delta.function.name === last.function.name) + (delta.function?.name == null || + delta.function?.name === last.function.name) ) return lastIndex - return delta.function.name == null && last ? lastIndex : toolCalls.length + return delta.function?.name == null && last ? lastIndex : toolCalls.length } const openaiCompatibleTokenUsageSchema = z @@ -957,10 +958,12 @@ const createOpenAICompatibleChatChunkSchema = < // the whole chunk, so the tool call was silently lost. index: z.number().nullish(), id: z.string().nullish(), - function: z.object({ - name: z.string().nullish(), - arguments: z.string().nullish(), - }), + function: z + .object({ + name: z.string().nullish(), + arguments: z.string().nullish(), + }) + .nullish(), extra_content: z.unknown().nullish(), }), )