Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🚩 fix: Preserve Client Tool Call Finish Reason After Text (#16408)
lia-by-librechat[bot]2278da115f28
diff --git a/api/server/controllers/agents/__tests__/openai.spec.js b/api/server/controllers/agents/__tests__/openai.spec.jsindex 097da840e3..25ae7ab602 100644--- a/api/server/controllers/agents/__tests__/openai.spec.js+++ b/api/server/controllers/agents/__tests__/openai.spec.js@@ -576,7 +576,7 @@ describe('OpenAIChatCompletionController', () => {);it.each([true, false])(- 'publishes only the accepted client call after a model claim (stream=%s)',+ 'publishes accepted client calls after text and a model claim (stream=%s)',async (streaming) => {const api = require('@librechat/api');const actual = jest.requireActual('@librechat/api');@@ -608,6 +608,9 @@ describe('OpenAIChatCompletionController', () => {agentId: 'agent-123',messageId: 'claimed-message',});+ await handlers.on_message_delta.handle('on_message_delta', {+ delta: { content: [{ type: 'text', text: 'Checking the weather.' }] },+ });await handlers.on_model_response.handle('on_model_response', {type: 'model_response',id: 'accepted-client',@@ -627,6 +630,9 @@ describe('OpenAIChatCompletionController', () => {.map(([frame]) => frame).filter((frame) => frame !== 'data: [DONE]\n\n').map((frame) => JSON.parse(frame.slice(6)));+ expect(+ frames.some((frame) => frame.choices[0].delta.content === 'Checking the weather.'),+ ).toBe(true);const calls = frames.flatMap((frame) => frame.choices[0].delta.tool_calls ?? []);expect(calls).toEqual([{ index: 0, id: 'a', type: 'function', function: { name: 'get_time', arguments: '' } },@@ -642,6 +648,7 @@ describe('OpenAIChatCompletionController', () => {expect(frames.at(-1).choices[0].finish_reason).toBe('tool_calls');} else {const choice = res.json.mock.calls[0][0].choices[0];+ expect(choice.message.content).toBe('Checking the weather.');expect(choice.message.tool_calls).toEqual([{id: 'a',@@ -909,6 +916,7 @@ describe('OpenAIChatCompletionController', () => {expect.anything(),expect.anything(),mockCompletionUsage,+ true,);});@@ -920,7 +928,12 @@ describe('OpenAIChatCompletionController', () => {await OpenAIChatCompletionController(req, res);- expect(sendFinalChunk).toHaveBeenCalledWith(expect.anything(), 'stop', mockCompletionUsage);+ expect(sendFinalChunk).toHaveBeenCalledWith(+ expect.anything(),+ 'stop',+ mockCompletionUsage,+ true,+ );});describe('content filtering', () => {diff --git a/api/server/controllers/agents/openai.js b/api/server/controllers/agents/openai.jsindex 1910cab8ef..6cb1e80053 100644--- a/api/server/controllers/agents/openai.js+++ b/api/server/controllers/agents/openai.js@@ -1180,7 +1180,7 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {// Finalize responseconst duration = Date.now() - requestStartTime;if (isStreaming) {- sendFinalChunk(handlerConfig, 'stop', usage);+ sendFinalChunk(handlerConfig, 'stop', usage, true);res.end();logger.debug(`[OpenAI API] Response ${responseId} completed in ${duration}ms (streaming)`);@@ -1216,6 +1216,7 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {aggregator.getReasoning(),aggregator.toolCalls,usage,+ true,);res.json(response);logger.debug(diff --git a/packages/api/src/agents/openai/README.md b/packages/api/src/agents/openai/README.mdindex a2bbc24899..d9cb6b293b 100644--- a/packages/api/src/agents/openai/README.md+++ b/packages/api/src/agents/openai/README.md@@ -59,7 +59,7 @@ Missing names, missing arguments, malformed JSON or unattributable argument data## Accepted-result terminal behavior-The SDK projector accepts only client-disposition calls from graph-accepted responses. A newer accepted text/internal result or a model-tool claim discards superseded calls for that agent. Calls are validated, given unique outward IDs and emitted at successful response completion; failed or aborted runs never publish buffered calls. Internal tools followed by a text answer finish with `stop` and no outward calls. The shared response builders use `tool_calls` as the finish reason when client calls exist without text.+The SDK projector accepts only client-disposition calls from graph-accepted responses. A newer accepted text/internal result or a model-tool claim discards superseded calls for that agent. Calls are validated, given unique outward IDs and emitted at successful response completion; failed or aborted runs never publish buffered calls. Internal tools followed by a text answer finish with `stop` and no outward calls. Both in-repo hosts explicitly mark the SDK map as accepted-only when calling the shared serializers: if any accepted client calls remain, the final reason is `tool_calls`, even when text was already emitted. Compatibility consumers of the legacy run-step projector retain the previous `stop` finish reason for historical calls followed by text.## Verification and limitsdiff --git a/packages/api/src/agents/openai/handlers.spec.ts b/packages/api/src/agents/openai/handlers.spec.tsindex 1a3edca37a..59a1d15c58 100644--- a/packages/api/src/agents/openai/handlers.spec.ts+++ b/packages/api/src/agents/openai/handlers.spec.ts@@ -50,13 +50,18 @@ describe('OpenAI-compatible agent stream handlers', () => {invalidToolCalls: [],});expect(frames).toHaveLength(3);- sendFinalChunk(config, 'stop', {- prompt_tokens: 12,- completion_tokens: 3,- total_tokens: 15,- primary: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 },- subagent: { prompt_tokens: 2, completion_tokens: 1, total_tokens: 3 },- });+ sendFinalChunk(+ config,+ 'stop',+ {+ prompt_tokens: 12,+ completion_tokens: 3,+ total_tokens: 15,+ primary: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 },+ subagent: { prompt_tokens: 2, completion_tokens: 1, total_tokens: 3 },+ },+ true,+ );return frames;};@@ -75,7 +80,7 @@ describe('OpenAI-compatible agent stream handlers', () => {},{},]);- expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('stop');+ expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('tool_calls');expect(chunks[chunks.length - 1].usage).toMatchObject({total_tokens: 15,subagent: { total_tokens: 3 },diff --git a/packages/api/src/agents/openai/handlers.ts b/packages/api/src/agents/openai/handlers.tsindex 69643326dd..550c14b0f7 100644--- a/packages/api/src/agents/openai/handlers.ts+++ b/packages/api/src/agents/openai/handlers.ts@@ -807,14 +807,16 @@ export function sendFinalChunk(config: OpenAIContentStreamConfig,finishReason: ChatCompletionChunkChoice['finish_reason'] = 'stop',usageOverride?: CompletionUsage,+ /** True when the map contains only accepted client-owned calls, not legacy run-step history. */+ acceptedToolCallsOnly = false,): void {const { context, tracker } = config;const writer = getStreamWriter(config);tracker.finishToolCalls?.();- // Determine finish reason based on content+ // Legacy run-step history followed by text stays 'stop'; accepted client calls must be executed.let reason = finishReason;- if (tracker.toolCalls.size > 0 && !tracker.hasText) {+ if (tracker.toolCalls.size > 0 && (acceptedToolCallsOnly || !tracker.hasText)) {reason = 'tool_calls';}diff --git a/packages/api/src/agents/openai/service.spec.ts b/packages/api/src/agents/openai/service.spec.tsindex d37b058233..852e1418aa 100644--- a/packages/api/src/agents/openai/service.spec.ts+++ b/packages/api/src/agents/openai/service.spec.ts@@ -161,6 +161,60 @@ describe('createAgentChatCompletion - MCP permission user propagation', () => {},);+ it.each([true, false])(+ 'finishes with tool_calls when an accepted client call follows text (stream=%s)',+ async (stream) => {+ const req = createMockReq(+ { id: 'user' },+ { model: 'agent_test', messages: [{ role: 'user', content: 'hi' }], stream },+ );+ const res = createMockRes();+ processStream.mockImplementationOnce(async () => {+ const { customHandlers: handlers } = createRun.mock.calls[0][0] as Parameters<+ NonNullable<ChatCompletionDependencies['createRun']>+ >[0];+ await handlers.on_message_delta.handle('on_message_delta', {+ delta: { content: [{ type: 'text', text: 'Checking the weather.' }] },+ });+ await handlers.on_model_response.handle('on_model_response', {+ type: 'model_response',+ id: 'accepted-client',+ agentId: 'agent_test',+ toolCalls: [{ id: 'client', name: 'lookup', args: { city: 'Madrid' } }],+ toolCallDispositions: ['client'],+ invalidToolCalls: [],+ });+ });++ await createAgentChatCompletion(req, res, deps);++ if (stream) {+ const chunks: ChatCompletionChunk[] = (res.write as jest.Mock).mock.calls+ .map(([frame]: [string]) => frame)+ .filter((frame) => frame !== 'data: [DONE]\n\n')+ .map((frame) => JSON.parse(frame.slice(6)));+ expect(+ chunks.some((chunk) => chunk.choices[0].delta.content === 'Checking the weather.'),+ ).toBe(true);+ expect(+ chunks+ .flatMap((chunk) => chunk.choices[0].delta.tool_calls ?? [])+ .some((call) => call.id === 'client'),+ ).toBe(true);+ expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('tool_calls');+ } else {+ const response = getResponseMock(res, 'json').mock.calls[0][0] as {+ choices: [+ { message: { content: string; tool_calls: [{ id: string }] }; finish_reason: string },+ ];+ };+ expect(response.choices[0].message.content).toBe('Checking the weather.');+ expect(response.choices[0].message.tool_calls[0].id).toBe('client');+ expect(response.choices[0].finish_reason).toBe('tool_calls');+ }+ },+ );+it.each([true, false])('does not publish fallback arguments after provider failure (stream=%s)',async (stream) => {diff --git a/packages/api/src/agents/openai/service.ts b/packages/api/src/agents/openai/service.tsindex 6d4204eb50..71a061b2cf 100644--- a/packages/api/src/agents/openai/service.ts+++ b/packages/api/src/agents/openai/service.ts@@ -570,9 +570,12 @@ export function buildNonStreamingResponse(reasoning: string,toolCalls: Map<number, ToolCall>,usage: CompletionUsage,+ /** True when the map contains only accepted client-owned calls, not legacy run-step history. */+ acceptedToolCallsOnly = false,): ChatCompletionResponse {const toolCallsArray = Array.from(toolCalls.values());- const finishReason = toolCallsArray.length > 0 && !text ? 'tool_calls' : 'stop';+ const finishReason =+ toolCallsArray.length > 0 && (acceptedToolCallsOnly || !text) ? 'tool_calls' : 'stop';return {id: context.requestId,@@ -920,7 +923,7 @@ export async function createAgentChatCompletion(// Finalize responseif (isStreaming && handlerConfig) {- sendFinalChunk(handlerConfig);+ sendFinalChunk(handlerConfig, 'stop', undefined, true);res.end();} else if (aggregator) {aggregator.finishToolCalls?.();@@ -939,6 +942,7 @@ export async function createAgentChatCompletion(aggregator.getReasoning(),aggregator.toolCalls,usage,+ true,);res.json(response);}