TanStack AI version#
@tanstack/ai 1.8.0, @tanstack/ai-bedrock 1.4.1 (the same code is on main)
Framework/Library version#
Node.js 26.13.1, server-side chat(), no framework
Describe the bug and the steps to reproduce it#
When a Bedrock ConverseStream ends before messageStop (for example, the connection drops or a proxy closes the response mid-stream), chat() treats the cut-off response as complete. It closes any tool call that is still open with its partial input, then emits RUN_FINISHED with finishReason: 'stop'. The caller can't tell that the response was cut off, and the tool call comes out with truncated arguments that aren't valid JSON (#1582 shows where that can lead on replay).
At the end of the stream, processConverseStream emits TOOL_CALL_END for every tool call that never got a contentBlockStop, and then falls back to finishReason ?? 'stop', whether or not a messageStop arrived.
Expected: unless the request was aborted, a RUN_ERROR (for example with code incomplete-stream) that keeps the partial text, and no TOOL_CALL_END for a tool call whose input never finished. #2042 added this for @tanstack/ai-anthropic, #2124 for @tanstack/ai-gemini, and @tanstack/openai-base has had it since #1604.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
The script overrides the protected sendStream seam, as the adapter's own tests do, so no AWS credentials or network are needed:
npm install @tanstack/ai@1.8.0 @tanstack/ai-bedrock@1.4.1
node repro.mjs
import { chat } from '@tanstack/ai'
import { BedrockConverseTextAdapter } from '@tanstack/ai-bedrock'
const events = [
{ messageStart: { role: 'assistant' } },
{ contentBlockDelta: { contentBlockIndex: 0, delta: { text: 'Let me clean that up.' } } },
{ contentBlockStop: { contentBlockIndex: 0 } },
{ contentBlockStart: { contentBlockIndex: 1, start: { toolUse: { toolUseId: 'tooluse_1', name: 'delete_files' } } } },
{ contentBlockDelta: { contentBlockIndex: 1, delta: { toolUse: { input: '{"path": "/data/repo' } } } },
]
class CutStreamAdapter extends BedrockConverseTextAdapter {
async sendStream() {
return (async function* () {
yield* events
})()
}
}
const adapter = new CutStreamAdapter({ region: 'us-east-1' }, 'anthropic.claude-sonnet-4-5-20250929-v1:0')
for await (const chunk of chat({
adapter,
messages: [{ role: 'user', content: 'Clean up /data/repo' }],
threadId: 'repro',
})) {
const { args, finishReason } = chunk.metadata?.tanstack ?? {}
console.log(chunk.type, args ?? finishReason ?? '')
}
Output:
RUN_STARTED
TEXT_MESSAGE_START
TEXT_MESSAGE_CONTENT
TOOL_CALL_START
TOOL_CALL_ARGS
TOOL_CALL_END {"path": "/data/repo
TEXT_MESSAGE_END
RUN_FINISHED stop
Screenshots or Videos (Optional)#
No response
Do you intend to try to help solve this bug with your own PR?#
No, because I do not have time to dig into it
Terms & Code of Conduct#
Reproduced on
main(7ba3926).Result#
A ConverseStream that stops before
messageStopends the run withRUN_FINISHEDandfinishReason: 'stop'. The open tool call is closed with its partial input, and noRUN_ERRORarrives.The exact event list from the cut stream:
A completed stream with
contentBlockStop,messageStop: tool_useandmetadatastill givesRUN_FINISHEDwithfinishReason: tool_calls, which is correct.Suspected files#
packages/ai-bedrock/src/converse/stream-processor.ts: the drain loop at lines 317-328 emitsTOOL_CALL_ENDfor every tool call that never gotcontentBlockStop. Line 364 falls back tofinishReason ?? 'stop', whatever happened to the stream. There is no check for a missingmessageStop.packages/ai-bedrock/src/adapters/converse-text.ts:chatStream(line 295) only turns thrown errors intoRUN_ERROR.processConverseStreamnever throws for a cut stream, so this path is not reached.packages/ai-bedrock/tests/converse/stream-processor.test.ts(lines 287-300, 459-478) asserts the current behavior: the drain emitsTOOL_CALL_ENDandRUN_FINISHEDafter a stream with nocontentBlockStopand nomessageStop. These two tests fix the bug that the issue reports.docs/chat/streaming.md(lines 199-201) documentsincomplete-streamfor OpenAI, OpenRouter and Anthropic. It has no Bedrock entry, which matches the missing code.How I ran it#
The sandbox had no
node_modules, andpnpm installfailed on this host: SQLite cannot open a WAL journal here (no/dev/shm). I patched the twojournal_modepragmas in the installed pnpm and its worker outside the checkout, then installed withpnpm install --filter @tanstack/ai-bedrock... --frozen-lockfile. The@tanstack/ai,@tanstack/ai-utils,@tanstack/ai-event-clientand@tanstack/openai-basepackages had to be built first, because the tests import theirdist.The repro file is new and no other file changed. It uses the protected
sendStreamseam, so it needs no AWS credentials or network.Repro#
Path:
packages/ai-bedrock/tests/converse/incomplete-stream.test.tsCommand:cd packages/ai-bedrock && ../../node_modules/.bin/vitest run tests/converse/incomplete-stream.test.tsOutput (end):
TanStack Source bot, investigate agent