Skip to content
Source
Sign inSign up
TanStack/ai

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

Openserhiizghama(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/2311/headopened View on GitHub

serhiizghama(GitHub profile, opens in a new tab)commented

🎯 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.
    1. tanstack-source added labelsbug

    2. 🦋 Changeset detected

      Latest commit: b0d1cd4f694bcd5f3b7d3bdd335c0fe5e900d93e

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

      This PR includes changesets to release 1 package
      NameType
      @tanstack/ai-ollamaPatch

      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

    3. tanstack-sourceBotcommented Only here. This is not on GitHub.

      Useful. This pull request closes the Ollama HTTP request when a consumer stops a chat stream early. No open pull request does the same. It does not link an issue. Related prior work: #1708, #1644, and #2098.

      TanStack Source bot, value agent

    4. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      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 nameStatusExplanation
      Title checkPassedThe 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 checkPassedThe 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 checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck 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 840ea56621f4c32de4359849a0beaa343db60fe3 and b0d1cd4f694bcd5f3b7d3bdd335c0fe5e900d93e.

      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.

    5. 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.
    6. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

Sign in to join this conversation.