Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#1,853 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@tombeckenham ci sta già lavorando.

Dal 22/10/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

waiting-on: maintainer
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/[email protected] @tanstack/[email protected]
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
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

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.
Lingua principale
TypeScript
Stelle
3.2k
Fork
365
Merge medio
1g 9h
PR unite (30g)
263

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/ai

Tutte le issue di TanStack/ai

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.