Skip to content
Source
Sign inSign up
TanStack/ai

openai-base: a tool call cut off by finish_reason "length" is reported as "tool_calls" and re-sent #1853

Closedkartsan03(GitHub profile, opens in a new tab) opened this issue 0 comments

TanStack AI version

@tanstack/ai 0.71.0, @tanstack/ai-openai 0.31.0 (@tanstack/openai-base 0.16.0)

Framework/Library version

Node.js 22, chat() on the server, no framework

Describe the bug and the steps to reproduce it

With the Chat Completions adapter, when max_tokens runs out in the middle of a tool call's arguments, the stream ends with finish_reason: "length". chat() then:

  1. emits TOOL_CALL_END for the unfinished arguments and RUN_FINISHED with finishReason: "tool_calls", so the token limit is lost;
  2. treats the arguments as a tool parse failure (TOOL_CALL_RESULT: Failed to parse tool arguments as JSON) and sends the conversation back to the model. With the same max_tokens the next answer is usually cut off too; in the repro one chat() call sends 5 requests.

The finish is chosen at packages/openai-base/src/adapters/chat-completions-text.ts#L1392-L1399: emittedAnyToolCallEnd wins over the pending length. For plain text the adapter already passes length through.

Expected: finishReason: "length" (the arguments are incomplete because of the limit, not malformed), and no automatic retry of a call the limit cut off.

Steps: save the script below as repro.mjs, then

npm init -y && npm pkg set type=module
npm install @tanstack/ai@0.71.0 @tanstack/ai-openai@0.31.0
node repro.mjs

Output:

RUN_FINISHED tool_calls
TOOL_CALL_RESULT {"error":"Failed to parse tool arguments as JSON: {\"title\":\"Pottery workshop in Mál"}
(the two lines above repeat five times)
requests: 5

No API key is needed: a custom fetch returns the stream.

import { chat, toolDefinition } from '@tanstack/ai';
import { createOpenaiChatCompletions } from '@tanstack/ai-openai';

// The arguments stop mid-string because max_tokens ran out; the API says so with finish_reason "length".
const chunk = (delta, finish_reason = null) => ({ id: 'chatcmpl-1', object: 'chat.completion.chunk', created: 1,
  model: 'gpt-6-luna', choices: [{ index: 0, delta, finish_reason }] });
const body = [
  chunk({ role: 'assistant', tool_calls: [{ index: 0, id: 'call_1', type: 'function',
    function: { name: 'create_event', arguments: '{"title":"Pottery workshop in Mál' } }] }),
  chunk({}, 'length'),
].map(value => `data: ${JSON.stringify(value)}\n\n`).join('') + 'data: [DONE]\n\n';
let requests = 0;
globalThis.fetch = async () => {
  requests += 1;
  return new Response(body, { headers: { 'content-type': 'text/event-stream' } });
};

const createEvent = toolDefinition({
  name: 'create_event', description: 'Create a calendar event',
  inputSchema: { type: 'object', properties: { title: { type: 'string' } }, required: ['title'] },
});
for await (const event of chat({
  adapter: createOpenaiChatCompletions('gpt-6-luna', 'test-key'),
  messages: [{ role: 'user', content: 'Create an event for a pottery workshop in Málaga.' }],
  tools: [createEvent],
  modelOptions: { max_tokens: 12 },
  debug: false,
})) {
  if (event.type === 'RUN_FINISHED') console.log('RUN_FINISHED', event.metadata?.tanstack?.finishReason);
  if (event.type === 'TOOL_CALL_RESULT') console.log('TOOL_CALL_RESULT', event.content);
}
console.log('requests:', requests);

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliably reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  1. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

  2. AlemTuzlak closed this

Sign in to join this conversation.