fix(ai): make chat({ stream: false }) actually non-streaming on the wire #561
main from557-chat-stream-false-still-streams-on-the-wire-uses-runstreamingtext-+-streamtotext·opened View on GitHubCloses #557.
Summary#
- Adds optional
TextAdapter.chatNonStreaming()returningPromise<NonStreamingChatResult>({ content, reasoning?, toolCalls?, finishReason?, usage? }). When implemented,chat({ stream: false })routes through it for a true wire-level non-streaming request; when omitted, the legacy stream-then-concatenate fallback applies — out-of-tree adapters keep working unchanged. - Implements
chatNonStreaming()on all in-tree adapters: openai-base chat-completions + responses (covers openai, grok, groq), openrouter (chat-completions + responses), anthropic, gemini, ollama. - Rewires
runNonStreamingTextas a wire-level non-streaming agent loop: call adapter → execute returned tool calls server-side → append results → repeat untilfinishReason !== 'tool_calls'oragentLoopStrategyhalts. Approval-required and pure client-side tools throw an explicit error directing callers to streaming, since they can't round-trip throughPromise<string>.
Why#
Previously chat({ stream: false }) invoked runStreamingText() and concatenated the SSE response via streamToText. The wire request still carried Accept: text/event-stream and "stream": true in the body — only the SDK return type changed (Promise<string> instead of AsyncIterable<StreamChunk>). On reasoning models with long pre-content thinking phases (Grok 4.3 via OpenRouter was the original repro), proxies with sub-30s socket-idle timeouts could cut off the stream mid-flight even though a real non-streaming POST would complete fine.
Test plan#
- Unit: 5 new tests covering wire-level dispatch, multi-turn tool loop,
agentLoopStrategycap, approval-tool error, and out-of-tree fallback. Existing 757 tests still pass (762 total). - E2E: rewired
api.chat.tssofeature === 'one-shot-text'actually callschat({ stream: false }). The existingone-shot-text.spec.tsnow runs end-to-end against the new path across all 8 providers (openai, anthropic, gemini, ollama, groq, grok, openrouter, openrouter-responses). Updated the aimock fixture to includesystemFingerprint: nullso OpenRouter SDK's strict zod validation accepts the response. Full e2e suite (190 tests) passes. -
pnpm test:lib,pnpm test:types,pnpm test:eslint,pnpm build: all clean.
Notes#
- Marked draft per author's request — wants more review before merging.
- Middleware is intentionally not invoked on the non-streaming path, mirroring the silence of the existing
adapter.structuredOutputcall insiderunAgenticStructuredOutput. - The
Promise<string>return value remains content-only — reasoning is captured on the internalNonStreamingChatResultfor future surfaces but is not part of the public contract.
🤖 Generated with Claude Code
- 1
View your CI Pipeline Execution ↗ for commit 02e47658eb363d66c72c548128f28343ca77fa24
Command Status Duration Result nx run-many --targets=build --exclude=examples/**✅ Succeeded 1m 52s View ↗
☁️ Nx Cloud last updated this comment at
2026-05-15 00:13:45UTC@tanstack/ai
npm i https://pkg.pr.new/@tanstack/ai@561@tanstack/ai-anthropic
npm i https://pkg.pr.new/@tanstack/ai-anthropic@561@tanstack/ai-client
npm i https://pkg.pr.new/@tanstack/ai-client@561@tanstack/ai-code-mode
npm i https://pkg.pr.new/@tanstack/ai-code-mode@561@tanstack/ai-code-mode-skills
npm i https://pkg.pr.new/@tanstack/ai-code-mode-skills@561@tanstack/ai-devtools-core
npm i https://pkg.pr.new/@tanstack/ai-devtools-core@561@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@561@tanstack/ai-event-client
npm i https://pkg.pr.new/@tanstack/ai-event-client@561@tanstack/ai-fal
npm i https://pkg.pr.new/@tanstack/ai-fal@561@tanstack/ai-gemini
npm i https://pkg.pr.new/@tanstack/ai-gemini@561@tanstack/ai-grok
npm i https://pkg.pr.new/@tanstack/ai-grok@561@tanstack/ai-groq
npm i https://pkg.pr.new/@tanstack/ai-groq@561@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@561@tanstack/ai-isolate-node
npm i https://pkg.pr.new/@tanstack/ai-isolate-node@561@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@561@tanstack/ai-ollama
npm i https://pkg.pr.new/@tanstack/ai-ollama@561@tanstack/ai-openai
npm i https://pkg.pr.new/@tanstack/ai-openai@561@tanstack/ai-openrouter
npm i https://pkg.pr.new/@tanstack/ai-openrouter@561@tanstack/ai-preact
npm i https://pkg.pr.new/@tanstack/ai-preact@561@tanstack/ai-react
npm i https://pkg.pr.new/@tanstack/ai-react@561@tanstack/ai-react-ui
npm i https://pkg.pr.new/@tanstack/ai-react-ui@561@tanstack/ai-solid
npm i https://pkg.pr.new/@tanstack/ai-solid@561@tanstack/ai-solid-ui
npm i https://pkg.pr.new/@tanstack/ai-solid-ui@561@tanstack/ai-svelte
npm i https://pkg.pr.new/@tanstack/ai-svelte@561@tanstack/ai-utils
npm i https://pkg.pr.new/@tanstack/ai-utils@561@tanstack/ai-vue
npm i https://pkg.pr.new/@tanstack/ai-vue@561@tanstack/ai-vue-ui
npm i https://pkg.pr.new/@tanstack/ai-vue-ui@561@tanstack/openai-base
npm i https://pkg.pr.new/@tanstack/openai-base@561@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@561@tanstack/react-ai-devtools
npm i https://pkg.pr.new/@tanstack/react-ai-devtools@561@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@561commit:
02e4765I've looked into this a lot more. There are a tonne of restrictions when running non-streaming ai calls - particularly with anthropic. Given the amount of duplication we'd have to introduce to properly make non-streaming on the wire work, I don't think this is worth it @AlemTuzlak and @jherr . I'm closing this one as a not planned
tombeckenham closed this
Sign in to join this conversation.
🚀 Changeset Version Preview#
9 package(s) bumped directly, 25 bumped as dependents.
🟥 Major bumps#
@tanstack/ai-anthropic@tanstack/ai-gemini@tanstack/ai-grok@tanstack/ai-groq@tanstack/ai-ollama@tanstack/ai-openai@tanstack/ai-openrouter@tanstack/openai-base@tanstack/ai-code-mode@tanstack/ai-code-mode-skills@tanstack/ai-elevenlabs@tanstack/ai-event-client@tanstack/ai-fal@tanstack/ai-isolate-node@tanstack/ai-isolate-quickjs@tanstack/ai-preact@tanstack/ai-react@tanstack/ai-react-ui@tanstack/ai-solid@tanstack/ai-solid-ui@tanstack/ai-svelte@tanstack/ai-vue🟨 Minor bumps#
@tanstack/ai🟩 Patch bumps#
@tanstack/ai-client@tanstack/ai-code-mode-models-eval@tanstack/ai-devtools-core@tanstack/ai-isolate-cloudflare@tanstack/ai-vue-ui@tanstack/preact-ai-devtools@tanstack/react-ai-devtools@tanstack/solid-ai-devtoolsts-svelte-chatts-vue-chatvanilla-chat