Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(otel): derive completionStartTime from AI SDK 7 time_to_first_chunk (#18875)

Hassieb Pakzadd743fecf2111
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 50f1de4368..a5c0502175 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -3495,11 +3495,17 @@ export class OtelIngestionProcessor {
// Fallthrough
}
- // Vercel AI SDK
+ // Vercel AI SDK: <= v6 reports milliseconds, v7 reports the GenAI
+ // semconv metric name in seconds.
try {
+ const secondsToFirstChunk =
+ attributes["gen_ai.client.operation.time_to_first_chunk"];
const msToFirstChunk =
attributes["ai.response.msToFirstChunk"] ??
- attributes["ai.stream.msToFirstChunk"];
+ attributes["ai.stream.msToFirstChunk"] ??
+ (secondsToFirstChunk != null && secondsToFirstChunk !== ""
+ ? Number(secondsToFirstChunk) * 1000
+ : undefined);
if (msToFirstChunk && startTimeISO) {
const msToFirstChunkNumber = Math.ceil(Number(msToFirstChunk));
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index bbc2a044b2..fd6ec8b214 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -1543,6 +1543,64 @@ describe("OTel Resource Span Mapping", () => {
],
});
+ it.each([
+ [
+ "AI SDK 6 ai.response.msToFirstChunk (milliseconds)",
+ "ai.response.msToFirstChunk",
+ { doubleValue: 250.4 },
+ ],
+ [
+ "AI SDK 7 gen_ai.client.operation.time_to_first_chunk (seconds)",
+ "gen_ai.client.operation.time_to_first_chunk",
+ { doubleValue: 0.2505 },
+ ],
+ ])(
+ "should derive completionStartTime from %s",
+ async (_name, otelAttributeKey, otelAttributeValue) => {
+ const resourceSpan = {
+ scopeSpans: [
+ {
+ scope: { name: "ai" },
+ spans: [
+ {
+ ...defaultSpanProps,
+ name: "ai.streamText.doStream",
+ attributes: [
+ {
+ key: "ai.operationId",
+ value: { stringValue: "ai.streamText.doStream" },
+ },
+ { key: "ai.model.id", value: { stringValue: "gpt-4o" } },
+ { key: otelAttributeKey, value: otelAttributeValue },
+ ],
+ },
+ ],
+ },
+ ],
+ } as unknown as ResourceSpan;
+
+ const ttftMs = (startTime?: string, completionStartTime?: string) =>
+ new Date(completionStartTime!).getTime() -
+ new Date(startTime!).getTime();
+
+ const ingestionEvents = await convertOtelSpanToIngestionEvent(
+ resourceSpan,
+ new Set(),
+ );
+ const generation = ingestionEvents.find(
+ (event) => event.type === "generation-create",
+ )?.body as { startTime?: string; completionStartTime?: string };
+ expect(
+ ttftMs(generation?.startTime, generation?.completionStartTime),
+ ).toBe(251);
+
+ const [event] = createTestOtelProcessor().processToEvent([
+ resourceSpan,
+ ]);
+ expect(ttftMs(event.startTimeISO, event.completionStartTime)).toBe(251);
+ },
+ );
+
it("should interpret an empty buffer as an unset parentSpanId", async () => {
// https://github.com/langchain4j/langchain4j/issues/2328#issuecomment-2686129552
// Empty buffers where detected as truthy, i.e. behaved like they had a parent span.