From 7f530ef06c7d41b664af939b1a29836b309a3350 Mon Sep 17 00:00:00 2001 From: Harshdeep Singh Date: Sun, 28 Jun 2026 13:07:36 +0530 Subject: [PATCH 1/2] feat(ai-ollama): enhance error handling in chatStream to emit RUN_ERROR events - Added error handling in the OllamaTextAdapter's chatStream method to yield RUN_ERROR events instead of throwing errors directly. - Introduced utility functions to format error payloads and raw events for better error reporting. - Added a new test suite to verify the correct emission of RUN_ERROR events on chat stream rejections. --- packages/ai-ollama/src/adapters/text.ts | 22 ++++++++++++++++++- packages/ai-ollama/tests/text-adapter.test.ts | 21 ++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/ai-ollama/src/adapters/text.ts b/packages/ai-ollama/src/adapters/text.ts index d082db0af..b7ae351f2 100644 --- a/packages/ai-ollama/src/adapters/text.ts +++ b/packages/ai-ollama/src/adapters/text.ts @@ -1,4 +1,8 @@ import { EventType, normalizeSystemPrompts } from '@tanstack/ai' +import { + toRunErrorPayload, + toRunErrorRawEvent, +} from '@tanstack/ai/adapter-internals' import { BaseTextAdapter } from '@tanstack/ai/adapters' import { buildOllamaUsage } from '../usage' import { createOllamaClient, generateId, getOllamaHostFromEnv } from '../utils' @@ -112,11 +116,27 @@ export class OllamaTextAdapter extends BaseTextAdapter< }) yield* this.processOllamaStreamChunks(response, options, logger) } catch (error: unknown) { + const errorPayload = toRunErrorPayload( + error, + 'An unknown error occurred during the chat stream.', + ) + const rawEvent = toRunErrorRawEvent(error) logger.errors('ollama.chatStream fatal', { error, source: 'ollama.chatStream', }) - throw error + yield { + type: EventType.RUN_ERROR, + model: options.model, + timestamp: Date.now(), + message: errorPayload.message, + code: errorPayload.code, + ...(rawEvent !== undefined && { rawEvent }), + error: { + message: errorPayload.message, + code: errorPayload.code, + }, + } } } diff --git a/packages/ai-ollama/tests/text-adapter.test.ts b/packages/ai-ollama/tests/text-adapter.test.ts index f5836bd70..d8ae8beb9 100644 --- a/packages/ai-ollama/tests/text-adapter.test.ts +++ b/packages/ai-ollama/tests/text-adapter.test.ts @@ -590,3 +590,24 @@ describe('OllamaTextAdapter system prompts', () => { expect(roles).not.toContain('system') }) }) + +describe('Ollama adapter error handling', () => { + it('emits RUN_ERROR on pre-stream client.chat rejection without throwing', async () => { + chatMock.mockRejectedValueOnce(new Error('connection refused')) + const adapter = createOllamaChat('llama3.2') + const chunks = await collectStream( + adapter.chatStream({ + logger: testLogger, + model: 'llama3.2', + messages: [{ role: 'user', content: 'hi' }], + }), + ) + + expect(chunks).toHaveLength(1) + expect(chunks[0]?.type).toBe('RUN_ERROR') + if (chunks[0]?.type === 'RUN_ERROR') { + expect(chunks[0].message).toBe('connection refused') + expect(chunks[0].error?.message).toBe('connection refused') + } + }) +}) From c494bd9801f2ac53a9c236423df51781dea50593 Mon Sep 17 00:00:00 2001 From: Alem Tuzlak Date: Wed, 1 Jul 2026 13:15:03 +0200 Subject: [PATCH 2/2] chore(ai-ollama): add changeset for pre-stream RUN_ERROR handling --- .changeset/ollama-prestream-run-error.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/ollama-prestream-run-error.md diff --git a/.changeset/ollama-prestream-run-error.md b/.changeset/ollama-prestream-run-error.md new file mode 100644 index 000000000..6ccba3e9d --- /dev/null +++ b/.changeset/ollama-prestream-run-error.md @@ -0,0 +1,5 @@ +--- +'@tanstack/ai-ollama': patch +--- + +Emit a `RUN_ERROR` event instead of throwing when the Ollama text adapter's `chatStream` fails before streaming begins (e.g. the initial `client.chat` call is rejected because the host is unreachable). The error is now surfaced as a structured chunk — with `message`, `code`, and any available `rawEvent` — so failures flow through the stream consistently with other adapters rather than escaping as an unhandled exception.