From 7f530ef06c7d41b664af939b1a29836b309a3350 Mon Sep 17 00:00:00 2001 From: Harshdeep Singh Date: Sun, 28 Jun 2026 13:07:36 +0530 Subject: [PATCH] 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') + } + }) +})