🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(otel): map observation types for gen_ai ie pydantic (#10884)
Nimar9dd6ad09db34
diff --git a/packages/shared/src/server/otel/ObservationTypeMapper.ts b/packages/shared/src/server/otel/ObservationTypeMapper.tsindex 281856ce11..5bda89d2c6 100644--- a/packages/shared/src/server/otel/ObservationTypeMapper.ts+++ b/packages/shared/src/server/otel/ObservationTypeMapper.ts@@ -241,7 +241,9 @@ export class ObservationTypeMapperRegistry {// Format:// GenAI Value: Langfuse ObservationTypechat: "GENERATION",+ // completion was used historically (keeping it for backward compatibility), text_completion is per spec as of 2025-12-04completion: "GENERATION",+ text_completion: "GENERATION",generate_content: "GENERATION",generate: "GENERATION",embeddings: "EMBEDDING",@@ -364,9 +366,25 @@ export class ObservationTypeMapperRegistry {},),+ // GenAI tool call detection (e.g., Pydantic AI, any framework using gen_ai.tool.* attributes)+ // unfortunately, Pydantic does not set the gen_ai.operation.name attribute on tool calls+ // therefore, we need another mapper here.new CustomAttributeMapper(- "ModelBased",+ "GenAI_Tool_Call",6,+ (attributes) => {+ // Check for standard GenAI tool call attributes+ return (+ hasMeaningfulValue(attributes["gen_ai.tool.name"]) ||+ hasMeaningfulValue(attributes["gen_ai.tool.call.id"])+ );+ },+ () => "TOOL",+ ),++ new CustomAttributeMapper(+ "ModelBased",+ 7,(attributes, _resourceAttributes, _scopeData) => {const modelKeys = [LangfuseOtelSpanAttributes.OBSERVATION_MODEL,diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex 833a990ecb..a8425426ad 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -1534,6 +1534,14 @@ export class OtelIngestionProcessor {spanName: string,attributes: Record<string, unknown>,): string {+ // GenAI tool name (standard OTel GenAI attribute)+ if ("gen_ai.tool.name" in attributes && attributes["gen_ai.tool.name"]) {+ return typeof attributes["gen_ai.tool.name"] === "string"+ ? (attributes["gen_ai.tool.name"] as string)+ : JSON.stringify(attributes["gen_ai.tool.name"]);+ }++ // Logfire message for pydantic AIconst nameKeys = ["logfire.msg"];for (const key of nameKeys) {if (attributes[key]) {diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex 81ee31f0b7..e3cab7302a 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -1227,6 +1227,159 @@ describe("OTel Resource Span Mapping", () => {},);+ it("should map Pydantic AI tool call to TOOL observation type via gen_ai.tool.* attributes", async () => {+ const traceId = "abcdef1234567890abcdef1234567890";++ const pydanticAiToolSpan = {+ resource: {+ attributes: [+ {+ key: "telemetry.sdk.language",+ value: { stringValue: "python" },+ },+ {+ key: "telemetry.sdk.name",+ value: { stringValue: "opentelemetry" },+ },+ {+ key: "telemetry.sdk.version",+ value: { stringValue: "1.36.0" },+ },+ ],+ },+ scopeSpans: [+ {+ scope: {+ name: "pydantic-ai",+ version: "0.7.4",+ attributes: [],+ },+ spans: [+ {+ traceId: Buffer.from(traceId, "hex"),+ spanId: Buffer.from("1234567890abcdef", "hex"),+ parentSpanId: Buffer.from("fedcba0987654321", "hex"),+ name: "tool-call",+ kind: 1,+ startTimeUnixNano: {+ low: 1000000,+ high: 406528574,+ unsigned: true,+ },+ endTimeUnixNano: {+ low: 2000000,+ high: 406528574,+ unsigned: true,+ },+ attributes: [+ {+ key: "gen_ai.tool.name",+ value: { stringValue: "roulette_wheel" },+ },+ {+ key: "gen_ai.tool.call.id",+ value: { stringValue: "call_idvalue" },+ },+ {+ key: "tool_arguments",+ value: { stringValue: '{"square": 18}' },+ },+ {+ key: "tool_response",+ value: { stringValue: "winner" },+ },+ {+ key: "logfire.msg",+ value: { stringValue: "running tool: roulette_wheel" },+ },+ ],+ events: [],+ status: { code: 1 },+ },+ ],+ },+ ],+ };++ const events = await convertOtelSpanToIngestionEvent(+ pydanticAiToolSpan,+ new Set([traceId]),+ );++ const toolObservation = events.find((e) => e.type === "tool-create");++ // Should map to TOOL observation type+ expect(toolObservation).toBeDefined();+ expect(toolObservation?.type).toBe("tool-create");++ // Tool name should come from gen_ai.tool.name+ expect(toolObservation?.body.name).toBe("roulette_wheel");++ // Verify trace structure+ expect(toolObservation?.body.traceId).toBe(traceId);+ });++ it("should prioritize gen_ai.tool.name over logfire.msg for tool observation name", async () => {+ // why? because the logfire.msg value usually has: "running tool: roulette_wheel" whereas the gen_ai.tool.name value is "roulette_wheel"+ // that is cleaner!+ const traceId = "abcdef1234567890abcdef1234567890";++ const toolSpanWithBothNames = {+ resource: {+ attributes: [+ {+ key: "telemetry.sdk.language",+ value: { stringValue: "python" },+ },+ ],+ },+ scopeSpans: [+ {+ scope: {+ name: "pydantic-ai",+ version: "0.7.4",+ },+ spans: [+ {+ traceId: Buffer.from(traceId, "hex"),+ spanId: Buffer.from("1234567890abcdef", "hex"),+ name: "span-name",+ kind: 1,+ startTimeUnixNano: { low: 1000000, high: 406528574 },+ endTimeUnixNano: { low: 2000000, high: 406528574 },+ attributes: [+ {+ key: "gen_ai.tool.name",+ value: { stringValue: "correct_tool_name" },+ },+ {+ key: "gen_ai.tool.call.id",+ value: { stringValue: "call_123" },+ },+ {+ key: "logfire.msg",+ value: { stringValue: "wrong name from logfire" },+ },+ ],+ events: [],+ status: { code: 1 },+ },+ ],+ },+ ],+ };++ const events = await convertOtelSpanToIngestionEvent(+ toolSpanWithBothNames,+ new Set([traceId]),+ );++ const toolObservation = events.find((e) => e.type === "tool-create");++ // Should use gen_ai.tool.name, NOT logfire.msg+ expect(toolObservation?.body.name).toBe("correct_tool_name");+ });+it("should prioritize OpenInference over OTel GenAI and model detection", async () => {const resourceSpan = {scopeSpans: [