Skip to content

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

chore: move obsevation type setting override to mapper (#8714)

Nimar816abfb59eb4
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index 6155705826..15442c7eff 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -3876,14 +3876,30 @@ describe("OTel Resource Span Mapping", () => {
expect(traceEvents.length).toBe(1);
});
- it("should override the observation type if it is declared as 'span' but holds generation-like attributes", async () => {
+ it("should override the observation type if it is declared as 'span' but holds generation-like attributes for python-sdk <= 3.3.0", async () => {
// Issue: https://github.com/langfuse/langfuse/issues/8682
const otelSpans = [
{
- resource: { attributes: [] },
+ resource: {
+ attributes: [
+ {
+ key: "telemetry.sdk.language",
+ value: { stringValue: "python" },
+ },
+ ],
+ },
scopeSpans: [
{
- scope: { name: "test-scope" },
+ scope: {
+ name: "langfuse-sdk",
+ version: "3.3.0",
+ attributes: [
+ {
+ key: "public_key",
+ value: { stringValue: "pk-lf-1234567890" },
+ },
+ ],
+ },
spans: [
{
traceId: {
diff --git a/web/src/features/otel/server/ObservationTypeMapper.ts b/web/src/features/otel/server/ObservationTypeMapper.ts
index ef3b834d94..81f0e4e934 100644
--- a/web/src/features/otel/server/ObservationTypeMapper.ts
+++ b/web/src/features/otel/server/ObservationTypeMapper.ts
@@ -6,15 +6,18 @@ type LangfuseObservationType = keyof typeof ObservationType;
interface ObservationTypeMapper {
readonly name: string;
readonly priority: number; // Lower numbers = higher priority
- canMap(attributes: Record<string, unknown>): boolean;
+ canMap(
+ attributes: Record<string, unknown>,
+ resourceAttributes?: Record<string, unknown>,
+ scopeData?: Record<string, unknown>,
+ ): boolean;
mapToObservationType(
attributes: Record<string, unknown>,
+ resourceAttributes?: Record<string, unknown>,
+ scopeData?: Record<string, unknown>,
): LangfuseObservationType | null;
}
-/**
- * Simple mapper for direct attribute key-value mappings.
- */
class SimpleAttributeMapper implements ObservationTypeMapper {
constructor(
public readonly name: string,
@@ -23,7 +26,11 @@ class SimpleAttributeMapper implements ObservationTypeMapper {
private readonly mappings: Record<string, string>,
) {}
- canMap(attributes: Record<string, unknown>): boolean {
+ canMap(
+ attributes: Record<string, unknown>,
+ _resourceAttributes?: Record<string, unknown>,
+ _scopeData?: Record<string, unknown>,
+ ): boolean {
return (
this.attributeKey in attributes && attributes[this.attributeKey] != null
);
@@ -31,6 +38,8 @@ class SimpleAttributeMapper implements ObservationTypeMapper {
mapToObservationType(
attributes: Record<string, unknown>,
+ _resourceAttributes?: Record<string, unknown>,
+ _scopeData?: Record<string, unknown>,
): LangfuseObservationType | null {
const value = attributes[this.attributeKey] as string;
const mappedType = this.mappings[value];
@@ -53,20 +62,32 @@ class CustomAttributeMapper implements ObservationTypeMapper {
constructor(
public readonly name: string,
public readonly priority: number,
- private readonly canMapFn: (attributes: Record<string, unknown>) => boolean,
+ private readonly canMapFn: (
+ attributes: Record<string, unknown>,
+ resourceAttributes?: Record<string, unknown>,
+ scopeData?: Record<string, unknown>,
+ ) => boolean,
private readonly mapFn: (
attributes: Record<string, unknown>,
+ resourceAttributes?: Record<string, unknown>,
+ scopeData?: Record<string, unknown>,
) => LangfuseObservationType | null,
) {}
- canMap(attributes: Record<string, unknown>): boolean {
- return this.canMapFn(attributes);
+ canMap(
+ attributes: Record<string, unknown>,
+ resourceAttributes?: Record<string, unknown>,
+ scopeData?: Record<string, unknown>,
+ ): boolean {
+ return this.canMapFn(attributes, resourceAttributes, scopeData);
}
mapToObservationType(
attributes: Record<string, unknown>,
+ resourceAttributes?: Record<string, unknown>,
+ scopeData?: Record<string, unknown>,
): LangfuseObservationType | null {
- const result = this.mapFn(attributes);
+ const result = this.mapFn(attributes, resourceAttributes, scopeData);
if (
result &&
@@ -89,7 +110,76 @@ class CustomAttributeMapper implements ObservationTypeMapper {
*/
export class ObservationTypeMapperRegistry {
private readonly mappers: ObservationTypeMapper[] = [
- new SimpleAttributeMapper("OpenInference", 1, "openinference.span.kind", {
+ // Priority 0: Python SDK <= 3.3.0 override
+ // If generation-like attributes are set even though observation type is span, override to 'generation'
+ // Issue: https://github.com/langfuse/langfuse/issues/8682
+ // Affected SDK versions: Python SDK <= 3.3.0
+ new CustomAttributeMapper(
+ "PythonSDKv330Override",
+ 0, // Priority
+ // canMap?
+ (attributes, resourceAttributes, scopeData) => {
+ return (
+ attributes[LangfuseOtelSpanAttributes.OBSERVATION_TYPE] === "span" &&
+ scopeData?.name === "langfuse-sdk" &&
+ resourceAttributes?.["telemetry.sdk.language"] === "python"
+ );
+ },
+ // map!
+ (attributes, resourceAttributes, scopeData) => {
+ // Check version <= 3.3.0
+ const scopeVersion = scopeData?.version as string;
+ if (scopeVersion) {
+ const [major, minor] = scopeVersion.split(".").map(Number);
+ if (major > 3 || (major === 3 && minor > 3)) {
+ return null;
+ }
+ }
+
+ // Check for generation-like attributes
+ const generationKeys = [
+ LangfuseOtelSpanAttributes.OBSERVATION_MODEL,
+ LangfuseOtelSpanAttributes.OBSERVATION_COST_DETAILS,
+ LangfuseOtelSpanAttributes.OBSERVATION_USAGE_DETAILS,
+ LangfuseOtelSpanAttributes.OBSERVATION_COMPLETION_START_TIME,
+ LangfuseOtelSpanAttributes.OBSERVATION_MODEL_PARAMETERS,
+ LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_NAME,
+ LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION,
+ ];
+
+ const hasGenerationAttributes = Object.keys(attributes).some((key) =>
+ generationKeys.includes(key as any),
+ );
+
+ if (hasGenerationAttributes) {
+ return "GENERATION";
+ }
+
+ return null;
+ },
+ ),
+
+ // Priority 1: maps langfuse.observation.type directly
+ new SimpleAttributeMapper(
+ "LangfuseObservationTypeDirectMapping",
+ 1,
+ LangfuseOtelSpanAttributes.OBSERVATION_TYPE,
+ {
+ span: "SPAN",
+ generation: "GENERATION",
+ embedding: "EMBEDDING",
+ agent: "AGENT",
+ tool: "TOOL",
+ chain: "CHAIN",
+ retriever: "RETRIEVER",
+ guardrail: "GUARDRAIL",
+ evaluator: "EVALUATOR",
+ },
+ ),
+
+ new SimpleAttributeMapper("OpenInference", 2, "openinference.span.kind", {
+ // Format:
+ // OpenInference Value: Langfuse ObservationType
CHAIN: "CHAIN",
RETRIEVER: "RETRIEVER",
LLM: "GENERATION",
@@ -102,9 +192,11 @@ export class ObservationTypeMapperRegistry {
new SimpleAttributeMapper(
"OTel_GenAI_Operation",
- 2,
+ 3,
"gen_ai.operation.name",
{
+ // Format:
+ // GenAI Value: Langfuse ObservationType
chat: "GENERATION",
completion: "GENERATION",
generate_content: "GENERATION",
@@ -116,7 +208,9 @@ export class ObservationTypeMapperRegistry {
},
),
- new SimpleAttributeMapper("Vercel_AI_SDK_Operation", 3, "operation.name", {
+ new SimpleAttributeMapper("Vercel_AI_SDK_Operation", 4, "operation.name", {
+ // Format:
+ // Vercel AI SDK Value: Langfuse ObservationType
"ai.generateText": "GENERATION",
"ai.generateText.doGenerate": "GENERATION",
"ai.streamText": "GENERATION",
@@ -134,8 +228,8 @@ export class ObservationTypeMapperRegistry {
new CustomAttributeMapper(
"ModelBased",
- 4,
- (attributes) => {
+ 5,
+ (attributes, _resourceAttributes, _scopeData) => {
const modelKeys = [
LangfuseOtelSpanAttributes.OBSERVATION_MODEL,
"gen_ai.request.model",
@@ -160,18 +254,19 @@ export class ObservationTypeMapperRegistry {
return this.sortedMappersCache;
}
- /**
- * Maps span attributes to a Langfuse observation type.
- * Returns null if no mapper can handle the attributes.
- */
mapToObservationType(
attributes: Record<string, unknown>,
+ resourceAttributes?: Record<string, unknown>,
+ scopeData?: Record<string, unknown>,
): LangfuseObservationType | null {
const sortedMappers = this.getSortedMappers();
-
for (const mapper of sortedMappers) {
- if (mapper.canMap(attributes)) {
- const result = mapper.mapToObservationType(attributes);
+ if (mapper.canMap(attributes, resourceAttributes, scopeData)) {
+ const result = mapper.mapToObservationType(
+ attributes,
+ resourceAttributes,
+ scopeData,
+ );
if (result) {
return result;
}
diff --git a/web/src/features/otel/server/OtelIngestionProcessor.ts b/web/src/features/otel/server/OtelIngestionProcessor.ts
index a545ebac86..26dce0f4fb 100644
--- a/web/src/features/otel/server/OtelIngestionProcessor.ts
+++ b/web/src/features/otel/server/OtelIngestionProcessor.ts
@@ -641,38 +641,9 @@ export class OtelIngestionProcessor {
}),
};
- let observationType = attributes[
- LangfuseOtelSpanAttributes.OBSERVATION_TYPE
- ] as string;
-
- // If generation-like attributes are set even though observation type is span, override to 'generation'
- // Issue: https://github.com/langfuse/langfuse/issues/8682
- // Affected SDK versions: Python SDK <= 3.3.0
- const hasGenerationAttributes = Object.keys(attributes).some((key) => {
- const generationKeys: LangfuseOtelSpanAttributes[] = [
- LangfuseOtelSpanAttributes.OBSERVATION_MODEL,
- LangfuseOtelSpanAttributes.OBSERVATION_COST_DETAILS,
- LangfuseOtelSpanAttributes.OBSERVATION_USAGE_DETAILS,
- LangfuseOtelSpanAttributes.OBSERVATION_COMPLETION_START_TIME,
- LangfuseOtelSpanAttributes.OBSERVATION_MODEL_PARAMETERS,
- LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_NAME,
- LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION,
- ];
-
- return generationKeys.includes(key as any);
- });
-
- if (observationType === "span" && hasGenerationAttributes) {
- observationType = "generation";
- }
-
- // If no explicit observation type, try mapping from various frameworks
- if (!observationType) {
- const mappedType = observationTypeMapper.mapToObservationType(attributes);
- if (mappedType) {
- observationType = mappedType.toLowerCase();
- }
- }
+ const observationType = observationTypeMapper
+ .mapToObservationType(attributes, resourceAttributes, scopeSpan?.scope)
+ ?.toLowerCase();
const isKnownObservationType =
observationType &&
@@ -680,7 +651,7 @@ export class OtelIngestionProcessor {
const getIngestionEventType = (): string => {
if (isKnownObservationType) {
- return `${observationType.toLowerCase()}-create`;
+ return `${observationType}-create`;
}
return "span-create";
};