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

Showing 6 changed files with 86 additions and 8 deletions (loading 2).

x
0 of 6 viewed, 6 left
5 additions and 0 deletions.changeset/ollama-early-stop-abort.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/ai-ollama': patch
+---
+​
+Close the Ollama request when the consumer stops reading a chat stream early. The `ollama` SDK does not cancel the response body when iteration ends, and the adapter only aborted the stream for an injected client with a caller signal. In every other case, including the default `ollamaText()` and `createOllamaChat()` with no abort controller, the request stayed open after the loop exited.
15 additions and 8 deletionspackages/ai-ollama/src/adapters/text.tsModified
View file
@@ -175,7 +175,16 @@
const send = wrapFetch ? wrapFetch(fetch) : fetch
return createOllamaClient(
this.clientConfig,
- signal ? (input, init) => send(input, { ...init, signal }) : send,
+ // Keep the stream's own signal too, so its abort() still ends the request.
+ signal
+ ? (input, init) =>
+ send(input, {
+ ...init,
+ signal: init?.signal
+ ? AbortSignal.any([init.signal, signal])
+ : signal,
+ })
+ : send,
​
@@ -217,13 +226,11 @@
ended = true