Skip to content

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

fix(otel): parse prompt version attributes as integers (#16805)

David Traina6d8ffefd0a9d
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 98da76e795..5334f756c1 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -593,13 +593,7 @@ export class OtelIngestionProcessor {
null)
: null,
promptVersion: canLinkPrompt
- ? (spanAttributes?.[
- LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION
- ] ??
- spanAttributes["langfuse.prompt.version"] ??
- this.parseLangfusePromptFromAISDK(spanAttributes)
- ?.version ??
- null)
+ ? this.extractPromptVersion(spanAttributes)
: null,
modelParameters: this.extractModelParameters(
@@ -1277,12 +1271,7 @@ export class OtelIngestionProcessor {
null)
: null,
promptVersion: canLinkPrompt
- ? (attributes?.[
- LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION
- ] ??
- attributes["langfuse.prompt.version"] ??
- this.parseLangfusePromptFromAISDK(attributes)?.version ??
- null)
+ ? this.extractPromptVersion(attributes)
: null,
usageDetails: isAiSdkAgentSpan
? {}
@@ -3526,6 +3515,25 @@ export class OtelIngestionProcessor {
return undefined;
}
+ /**
+ * OTLP exporters may carry the prompt version as a stringValue; downstream
+ * schemas require an integer. Non-integer values become null.
+ */
+ private extractPromptVersion(
+ attributes: Record<string, unknown>,
+ ): number | null {
+ const raw =
+ attributes[LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION] ??
+ attributes["langfuse.prompt.version"] ??
+ this.parseLangfusePromptFromAISDK(attributes)?.version;
+
+ if (typeof raw === "number") return Number.isInteger(raw) ? raw : null;
+ if (typeof raw === "string" && /^\d+$/.test(raw.trim())) {
+ return Number(raw);
+ }
+ return null;
+ }
+
private parseLangfusePromptFromAISDK(
attributes: Record<string, unknown>,
): { name: string; version: number } | undefined {
diff --git a/packages/shared/src/server/otel/internalAiFeatureOtelWriter.test.ts b/packages/shared/src/server/otel/internalAiFeatureOtelWriter.test.ts
index 176de5ef8a..d88dcce4a3 100644
--- a/packages/shared/src/server/otel/internalAiFeatureOtelWriter.test.ts
+++ b/packages/shared/src/server/otel/internalAiFeatureOtelWriter.test.ts
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createIngestionEventSchema } from "../ingestion/types";
import { buildInternalTraceEventInputs } from "../llm/internalTraceEvents";
import {
AI_FEATURE_OTEL_SDK_NAME,
@@ -88,6 +89,8 @@ const processedEvents = [
endTime: new Date(END_ISO),
model: "claude-opus",
usageDetails: { input: 10, output: 5, total: 15 },
+ promptName: "in-app-agent-system",
+ promptVersion: 3,
},
},
];
@@ -107,9 +110,7 @@ const spanAttributeMap = (span: {
]),
);
-const processPublishedSpans = async (sdkName = AI_FEATURE_OTEL_SDK_NAME) => {
- const resourceSpans = getPublishedResourceSpans();
-
+const createProcessor = async (sdkName = AI_FEATURE_OTEL_SDK_NAME) => {
const { OtelIngestionProcessor } = await vi.importActual<
typeof import("./OtelIngestionProcessor")
>("./OtelIngestionProcessor");
@@ -120,7 +121,22 @@ const processPublishedSpans = async (sdkName = AI_FEATURE_OTEL_SDK_NAME) => {
sdkVersion: "unknown",
ingestionVersion: "4",
isLangfuseInternal: false,
- }).processToEvent(resourceSpans);
+ });
+};
+
+const processPublishedSpans = async (sdkName = AI_FEATURE_OTEL_SDK_NAME) => {
+ const resourceSpans = getPublishedResourceSpans();
+ return (await createProcessor(sdkName)).processToEvent(resourceSpans);
+};
+
+const processPublishedIngestionEvents = async () => {
+ const resourceSpans = getPublishedResourceSpans();
+ const processor = await createProcessor();
+ vi.spyOn(
+ processor as unknown as { getSeenTracesSet: () => Promise<Set<string>> },
+ "getSeenTracesSet",
+ ).mockResolvedValue(new Set());
+ return processor.processToIngestionEvents(resourceSpans);
};
const publishFixture = async () => {
@@ -193,6 +209,8 @@ describe("publishAiFeatureTraceViaOtelIngestion", () => {
modelName: "claude-opus",
userId: "user-1",
sessionId: "conversation-1",
+ promptName: "in-app-agent-system",
+ promptVersion: 3,
});
expect(tool).toMatchObject({
traceId: TRACE_ID,
@@ -248,24 +266,7 @@ describe("publishAiFeatureTraceViaOtelIngestion", () => {
it("does not emit named trace-create rewrites from child spans", async () => {
await publishFixture();
- const resourceSpans = getPublishedResourceSpans();
- const { OtelIngestionProcessor } = await vi.importActual<
- typeof import("./OtelIngestionProcessor")
- >("./OtelIngestionProcessor");
- const processor = new OtelIngestionProcessor({
- projectId: "project-1",
- publicKey: "",
- sdkName: AI_FEATURE_OTEL_SDK_NAME,
- sdkVersion: "unknown",
- ingestionVersion: "4",
- isLangfuseInternal: false,
- });
- vi.spyOn(
- processor as unknown as { getSeenTracesSet: () => Promise<Set<string>> },
- "getSeenTracesSet",
- ).mockResolvedValue(new Set());
-
- const events = await processor.processToIngestionEvents(resourceSpans);
+ const events = await processPublishedIngestionEvents();
const namedTraceCreates = events.filter(
(event) =>
event.type === "trace-create" &&
@@ -279,6 +280,22 @@ describe("publishAiFeatureTraceViaOtelIngestion", () => {
});
});
+ it("links the generation prompt as an integer version in legacy ingestion events", async () => {
+ await publishFixture();
+
+ const events = await processPublishedIngestionEvents();
+ const generation = events.find(
+ (event) => (event.body as { id?: string }).id === `${RUN_ID}-llm-0`,
+ );
+
+ const parsed = createIngestionEventSchema(false).safeParse(generation);
+ expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true);
+ expect(parsed.data?.body).toMatchObject({
+ promptName: "in-app-agent-system",
+ promptVersion: 3,
+ });
+ });
+
it("skips an empty input", async () => {
await publishAiFeatureTraceViaOtelIngestion({
projectId: "project-1",
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index 83c1dad292..d105411aaf 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -4209,6 +4209,38 @@ describe("OTel Resource Span Mapping", () => {
expect(eventInputs[0].promptVersion).toBe(1);
});
+ it("should map string-encoded prompt version on generation observations", async () => {
+ const resourceSpan = buildResourceSpan([
+ {
+ key: "langfuse.observation.type",
+ value: { stringValue: "generation" },
+ },
+ {
+ key: "langfuse.observation.prompt.name",
+ value: { stringValue: "my-prompt" },
+ },
+ {
+ key: "langfuse.observation.prompt.version",
+ value: { stringValue: "3" },
+ },
+ ]);
+
+ const events = await convertOtelSpanToIngestionEvent(
+ resourceSpan,
+ new Set(),
+ );
+ const observation = events.find((e) => e.type !== "trace-create");
+ expect(observation?.type).toBe("generation-create");
+ expect(observation?.body.promptName).toBe("my-prompt");
+ expect(observation?.body.promptVersion).toBe(3);
+
+ const processor = createTestOtelProcessor();
+ const eventInputs = processor.processToEvent([resourceSpan]);
+ expect(eventInputs).toHaveLength(1);
+ expect(eventInputs[0].promptName).toBe("my-prompt");
+ expect(eventInputs[0].promptVersion).toBe(3);
+ });
+
it("should map legacy langfuse.prompt.name attributes on generation observations", async () => {
const resourceSpan = buildResourceSpan([
{