Skip to content

Commit 5e265b8

Browse files
fix: Remove overwriting of TTFT and RTTFT in openai streaming wrapper
1 parent d0f743b commit 5e265b8

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/instrumentation/openai/wrappers.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,29 @@ abstract class BaseStreamHandler {
186186

187187
protected finalizeSpan(code: SpanStatusCode): void {
188188
if (code === SpanStatusCode.OK) {
189-
finalizeSpanSuccess(
189+
setResponseAttributes(
190190
this.span,
191191
this.completeResponse as Record<string, unknown>,
192192
);
193-
} else {
194-
recordSpanTiming(this.span, SpanAttributes.LLM_RESPONSE_DURATION);
195-
this.span.setStatus({ code });
196-
this.span.end();
197193
}
194+
recordSpanTiming(this.span, SpanAttributes.LLM_RESPONSE_DURATION);
195+
// Only set TTFT at finalize when no content was detected during
196+
// streaming (degenerate case). When streaming produced content,
197+
// TTFT was already recorded in processChunk on the first chunk.
198+
if (!this.firstContentRecorded) {
199+
recordSpanTiming(
200+
this.span,
201+
SpanAttributes.LLM_PERFORMANCE_TIME_TO_FIRST_TOKEN,
202+
{ recordEventTimestamp: true },
203+
);
204+
recordSpanTiming(
205+
this.span,
206+
SpanAttributes.LLM_PERFORMANCE_RELATIVE_TIME_TO_FIRST_TOKEN,
207+
{ useRootSpan: true },
208+
);
209+
}
210+
this.span.setStatus({ code });
211+
this.span.end();
198212
}
199213
}
200214

0 commit comments

Comments
 (0)