Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cleans
  • Loading branch information
nicohrubec committed Mar 31, 2026
commit dde4896ec2468b6e9808786ced0df5830d450f3e
12 changes: 8 additions & 4 deletions packages/core/src/tracing/ai/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function setTokenUsageAttributes(
}
}

export interface StreamSpanState {
export interface StreamResponseState {
responseId?: string;
responseModel?: string;
finishReasons: string[];
Expand All @@ -119,10 +119,14 @@ export interface StreamSpanState {
}

/**
* Finalizes a streaming span by setting all accumulated response attributes and ending the span.
* Ends a streaming span by setting all accumulated response attributes and ending the span.
* Shared across OpenAI, Anthropic, and Google GenAI streaming implementations.
*/
export function finalizeStreamSpan(span: Span, state: StreamSpanState, recordOutputs: boolean): void {
export function endStreamSpan(span: Span, state: StreamResponseState, recordOutputs: boolean): void {
if (!span.isRecording()) {
return;
}

const attrs: Record<string, string | number | boolean> = {
[GEN_AI_RESPONSE_STREAMING_ATTRIBUTE]: true,
};
Expand Down Expand Up @@ -155,7 +159,7 @@ export function finalizeStreamSpan(span: Span, state: StreamSpanState, recordOut
if (recordOutputs && state.responseTexts.length) {
attrs[GEN_AI_RESPONSE_TEXT_ATTRIBUTE] = state.responseTexts.join('');
}
if (state.toolCalls.length) {
if (recordOutputs && state.toolCalls.length) {
attrs[GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE] = JSON.stringify(state.toolCalls);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/tracing/anthropic-ai/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { captureException } from '../../exports';
import { SPAN_STATUS_ERROR } from '../../tracing';
import type { Span } from '../../types-hoist/span';
import { finalizeStreamSpan } from '../ai/utils';
import { endStreamSpan } from '../ai/utils';
import type { AnthropicAiStreamingEvent } from './types';
import { mapAnthropicErrorToStatusMessage } from './utils';

Expand Down Expand Up @@ -229,7 +229,7 @@ export async function* instrumentAsyncIterableStream(
yield event;
}
} finally {
finalizeStreamSpan(span, state, recordOutputs);
endStreamSpan(span, state, recordOutputs);
}
}

Expand Down Expand Up @@ -261,7 +261,7 @@ export function instrumentMessageStream<R extends { on: (...args: unknown[]) =>
// The event fired when a message is done being streamed by the API. Corresponds to the message_stop SSE event.
// @see https://github.com/anthropics/anthropic-sdk-typescript/blob/d3be31f5a4e6ebb4c0a2f65dbb8f381ae73a9166/helpers.md?plain=1#L42-L44
stream.on('message', () => {
finalizeStreamSpan(span, state, recordOutputs);
endStreamSpan(span, state, recordOutputs);
});

stream.on('error', (error: unknown) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/tracing/google-genai/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { captureException } from '../../exports';
import { SPAN_STATUS_ERROR } from '../../tracing';
import type { Span } from '../../types-hoist/span';
import { finalizeStreamSpan } from '../ai/utils';
import { endStreamSpan } from '../ai/utils';
import type { GoogleGenAIResponse } from './types';

/**
Expand Down Expand Up @@ -127,6 +127,6 @@ export async function* instrumentStream(
yield chunk;
}
} finally {
finalizeStreamSpan(span, state, recordOutputs);
endStreamSpan(span, state, recordOutputs);
}
}
4 changes: 2 additions & 2 deletions packages/core/src/tracing/openai/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { captureException } from '../../exports';
import { SPAN_STATUS_ERROR } from '../../tracing';
import type { Span } from '../../types-hoist/span';
import { finalizeStreamSpan } from '../ai/utils';
import { endStreamSpan } from '../ai/utils';
import { RESPONSE_EVENT_TYPES } from './constants';
import type {
ChatCompletionChunk,
Expand Down Expand Up @@ -236,6 +236,6 @@ export async function* instrumentStream<T>(
}
} finally {
const allToolCalls = [...Object.values(state.chatCompletionToolCalls), ...state.responsesApiToolCalls];
finalizeStreamSpan(span, { ...state, toolCalls: allToolCalls }, recordOutputs);
endStreamSpan(span, { ...state, toolCalls: allToolCalls }, recordOutputs);
}
}
Loading