Skip to content

An AI-powered search engine with a generative UI

fix(streaming): record provider API call diagnostics on the failing trace (#945)

Yoshiki Miurab3e59bbc8245
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 466fb391..6313e172 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -30,7 +30,10 @@ import {
EMPTY_RESPONSE_STATUS_MESSAGE,
isEmptyResponse
} from './helpers/is-empty-response'
-import { logAPICallErrorDiagnostics } from './helpers/log-api-call-error'
+import {
+ buildAPICallErrorDiagnostics,
+ logAPICallErrorDiagnostics
+} from './helpers/log-api-call-error'
import { persistStreamResults } from './helpers/persist-stream-results'
import { prepareMessages } from './helpers/prepare-messages'
import { stripSpecFromMessages } from './helpers/strip-spec-from-messages'
@@ -95,9 +98,11 @@ export async function createChatStreamResponse(
const endTracing = async () => {
if (rootSpan) {
if (hasStreamError) {
+ const apiCallDiagnostics = buildAPICallErrorDiagnostics(streamError)
rootSpan.update({
level: 'ERROR',
- statusMessage: describeStreamError(streamError)
+ statusMessage: describeStreamError(streamError),
+ ...(apiCallDiagnostics && { metadata: { apiCallDiagnostics } })
})
} else if (hasEmptyResponse) {
rootSpan.update({
diff --git a/lib/streaming/create-ephemeral-chat-stream-response.ts b/lib/streaming/create-ephemeral-chat-stream-response.ts
index 3b98c956..efac3c1b 100644
--- a/lib/streaming/create-ephemeral-chat-stream-response.ts
+++ b/lib/streaming/create-ephemeral-chat-stream-response.ts
@@ -26,7 +26,10 @@ import {
EMPTY_RESPONSE_STATUS_MESSAGE,
isEmptyResponse
} from './helpers/is-empty-response'
-import { logAPICallErrorDiagnostics } from './helpers/log-api-call-error'
+import {
+ buildAPICallErrorDiagnostics,
+ logAPICallErrorDiagnostics
+} from './helpers/log-api-call-error'
import { stripSpecFromMessages } from './helpers/strip-spec-from-messages'
import { BaseStreamConfig } from './types'
@@ -63,9 +66,11 @@ export async function createEphemeralChatStreamResponse(
const endTracing = async () => {
if (rootSpan) {
if (hasStreamError) {
+ const apiCallDiagnostics = buildAPICallErrorDiagnostics(streamError)
rootSpan.update({
level: 'ERROR',
- statusMessage: describeStreamError(streamError)
+ statusMessage: describeStreamError(streamError),
+ ...(apiCallDiagnostics && { metadata: { apiCallDiagnostics } })
})
} else if (hasEmptyResponse) {
rootSpan.update({
diff --git a/lib/streaming/helpers/__tests__/log-api-call-error.test.ts b/lib/streaming/helpers/__tests__/log-api-call-error.test.ts
index c4a2bdbc..27dabced 100644
--- a/lib/streaming/helpers/__tests__/log-api-call-error.test.ts
+++ b/lib/streaming/helpers/__tests__/log-api-call-error.test.ts
@@ -1,7 +1,10 @@
import { APICallError } from 'ai'
import { afterEach, describe, expect, it, vi } from 'vitest'
-import { logAPICallErrorDiagnostics } from '../log-api-call-error'
+import {
+ buildAPICallErrorDiagnostics,
+ logAPICallErrorDiagnostics
+} from '../log-api-call-error'
function createAPICallError(
requestBodyValues: unknown,
@@ -49,6 +52,10 @@ describe('logAPICallErrorDiagnostics', () => {
logAPICallErrorDiagnostics(error)
+ expect(consoleError).toHaveBeenCalledWith(
+ 'Provider API call diagnostics:',
+ '{"statusCode":400,"url":"https://provider.example/v1/messages","requestBody":{"topLevelKeys":["model","messages"],"totalSerializedSize":156,"turnsKey":"messages","turnCount":1,"turns":[{"role":"user","content":[{"type":"text","length":45},{"type":"file","length":42}]}]},"responseBody":{"length":12}}'
+ )
const output = consoleError.mock.calls.flat().join(' ')
expect(output).toContain('"statusCode":400')
expect(output).toContain('https://provider.example/v1/messages')
@@ -156,3 +163,52 @@ describe('logAPICallErrorDiagnostics', () => {
expect(diagnostics.endsWith('...[truncated]')).toBe(true)
})
})
+
+describe('buildAPICallErrorDiagnostics', () => {
+ it.each([new Error('failure'), null, undefined, 'failure'])(
+ 'returns null for a non-API call error',
+ value => {
+ expect(buildAPICallErrorDiagnostics(value)).toBeNull()
+ }
+ )
+
+ it('returns structural API call diagnostics without private content', () => {
+ const sentinel = 'SENTINEL_PRIVATE_MESSAGE_20260807'
+ const error = createAPICallError({
+ model: 'provider-model',
+ messages: [
+ {
+ role: 'user',
+ content: [
+ { type: 'text', text: sentinel },
+ { type: 'file', data: 'private file data' }
+ ]
+ }
+ ]
+ })
+
+ const diagnostics = buildAPICallErrorDiagnostics(error)
+
+ expect(diagnostics).toEqual({
+ statusCode: 400,
+ url: 'https://provider.example/v1/messages',
+ requestBody: {
+ topLevelKeys: ['model', 'messages'],
+ totalSerializedSize: 169,
+ turnsKey: 'messages',
+ turnCount: 1,
+ turns: [
+ {
+ role: 'user',
+ content: [
+ { type: 'text', length: 58 },
+ { type: 'file', length: 42 }
+ ]
+ }
+ ]
+ },
+ responseBody: { length: 12 }
+ })
+ expect(JSON.stringify(diagnostics)).not.toContain(sentinel)
+ })
+})
diff --git a/lib/streaming/helpers/log-api-call-error.ts b/lib/streaming/helpers/log-api-call-error.ts
index 6deb0580..82a48127 100644
--- a/lib/streaming/helpers/log-api-call-error.ts
+++ b/lib/streaming/helpers/log-api-call-error.ts
@@ -127,16 +127,30 @@ function summarizeResponseBody(responseBody: unknown) {
}
export function logAPICallErrorDiagnostics(error: unknown): void {
+ const diagnostics = buildAPICallErrorDiagnostics(error)
+ if (!diagnostics) return
+
try {
- if (!APICallError.isInstance(error)) return
+ console.error(
+ 'Provider API call diagnostics:',
+ truncate(JSON.stringify(diagnostics))
+ )
+ } catch {}
+}
- const diagnostics = JSON.stringify({
+export function buildAPICallErrorDiagnostics(
+ error: unknown
+): Record<string, unknown> | null {
+ try {
+ if (!APICallError.isInstance(error)) return null
+
+ return {
statusCode: error.statusCode,
url: error.url,
requestBody: summarizeRequestBody(error.requestBodyValues),
responseBody: summarizeResponseBody(error.responseBody)
- })
-
- console.error('Provider API call diagnostics:', truncate(diagnostics))
- } catch {}
+ }
+ } catch {
+ return null
+ }
}