Skip to content

An AI-powered search engine with a generative UI

fix(streaming): stop recoverable tool errors from failing the research trace (#934)

Yoshiki Miura92e6f6b2e776
diff --git a/lib/agents/__tests__/tool-loop-agent-on-error.test.ts b/lib/agents/__tests__/tool-loop-agent-on-error.test.ts
new file mode 100644
index 00000000..afb0bdf0
--- /dev/null
+++ b/lib/agents/__tests__/tool-loop-agent-on-error.test.ts
@@ -0,0 +1,51 @@
+import type { StreamTextOnErrorCallback } from 'ai'
+import { ToolLoopAgent } from 'ai'
+import { MockLanguageModelV3, simulateReadableStream } from 'ai/test'
+import { describe, expect, it, vi } from 'vitest'
+
+describe('ToolLoopAgent onError forwarding', () => {
+ it('forwards onError to streamText', async () => {
+ const streamError = new Error('stream failed')
+ const onError = vi.fn()
+ const agent = new ToolLoopAgent({
+ model: new MockLanguageModelV3({
+ doStream: async () => ({
+ stream: simulateReadableStream({
+ chunks: [
+ { type: 'stream-start', warnings: [] },
+ { type: 'error', error: streamError },
+ {
+ type: 'finish',
+ usage: {
+ inputTokens: {
+ total: 1,
+ noCache: 1,
+ cacheRead: 0,
+ cacheWrite: 0
+ },
+ outputTokens: {
+ total: 0,
+ text: 0,
+ reasoning: 0
+ }
+ },
+ finishReason: { unified: 'error', raw: 'mock-error' }
+ }
+ ]
+ })
+ })
+ })
+ })
+
+ const result = await agent.stream({
+ messages: [{ role: 'user', content: 'hello' }],
+ onError
+ } as Parameters<typeof agent.stream>[0] & {
+ onError: StreamTextOnErrorCallback
+ })
+ await result.consumeStream()
+
+ expect(onError).toHaveBeenCalledOnce()
+ expect(onError).toHaveBeenCalledWith({ error: streamError })
+ })
+})
diff --git a/lib/streaming/__tests__/create-ephemeral-chat-stream-response.test.ts b/lib/streaming/__tests__/create-ephemeral-chat-stream-response.test.ts
index 6f51c341..f5e83cde 100644
--- a/lib/streaming/__tests__/create-ephemeral-chat-stream-response.test.ts
+++ b/lib/streaming/__tests__/create-ephemeral-chat-stream-response.test.ts
@@ -1,18 +1,144 @@
-import { describe, expect, it } from 'vitest'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+const mocks = vi.hoisted(() => ({
+ stream: vi.fn(),
+ span: {
+ traceId: 'trace-id',
+ update: vi.fn(),
+ end: vi.fn()
+ },
+ forceFlush: vi.fn(),
+ finishPromise: Promise.resolve(),
+ serializedError: undefined as string | undefined
+}))
+
+vi.mock('ai', () => ({
+ consumeStream: vi.fn(),
+ convertToModelMessages: vi.fn(async () => []),
+ smoothStream: vi.fn()
+}))
+
+vi.mock('@langfuse/tracing', () => ({
+ propagateAttributes: vi.fn((_attributes: unknown, callback: () => unknown) =>
+ callback()
+ ),
+ startActiveObservation: vi.fn(
+ (_name: string, callback: (span: typeof mocks.span) => unknown) =>
+ callback(mocks.span)
+ )
+}))
+
+vi.mock('@/instrumentation', () => ({
+ langfuseSpanProcessor: {
+ forceFlush: mocks.forceFlush
+ }
+}))
+
+vi.mock('@/lib/agents/researcher', () => ({
+ researcher: vi.fn(() => ({ stream: mocks.stream }))
+}))
+
+vi.mock('@/lib/utils/telemetry', () => ({
+ isTracingEnabled: vi.fn(() => true)
+}))
+
+vi.mock('@/lib/utils/usage-logging', () => ({
+ isUsageLogging: vi.fn(() => false),
+ logUsage: vi.fn()
+}))
+
+import { serializePublicError } from '@/lib/errors/public-error'
import { createEphemeralChatStreamResponse } from '@/lib/streaming/create-ephemeral-chat-stream-response'
+import { describeStreamError } from '@/lib/streaming/helpers/describe-stream-error'
+
+type StreamOptions = {
+ onError: (event: { error: unknown }) => void
+}
+
+type UIMessageStreamResponseOptions = {
+ onError: (error: unknown) => string
+ onFinish: () => Promise<void>
+}
+
+function createFakeResult(toolError?: unknown) {
+ return {
+ consumeStream: vi.fn(),
+ toUIMessageStreamResponse: vi.fn(
+ (options: UIMessageStreamResponseOptions) => {
+ if (toolError) {
+ mocks.serializedError = options.onError(toolError)
+ }
+ mocks.finishPromise = options.onFinish()
+ return new Response()
+ }
+ )
+ }
+}
+
+function createConfig() {
+ return {
+ messages: [
+ {
+ id: 'message-id',
+ role: 'user' as const,
+ parts: [{ type: 'text' as const, text: 'hello' }]
+ }
+ ],
+ model: { providerId: 'openai', id: 'gpt-4o-mini' } as any,
+ abortSignal: new AbortController().signal,
+ searchMode: 'quick' as const
+ }
+}
describe('createEphemeralChatStreamResponse', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ mocks.finishPromise = Promise.resolve()
+ mocks.serializedError = undefined
+ vi.spyOn(console, 'error').mockImplementation(() => {})
+ })
+
it('returns 400 when messages are missing', async () => {
const response = await createEphemeralChatStreamResponse({
- messages: [],
- model: { providerId: 'openai', id: 'gpt-4o-mini' } as any,
- abortSignal: new AbortController().signal,
- searchMode: 'quick'
+ ...createConfig(),
+ messages: []
})
expect(response.status).toBe(400)
const text = await response.text()
expect(text).toBe('messages are required')
})
+
+ it('does not mark the span as failed for a tool error', async () => {
+ const toolError = Object.assign(new Error('HTTP 403: Forbidden'), {
+ statusCode: 403
+ })
+ mocks.stream.mockResolvedValue(createFakeResult(toolError))
+
+ await createEphemeralChatStreamResponse(createConfig())
+ await mocks.finishPromise
+
+ expect(mocks.serializedError).toBe(serializePublicError(toolError))
+ expect(mocks.span.update).not.toHaveBeenCalled()
+ expect(mocks.span.end).toHaveBeenCalledOnce()
+ expect(mocks.forceFlush).toHaveBeenCalledOnce()
+ })
+
+ it('marks the span as failed for a stream-level error', async () => {
+ const streamError = new Error('stream failed')
+ mocks.stream.mockImplementation(async (options: StreamOptions) => {
+ options.onError({ error: streamError })
+ return createFakeResult()
+ })
+
+ await createEphemeralChatStreamResponse(createConfig())
+ await mocks.finishPromise
+
+ expect(mocks.span.update).toHaveBeenCalledWith({
+ level: 'ERROR',
+ statusMessage: describeStreamError(streamError)
+ })
+ expect(mocks.span.end).toHaveBeenCalledOnce()
+ expect(mocks.forceFlush).toHaveBeenCalledOnce()
+ })
})
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index c6e3307f..94c0c49e 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -1,5 +1,6 @@
import type { LangfuseSpan } from '@langfuse/tracing'
import { propagateAttributes, startActiveObservation } from '@langfuse/tracing'
+import type { StreamTextOnErrorCallback } from 'ai'
import { consumeStream, convertToModelMessages, smoothStream } from 'ai'
import { researcher } from '@/lib/agents/researcher'
@@ -168,9 +169,15 @@ export async function createChatStreamResponse(
perfLog(
`researchAgent.stream - Start: model=${context.modelId}, searchMode=${searchMode}`
)
+ // AgentStreamParameters omits onError, but it reaches streamText where only
+ // stream errors, not recoverable tool errors, invoke it.
const result = await researchAgent.stream({
messages: modelMessages,
abortSignal,
+ onError: ({ error }) => {
+ hasStreamError = true
+ streamError = error
+ },
experimental_transform: smoothStream({ chunking: 'word' }),
...(isUsageLogging() && {
onStepFinish: step => {
@@ -181,6 +188,8 @@ export async function createChatStreamResponse(
)
}
})
+ } as Parameters<typeof researchAgent.stream>[0] & {
+ onError: StreamTextOnErrorCallback
})
result.consumeStream()
@@ -226,8 +235,6 @@ export async function createChatStreamResponse(
}
},
onError: (error: unknown) => {
- hasStreamError = true
- streamError = error
logAPICallErrorDiagnostics(error)
console.error('Stream response error:', error)
return serializePublicError(error)
diff --git a/lib/streaming/create-ephemeral-chat-stream-response.ts b/lib/streaming/create-ephemeral-chat-stream-response.ts
index de6bb529..5b892217 100644
--- a/lib/streaming/create-ephemeral-chat-stream-response.ts
+++ b/lib/streaming/create-ephemeral-chat-stream-response.ts
@@ -1,6 +1,6 @@
import type { LangfuseSpan } from '@langfuse/tracing'
import { propagateAttributes, startActiveObservation } from '@langfuse/tracing'
-import type { UIMessage } from 'ai'
+import type { StreamTextOnErrorCallback, UIMessage } from 'ai'
import { consumeStream, convertToModelMessages, smoothStream } from 'ai'
import { researcher } from '@/lib/agents/researcher'
@@ -88,9 +88,15 @@ export async function createEphemeralChatStreamResponse(
})
const modelId = `${model.providerId}:${model.id}`
+ // AgentStreamParameters omits onError, but it reaches streamText where only
+ // stream errors, not recoverable tool errors, invoke it.
const result = await researchAgent.stream({
messages: modelMessages,
abortSignal,
+ onError: ({ error }) => {
+ hasStreamError = true
+ streamError = error
+ },
experimental_transform: smoothStream({ chunking: 'word' }),
...(isUsageLogging() && {
onStepFinish: step => {
@@ -101,6 +107,8 @@ export async function createEphemeralChatStreamResponse(
)
}
})
+ } as Parameters<typeof researchAgent.stream>[0] & {
+ onError: StreamTextOnErrorCallback
})
result.consumeStream()
@@ -124,8 +132,6 @@ export async function createEphemeralChatStreamResponse(
await endTracing()
},
onError: (error: unknown) => {
- hasStreamError = true
- streamError = error
logAPICallErrorDiagnostics(error)
console.error('Ephemeral stream response error:', error)
return serializePublicError(error)