Skip to content

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

fix(otel): link prompts only on generation observations (#14962)

Hassieb Pakzada0aada8f86be
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 400bc87480..a7d53f0e0d 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -3,6 +3,7 @@ import { randomUUID } from "crypto";
import {
ForbiddenError,
ObservationLevel,
+ ObservationType,
ObservationTypeDomain,
} from "../../";
import {
@@ -400,6 +401,17 @@ export class OtelIngestionProcessor {
? normalizedTools.toolCallNames
: undefined;
+ const observationType =
+ observationTypeMapper.mapToObservationType(
+ spanAttributes,
+ resourceAttributes,
+ scopeSpan?.scope,
+ span.name,
+ );
+ // Prompts can only be linked to GENERATION observations
+ const canLinkPrompt =
+ observationType === ObservationType.GENERATION;
+
events.push({
projectId: this.projectId,
traceId,
@@ -407,12 +419,7 @@ export class OtelIngestionProcessor {
parentSpanId,
name,
- type: observationTypeMapper.mapToObservationType(
- spanAttributes,
- resourceAttributes,
- scopeSpan?.scope,
- span.name,
- ),
+ type: observationType,
environment: this.extractEnvironment(
spanAttributes,
resourceAttributes,
@@ -442,21 +449,23 @@ export class OtelIngestionProcessor {
span.status?.message ??
null,
- promptName:
- spanAttributes?.[
- LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_NAME
- ] ??
- spanAttributes["langfuse.prompt.name"] ??
- this.parseLangfusePromptFromAISDK(spanAttributes)?.name ??
- null,
- promptVersion:
- spanAttributes?.[
- LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION
- ] ??
- spanAttributes["langfuse.prompt.version"] ??
- this.parseLangfusePromptFromAISDK(spanAttributes)
- ?.version ??
- null,
+ promptName: canLinkPrompt
+ ? (spanAttributes?.[
+ LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_NAME
+ ] ??
+ spanAttributes["langfuse.prompt.name"] ??
+ this.parseLangfusePromptFromAISDK(spanAttributes)?.name ??
+ null)
+ : null,
+ promptVersion: canLinkPrompt
+ ? (spanAttributes?.[
+ LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION
+ ] ??
+ spanAttributes["langfuse.prompt.version"] ??
+ this.parseLangfusePromptFromAISDK(spanAttributes)
+ ?.version ??
+ null)
+ : null,
modelParameters: this.extractModelParameters(
spanAttributes,
@@ -1026,6 +1035,15 @@ export class OtelIngestionProcessor {
// model-call spans — skip model/usage/cost for them.
const isAiSdkAgentSpan = this.isAiSdkAgentOperation(attributes);
+ const mappedObservationType = observationTypeMapper.mapToObservationType(
+ attributes,
+ resourceAttributes,
+ scopeSpan?.scope,
+ span.name,
+ );
+ // Prompts can only be linked to GENERATION observations
+ const canLinkPrompt = mappedObservationType === ObservationType.GENERATION;
+
const observation = {
id: this.parseId(span.spanId?.data ?? span.spanId),
traceId,
@@ -1060,16 +1078,20 @@ export class OtelIngestionProcessor {
instrumentationScopeName,
) as any,
model: isAiSdkAgentSpan ? undefined : this.extractModelName(attributes),
- promptName:
- attributes?.[LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_NAME] ??
- attributes["langfuse.prompt.name"] ??
- this.parseLangfusePromptFromAISDK(attributes)?.name ??
- null,
- promptVersion:
- attributes?.[LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION] ??
- attributes["langfuse.prompt.version"] ??
- this.parseLangfusePromptFromAISDK(attributes)?.version ??
- null,
+ promptName: canLinkPrompt
+ ? (attributes?.[LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_NAME] ??
+ attributes["langfuse.prompt.name"] ??
+ this.parseLangfusePromptFromAISDK(attributes)?.name ??
+ null)
+ : null,
+ promptVersion: canLinkPrompt
+ ? (attributes?.[
+ LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION
+ ] ??
+ attributes["langfuse.prompt.version"] ??
+ this.parseLangfusePromptFromAISDK(attributes)?.version ??
+ null)
+ : null,
usageDetails: isAiSdkAgentSpan
? {}
: this.extractUsageDetails(
@@ -1084,12 +1106,6 @@ export class OtelIngestionProcessor {
output,
};
- const mappedObservationType = observationTypeMapper.mapToObservationType(
- attributes,
- resourceAttributes,
- scopeSpan?.scope,
- span.name,
- );
const observationType =
mappedObservationType && typeof mappedObservationType === "string"
? mappedObservationType.toLowerCase()
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index 328191103f..9661a9b708 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -2966,13 +2966,13 @@ describe("OTel Resource Span Mapping", () => {
},
],
[
- "should extract promptName on observation from langfuse.prompt.name",
+ "should not extract promptName from langfuse.prompt.name on non-generation observation",
{
entity: "observation",
otelAttributeKey: "langfuse.prompt.name",
otelAttributeValue: { stringValue: "test" },
entityAttributeKey: "promptName",
- entityAttributeValue: "test",
+ entityAttributeValue: null,
},
],
[
@@ -3879,6 +3879,111 @@ describe("OTel Resource Span Mapping", () => {
},
);
+ describe("prompt linking gated to GENERATION observations", () => {
+ const buildResourceSpan = (attributes: Record<string, any>[]) => ({
+ scopeSpans: [
+ {
+ spans: [
+ {
+ ...defaultSpanProps,
+ attributes,
+ },
+ ],
+ },
+ ],
+ });
+
+ const promptAttributes = [
+ {
+ key: "langfuse.observation.prompt.name",
+ value: { stringValue: "my-prompt" },
+ },
+ {
+ key: "langfuse.observation.prompt.version",
+ value: { intValue: { low: 1, high: 0, unsigned: false } },
+ },
+ ];
+
+ it.each([["agent"], ["tool"], ["span"]])(
+ "should not map prompt attributes on %s observations",
+ async (observationType: string) => {
+ const resourceSpan = buildResourceSpan([
+ {
+ key: "langfuse.observation.type",
+ value: { stringValue: observationType },
+ },
+ ...promptAttributes,
+ ]);
+
+ const events = await convertOtelSpanToIngestionEvent(
+ resourceSpan,
+ new Set(),
+ );
+ const observation = events.find((e) => e.type !== "trace-create");
+ expect(observation?.type).toBe(`${observationType}-create`);
+ expect(observation?.body.promptName).toBeNull();
+ expect(observation?.body.promptVersion).toBeNull();
+
+ const processor = createTestOtelProcessor();
+ const eventInputs = processor.processToEvent([resourceSpan]);
+ expect(eventInputs).toHaveLength(1);
+ expect(eventInputs[0].promptName).toBeNull();
+ expect(eventInputs[0].promptVersion).toBeNull();
+ },
+ );
+
+ it("should map prompt attributes on generation observations", async () => {
+ const resourceSpan = buildResourceSpan([
+ {
+ key: "langfuse.observation.type",
+ value: { stringValue: "generation" },
+ },
+ ...promptAttributes,
+ ]);
+
+ 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(1);
+
+ const processor = createTestOtelProcessor();
+ const eventInputs = processor.processToEvent([resourceSpan]);
+ expect(eventInputs).toHaveLength(1);
+ expect(eventInputs[0].promptName).toBe("my-prompt");
+ expect(eventInputs[0].promptVersion).toBe(1);
+ });
+
+ it("should map legacy langfuse.prompt.name attributes on generation observations", async () => {
+ const resourceSpan = buildResourceSpan([
+ {
+ key: "gen_ai.request.model",
+ value: { stringValue: "gpt-4o" },
+ },
+ {
+ key: "langfuse.prompt.name",
+ value: { stringValue: "my-prompt" },
+ },
+ {
+ key: "langfuse.prompt.version",
+ value: { intValue: { low: 2, high: 0, unsigned: false } },
+ },
+ ]);
+
+ 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(2);
+ });
+ });
+
// Regression: OTLP/JSON exporters (e.g. the OpenTelemetry PHP SDK) send
// int64 resource attributes as decimal strings (intValue: "7") rather than
// the Long object produced by the protobuf decoder. Previously these were