Skip to content

An AI-powered search engine with a generative UI

fix(streaming): mark the research span as ERROR when the stream fails (#930)

Yoshiki Miura3021aa108a54
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 82709ca4..a5a47a1c 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -22,6 +22,7 @@ import { isUsageLogging, logUsage } from '../utils/usage-logging'
import { compactHistoricalMessages } from './helpers/compact-historical-messages'
import { convertDataPart } from './helpers/convert-data-part'
+import { describeStreamError } from './helpers/describe-stream-error'
import { logAPICallErrorDiagnostics } from './helpers/log-api-call-error'
import { persistStreamResults } from './helpers/persist-stream-results'
import { prepareMessages } from './helpers/prepare-messages'
@@ -80,9 +81,17 @@ export async function createChatStreamResponse(
// Real OTel trace ID, stored in message metadata so feedback scores can
// be attached to this trace later
const parentTraceId = rootSpan?.traceId
+ let hasStreamError = false
+ let streamError: unknown
const endTracing = async () => {
if (rootSpan) {
+ if (hasStreamError) {
+ rootSpan.update({
+ level: 'ERROR',
+ statusMessage: describeStreamError(streamError)
+ })
+ }
rootSpan.end()
await langfuseSpanProcessor.forceFlush()
}
@@ -215,6 +224,8 @@ export async function createChatStreamResponse(
}
},
onError: (error: unknown) => {
+ hasStreamError = true
+ streamError = error
logAPICallErrorDiagnostics(error)
console.error('Stream response error:', error)
return serializePublicError(error)
@@ -222,6 +233,8 @@ export async function createChatStreamResponse(
consumeSseStream: consumeStream
})
} catch (error) {
+ hasStreamError = true
+ streamError = error
await endTracing()
logAPICallErrorDiagnostics(error)
console.error('Stream execution error:', error)
diff --git a/lib/streaming/create-ephemeral-chat-stream-response.ts b/lib/streaming/create-ephemeral-chat-stream-response.ts
index d72410eb..3a831523 100644
--- a/lib/streaming/create-ephemeral-chat-stream-response.ts
+++ b/lib/streaming/create-ephemeral-chat-stream-response.ts
@@ -19,6 +19,7 @@ import { isUsageLogging, logUsage } from '../utils/usage-logging'
import { compactHistoricalMessages } from './helpers/compact-historical-messages'
import { convertDataPart } from './helpers/convert-data-part'
+import { describeStreamError } from './helpers/describe-stream-error'
import { logAPICallErrorDiagnostics } from './helpers/log-api-call-error'
import { stripSpecFromMessages } from './helpers/strip-spec-from-messages'
import { BaseStreamConfig } from './types'
@@ -49,9 +50,17 @@ export async function createEphemeralChatStreamResponse(
// Real OTel trace ID, sent to the client in message metadata so feedback
// scores can be attached to this trace later
const parentTraceId = rootSpan?.traceId
+ let hasStreamError = false
+ let streamError: unknown
const endTracing = async () => {
if (rootSpan) {
+ if (hasStreamError) {
+ rootSpan.update({
+ level: 'ERROR',
+ statusMessage: describeStreamError(streamError)
+ })
+ }
rootSpan.end()
await langfuseSpanProcessor.forceFlush()
}
@@ -113,6 +122,8 @@ 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)
@@ -120,6 +131,8 @@ export async function createEphemeralChatStreamResponse(
consumeSseStream: consumeStream
})
} catch (error) {
+ hasStreamError = true
+ streamError = error
await endTracing()
logAPICallErrorDiagnostics(error)
console.error('Ephemeral stream execution error:', error)
diff --git a/lib/streaming/helpers/__tests__/describe-stream-error.test.ts b/lib/streaming/helpers/__tests__/describe-stream-error.test.ts
new file mode 100644
index 00000000..9f30a768
--- /dev/null
+++ b/lib/streaming/helpers/__tests__/describe-stream-error.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, it } from 'vitest'
+
+import { describeStreamError } from '@/lib/streaming/helpers/describe-stream-error'
+
+describe('describeStreamError', () => {
+ it('describes a provider billing failure using the public code', () => {
+ const error = new Error(
+ JSON.stringify({
+ type: 'insufficient_quota',
+ code: 'credit_balance_exhausted',
+ message:
+ 'You have no credits remaining. Add credits to continue using the API at https://platform.openai.com/settings/organization/billing/.'
+ })
+ )
+
+ expect(describeStreamError(error)).toBe(
+ 'provider_billing: The AI service is currently unavailable.'
+ )
+ })
+
+ it('does not expose the raw provider message', () => {
+ const error = new Error(
+ 'insufficient_quota: credit_balance_exhausted for account secret-123'
+ )
+ const description = describeStreamError(error)
+
+ expect(description).toBe(
+ 'provider_quota: The AI service is currently unavailable.'
+ )
+ expect(description).not.toContain('secret-123')
+ })
+
+ it('does not expose the message of an unclassified error', () => {
+ const description = describeStreamError(
+ new Error('Unexpected failure for user-content-456')
+ )
+
+ expect(description).toBe(
+ 'unknown: We could not generate a response. Please try again.'
+ )
+ expect(description).not.toContain('user-content-456')
+ })
+
+ it('does not expose the contents of a non-Error value', () => {
+ const description = describeStreamError({
+ reason: 'provider response body credential-789',
+ requestId: 'private-request-id'
+ })
+
+ expect(description).not.toContain('credential-789')
+ expect(description).not.toContain('private-request-id')
+ expect(description.length).toBeGreaterThan(0)
+ })
+
+ it('caps a public message that is passed through verbatim', () => {
+ const error = JSON.stringify({
+ error: `Daily limit reached. ${'Please try again tomorrow. '.repeat(20)}TAIL-MARKER`,
+ code: 'rate_limit',
+ remaining: 0
+ })
+ const description = describeStreamError(error)
+
+ expect(description).toHaveLength(300)
+ expect(description.startsWith('rate_limit: Daily limit reached.')).toBe(
+ true
+ )
+ expect(description).not.toContain('TAIL-MARKER')
+ })
+})
diff --git a/lib/streaming/helpers/describe-stream-error.ts b/lib/streaming/helpers/describe-stream-error.ts
new file mode 100644
index 00000000..02b2bce2
--- /dev/null
+++ b/lib/streaming/helpers/describe-stream-error.ts
@@ -0,0 +1,11 @@
+import { toPublicErrorPayload } from '@/lib/errors/public-error'
+
+const MAX_STATUS_MESSAGE_LENGTH = 300
+
+// Only the public payload's code and message are used: that payload is already
+// sent to every client, so it carries no user content or credentials.
+export function describeStreamError(error: unknown): string {
+ const { code, error: message } = toPublicErrorPayload(error)
+
+ return `${code}: ${message}`.slice(0, MAX_STATUS_MESSAGE_LENGTH)
+}