Skip to content

fix(ai-ollama): close the request when the consumer stops a chat stream early - #2311

Merged
tombeckenham merged 2 commits into
TanStack:mainfrom
serhiizghama:fix/ollama-early-stop-abort
Dec 4, 2026
Merged

tombeckenham merged 2 commits into
TanStack:mainfrom
serhiizghama:fix/ollama-early-stop-abort

Conversation

@serhiizghama

@serhiizghama serhiizghama commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Changes

If you break out of a streamed Ollama chat early, the HTTP request to Ollama stays open. The SDK's own abort() works by closing that request, so while it stays open Ollama has no way to know that nobody is reading.

The ollama SDK does not cancel the response body when iteration stops, so the adapter has to call response.abort() itself. It already did that in the finally of chatStream, but only inside if (signal), and signal is only set for an injected client with a caller signal. The default path (ollamaText() / createOllamaChat() without an abortController) never got there.

Two changes in packages/ai-ollama/src/adapters/text.ts:

  1. chatStream calls response.abort() whenever the stream did not finish, with or without a caller signal. Removing the abort listener stays as it was.
  2. clientFor used to replace the SDK's own fetch signal with the caller's signal. Then response.abort() had nothing to cancel on the adapter's own client. It now combines both with AbortSignal.any, the same way ai-sandbox-railway does. Aborting the caller signal still cancels the request, and stopping one call does not abort the caller signal that chat() reuses for the next step.

Tests:

  • Unit: three cases in abort-signal-wire.test.ts (own client without signal, own client with signal, injected client without signal). All three fail on main. Reverting only the clientFor change fails the "own client with signal" case.
  • E2E: ollama-early-stop runs chat() against a local NDJSON server that sends one chunk and keeps the response open, then breaks on the first text chunk. On main the server still has the request open after 2 s. With this change it closes right away.

I ran test:sherif, test:knip, test:docs, test:kiira, test:maintainer, test:oxlint, test:lib, test:types, test:build and build through nx affected, plus test:dts and the full Playwright suite (853 passed, 4 skipped). Not test:ai-review.

Not sure the early-stop abort belongs in the finally rather than in a wrapper around the SDK iterator. I kept it where the existing abort already was.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request. (The test:pr targets above, without test:ai-review.)
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Ollama chat requests now close when you stop reading a streamed response early, including when no caller-provided abort signal is used.
    • Existing request cancellation signals are preserved.

@changeset-bot

changeset-bot Bot commented Dec 2, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b0d1cd4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tanstack/ai-ollama Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Ollama adapter now aborts the response when chat-stream iteration ends early, whether or not a caller signal exists. Unit tests and an end-to-end route test cover request closure.

Changes: Ollama stream cancellation

Layer / File(s) Summary
Signal handling and early-stop cancellation
packages/ai-ollama/src/adapters/text.ts, packages/ai-ollama/tests/abort-signal-wire.test.ts, .changeset/ollama-early-stop-abort.md
clientFor combines the caller signal with an existing request signal. chatStream removes its abort listener and aborts the response when iteration ends early. Unit tests cover adapter-created clients with and without a caller signal, and injected clients without one.
End-to-end request-close check
testing/e2e/src/routes/api.ollama-early-stop.ts, testing/e2e/src/routeTree.gen.ts, testing/e2e/tests/ollama-early-stop.spec.ts
The new POST route serves an unfinished response from a local Ollama stub and reports whether the request closes after the consumer stops reading. The generated route tree registers the route, and the end-to-end test checks the response.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • TanStack/ai#1975: Passes caller abort signals to provider requests, including Ollama stream cleanup, and changes the abort wiring tested by this PR.

Suggested reviewers: alemtuzlak, arvavit

Merge Risk: Minimal · up to b0d1c

This change makes the Ollama adapter close its HTTP request when a consumer stops reading a chat stream early, with or without a caller abort signal. Unit tests and an end-to-end test cover the behavior. No actionable merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the adapter now closes the Ollama request when a consumer stops reading a chat stream early. It matches the changeset, the text.ts edits, the unit tests, and the E2E t…
Description check Passed The description follows the template. It explains the change and why it is needed, and it names the two edits in text.ts. It lists the unit and E2E tests with the results on main. It reports the check…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the adapter now closes the Ollama request when a consumer stops reading a chat stream early. It matches the changeset, the text.ts edits, the unit tests, and the E2E test. The 'fix(ai-ollama):' prefix is a conventional-commit scope and is acceptable.

Full details: Description check

Explanation

The description follows the template. It explains the change and why it is needed, and it names the two edits in text.ts. It lists the unit and E2E tests with the results on main. It reports the checks that were run and marks the checklist items. It also notes an open design question about where the abort belongs. The Docs item is checked with the claim that the change is not user-facing, and the Release Impact item is checked as a published change, which matches the changeset. The template's 'Changes' section is filled in, so the required information is present.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: TanStack/ai
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 840ea56
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3e631994-fcd7-434a-aa2e-4af2566a73b9
Commits

Reviewing files that changed from the base of the PR and between 840ea56 and b0d1cd4.

Files selected for processing: 6

  • .changeset/ollama-early-stop-abort.md
  • packages/ai-ollama/src/adapters/text.ts
  • packages/ai-ollama/tests/abort-signal-wire.test.ts
  • testing/e2e/src/routeTree.gen.ts
  • testing/e2e/src/routes/api.ollama-early-stop.ts
  • testing/e2e/tests/ollama-early-stop.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR, @serhiizghama! 🙌 @tombeckenham will take a look.

Automated pre-review checks

  • ✅ CI passing
  • ✅ No merge conflicts
  • ✅ Changeset present
  • ✅ E2E test changes included

Automated triage — a human review follows.

@github-actions github-actions Bot added the waiting-on: maintainer The ball is in the maintainers’ court label Dec 3, 2026

@tombeckenham tombeckenham left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the early-stop leak. I checked both hunks against the ollama SDK: response.abort() now runs when the consumer stops, AbortSignal.any keeps the SDK fetch signal so that abort still closes the socket, and the caller signal stays reusable. The unit and e2e coverage match that.

@nx-cloud

nx-cloud Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit b0d1cd4

Command Status Duration Result
nx run-many --targets=build --exclude=examples/... ✅ Succeeded 6s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-12-04 07:02:12 UTC

@pkg-pr-new

pkg-pr-new Bot commented Dec 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

@tanstack/ai

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@2311

@tanstack/ai-acp

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-acp@2311

@tanstack/ai-angular

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-angular@2311

@tanstack/ai-anthropic

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@2311

@tanstack/ai-bedrock

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-bedrock@2311

@tanstack/ai-byteplus

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-byteplus@2311

@tanstack/ai-cartesia

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cartesia@2311

@tanstack/ai-claude-code

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-claude-code@2311

@tanstack/ai-client

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@2311

@tanstack/ai-cloudflare

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cloudflare@2311

@tanstack/ai-code-mode

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode@2311

@tanstack/ai-code-mode-snippets

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode-snippets@2311

@tanstack/ai-codex

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-codex@2311

@tanstack/ai-cohere

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cohere@2311

@tanstack/ai-compaction

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-compaction@2311

@tanstack/ai-computer-local

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-computer-local@2311

@tanstack/ai-computer-playwright

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-computer-playwright@2311

@tanstack/ai-computer-sandbox

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-computer-sandbox@2311

@tanstack/ai-computer-use

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-computer-use@2311

@tanstack/ai-devtools-core

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@2311

@tanstack/ai-durable-stream

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-durable-stream@2311

@tanstack/ai-effect

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-effect@2311

@tanstack/ai-elevenlabs

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-elevenlabs@2311

@tanstack/ai-event-client

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-event-client@2311

@tanstack/ai-fal

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-fal@2311

@tanstack/ai-gemini

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@2311

@tanstack/ai-grok

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@2311

@tanstack/ai-grok-build

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok-build@2311

@tanstack/ai-groq

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-groq@2311

@tanstack/ai-harness

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-harness@2311

@tanstack/ai-harness-cli

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-harness-cli@2311

@tanstack/ai-isolate-cloudflare

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-cloudflare@2311

@tanstack/ai-isolate-daytona

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-daytona@2311

@tanstack/ai-isolate-e2b

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-e2b@2311

@tanstack/ai-isolate-node

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-node@2311

@tanstack/ai-isolate-quickjs

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs@2311

@tanstack/ai-isolate-quickjs-bun

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs-bun@2311

@tanstack/ai-llmgateway

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-llmgateway@2311

@tanstack/ai-lovable

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-lovable@2311

@tanstack/ai-mcp

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp@2311

@tanstack/ai-mcp-sandbox

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp-sandbox@2311

@tanstack/ai-memory

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-memory@2311

@tanstack/ai-meshy

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-meshy@2311

@tanstack/ai-mistral

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mistral@2311

@tanstack/ai-muse

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-muse@2311

@tanstack/ai-neon

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-neon@2311

@tanstack/ai-octane

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-octane@2311

@tanstack/ai-ollama

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@2311

@tanstack/ai-ollaya

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollaya@2311

@tanstack/ai-openai

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@2311

@tanstack/ai-opencode

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-opencode@2311

@tanstack/ai-openrouter

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openrouter@2311

@tanstack/ai-optimize

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-optimize@2311

@tanstack/ai-perplexity

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-perplexity@2311

@tanstack/ai-persistence

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence@2311

@tanstack/ai-preact

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@2311

@tanstack/ai-react

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@2311

@tanstack/ai-reactor

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-reactor@2311

@tanstack/ai-remix

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-remix@2311

@tanstack/ai-sandbox

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox@2311

@tanstack/ai-sandbox-blaxel

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-blaxel@2311

@tanstack/ai-sandbox-boxd

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-boxd@2311

@tanstack/ai-sandbox-cloudflare

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-cloudflare@2311

@tanstack/ai-sandbox-daytona

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-daytona@2311

@tanstack/ai-sandbox-docker

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-docker@2311

@tanstack/ai-sandbox-e2b

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-e2b@2311

@tanstack/ai-sandbox-local-process

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-local-process@2311

@tanstack/ai-sandbox-railway

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-railway@2311

@tanstack/ai-sandbox-sprites

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-sprites@2311

@tanstack/ai-sandbox-upstash-box

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-upstash-box@2311

@tanstack/ai-sandbox-vercel

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-vercel@2311

@tanstack/ai-skills

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-skills@2311

@tanstack/ai-solid

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@2311

@tanstack/ai-svelte

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@2311

@tanstack/ai-tripo

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-tripo@2311

@tanstack/ai-typesafe

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-typesafe@2311

@tanstack/ai-uthana

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-uthana@2311

@tanstack/ai-utils

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-utils@2311

@tanstack/ai-vercel-compat

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vercel-compat@2311

@tanstack/ai-vercel-gateway

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vercel-gateway@2311

@tanstack/ai-vertex

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vertex@2311

@tanstack/ai-vue

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@2311

@tanstack/ai-worldlabs

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-worldlabs@2311

@tanstack/ai-zai

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-zai@2311

@tanstack/openai-base

npm i https://pkg.pr.new/TanStack/ai/@tanstack/openai-base@2311

@tanstack/preact-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/preact-ai-devtools@2311

@tanstack/react-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@2311

@tanstack/solid-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@2311

@tanstack/svelte-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/svelte-ai-devtools@2311

commit: b0d1cd4

@tombeckenham
tombeckenham merged commit 70c1a64 into TanStack:main Dec 4, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-on: maintainer The ball is in the maintainers’ court

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants