🪢 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: map llm.input_messages and llm.output_messages for otel (#10810)
Steffen Schmitzfbfceac46742
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex 0574b32f41..81a53ce8c7 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -1078,20 +1078,50 @@ export class OtelIngestionProcessor {const keys = Object.keys(input).map((key) => key.replace(`${prefix}.`, ""));const useArray = keys.some((key) => key.match(/^\d+\./));+ // Helper function to set a value at a nested path+ const setNestedValue = (obj: any, path: string[], value: unknown): void => {+ let current = obj;+ for (let i = 0; i < path.length - 1; i++) {+ const key = path[i];+ if (!(key in current)) {+ // Check if next key is a number to decide if we need an array or object+ current[key] = /^\d+$/.test(path[i + 1]) ? [] : {};+ }+ current = current[key];+ }+ current[path[path.length - 1]] = value;+ };+if (useArray) {const result: any[] = [];for (const key of keys) {- const [index, ikey] = key.split(".", 2) as [number, string];+ const pathParts = key.split(".");+ const index = parseInt(pathParts[0], 10);if (!result[index]) {result[index] = {};}- result[index][ikey] = input[`${prefix}.${index}.${ikey}`];+ if (pathParts.length === 2) {+ // Simple case: 0.content -> result[0].content+ result[index][pathParts[1]] = input[`${prefix}.${key}`];+ } else {+ // Nested case: 0.message.content -> result[0].message.content+ setNestedValue(+ result[index],+ pathParts.slice(1),+ input[`${prefix}.${key}`],+ );+ }}return result;} else {const result: Record<string, unknown> = {};for (const key of keys) {- result[key] = input[`${prefix}.${key}`];+ const pathParts = key.split(".");+ if (pathParts.length === 1) {+ result[key] = input[`${prefix}.${key}`];+ } else {+ setNestedValue(result, pathParts, input[`${prefix}.${key}`]);+ }}return result;}@@ -1166,11 +1196,13 @@ export class OtelIngestionProcessor {delete rawFilteredAttributes[key];});- // Delete gen_ai.prompt.* and gen_ai.completion.* keys+ // Delete gen_ai.prompt.*, gen_ai.completion.*, llm.input_messages.*, and llm.output_messages.* keysObject.keys(attributes).forEach((key) => {if (key.startsWith("gen_ai.prompt") ||- key.startsWith("gen_ai.completion")+ key.startsWith("gen_ai.completion") ||+ key.startsWith("llm.input_messages") ||+ key.startsWith("llm.output_messages")) {delete rawFilteredAttributes[key];}@@ -1455,6 +1487,35 @@ export class OtelIngestionProcessor {};}+ // OpenInference llm.input_messages and llm.output_messages (used by Agno, BeeAI, etc.)+ const llmInputAttributes = Object.keys(attributes).filter((key) =>+ key.startsWith("llm.input_messages"),+ );+ const llmOutputAttributes = Object.keys(attributes).filter((key) =>+ key.startsWith("llm.output_messages"),+ );+ if (llmInputAttributes.length > 0 || llmOutputAttributes.length > 0) {+ const llmInput = llmInputAttributes.reduce((acc: any, key) => {+ acc[key] = attributes[key];+ return acc;+ }, {});+ const llmOutput = llmOutputAttributes.reduce((acc: any, key) => {+ acc[key] = attributes[key];+ return acc;+ }, {});+ return {+ input: this.convertKeyPathToNestedObject(+ llmInput,+ "llm.input_messages",+ ),+ output: this.convertKeyPathToNestedObject(+ llmOutput,+ "llm.output_messages",+ ),+ filteredAttributes,+ };+ }+// OpenTelemetry messages (https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans)input = attributes["gen_ai.input.messages"];output = attributes["gen_ai.output.messages"];diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex 22dbd72cb4..9e445601c4 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -2082,6 +2082,46 @@ describe("OTel Resource Span Mapping", () => {entityAttributeValue: 100,},],+ [+ "should extract llm.input_messages.message.content to input",+ {+ entity: "observation",+ otelAttributeKey: "llm.input_messages.0.message.content",+ otelAttributeValue: {+ stringValue: "Hello, how are you?",+ },+ entityAttributeKey: "input",+ entityAttributeValue: [+ { message: { content: "Hello, how are you?" } },+ ],+ },+ ],+ [+ "should extract llm.input_messages.message.role to input",+ {+ entity: "observation",+ otelAttributeKey: "llm.input_messages.0.message.role",+ otelAttributeValue: {+ stringValue: "system",+ },+ entityAttributeKey: "input",+ entityAttributeValue: [{ message: { role: "system" } }],+ },+ ],+ [+ "should extract llm.output_messages.message.content to output",+ {+ entity: "observation",+ otelAttributeKey: "llm.output_messages.0.message.content",+ otelAttributeValue: {+ stringValue: "Hello, how are you?",+ },+ entityAttributeKey: "output",+ entityAttributeValue: [+ { message: { content: "Hello, how are you?" } },+ ],+ },+ ],["#5457: should map traceloop.entity.input to input",{@@ -2857,6 +2897,284 @@ describe("OTel Resource Span Mapping", () => {);},);++ it("should map llm.input_messages and llm.output_messages to input/output and filter from metadata", async () => {+ const traceId = "abcdef1234567890abcdef1234567890";+ const rootSpanId = "1234567890abcdef";++ const openInferenceSpan = {+ resource: {+ attributes: [+ {+ key: "service.name",+ value: { stringValue: "agno-service" },+ },+ ],+ },+ scopeSpans: [+ {+ scope: {+ name: "openinference",+ version: "1.0.0",+ },+ spans: [+ {+ traceId: Buffer.from(traceId, "hex"),+ spanId: Buffer.from(rootSpanId, "hex"),+ name: "llm-call",+ kind: 1,+ startTimeUnixNano: { low: 0, high: 406528574, unsigned: true },+ endTimeUnixNano: {+ low: 1000000,+ high: 406528574,+ unsigned: true,+ },+ attributes: [+ // OpenInference llm.input_messages format (used by Agno, BeeAI, etc.)+ {+ key: "llm.input_messages.0.message.role",+ value: { stringValue: "system" },+ },+ {+ key: "llm.input_messages.0.message.content",+ value: { stringValue: "You are a helpful assistant." },+ },+ {+ key: "llm.input_messages.1.message.role",+ value: { stringValue: "user" },+ },+ {+ key: "llm.input_messages.1.message.content",+ value: { stringValue: "What is the weather today?" },+ },+ // OpenInference llm.output_messages format+ {+ key: "llm.output_messages.0.message.role",+ value: { stringValue: "assistant" },+ },+ {+ key: "llm.output_messages.0.message.content",+ value: {+ stringValue:+ "I don't have access to real-time weather data.",+ },+ },+ // Custom attributes (should remain in metadata.attributes)+ {+ key: "custom_attribute",+ value: { stringValue: "should_be_preserved" },+ },+ ],+ status: {},+ },+ ],+ },+ ],+ };++ const events = await convertOtelSpanToIngestionEvent(+ openInferenceSpan,+ new Set(),+ );++ const observation = events.find(+ (e) => e.type.endsWith("-create") && e.type !== "trace-create",+ );++ // Verify input is extracted and structured as a nested array+ expect(observation?.body.input).toBeDefined();+ const inputParsed =+ typeof observation?.body.input === "string"+ ? JSON.parse(observation.body.input)+ : observation?.body.input;+ expect(Array.isArray(inputParsed)).toBe(true);+ expect(inputParsed[0].message.role).toBe("system");+ expect(inputParsed[0].message.content).toBe(+ "You are a helpful assistant.",+ );+ expect(inputParsed[1].message.role).toBe("user");+ expect(inputParsed[1].message.content).toBe("What is the weather today?");++ // Verify output is extracted and structured as a nested array+ expect(observation?.body.output).toBeDefined();+ const outputParsed =+ typeof observation?.body.output === "string"+ ? JSON.parse(observation.body.output)+ : observation?.body.output;+ expect(Array.isArray(outputParsed)).toBe(true);+ expect(outputParsed[0].message.role).toBe("assistant");+ expect(outputParsed[0].message.content).toBe(+ "I don't have access to real-time weather data.",+ );++ // Verify llm.input_messages.* and llm.output_messages.* are NOT in metadata.attributes+ expect(+ observation?.body.metadata?.attributes?.[+ "llm.input_messages.0.message.role"+ ],+ ).toBeUndefined();+ expect(+ observation?.body.metadata?.attributes?.[+ "llm.input_messages.0.message.content"+ ],+ ).toBeUndefined();+ expect(+ observation?.body.metadata?.attributes?.[+ "llm.input_messages.1.message.role"+ ],+ ).toBeUndefined();+ expect(+ observation?.body.metadata?.attributes?.[+ "llm.input_messages.1.message.content"+ ],+ ).toBeUndefined();+ expect(+ observation?.body.metadata?.attributes?.[+ "llm.output_messages.0.message.role"+ ],+ ).toBeUndefined();+ expect(+ observation?.body.metadata?.attributes?.[+ "llm.output_messages.0.message.content"+ ],+ ).toBeUndefined();++ // Verify custom attributes ARE in metadata.attributes+ expect(observation?.body.metadata?.attributes?.custom_attribute).toBe(+ "should_be_preserved",+ );+ });++ it("should filter all input/output attribute patterns from metadata.attributes while preserving custom attributes", async () => {+ // This test verifies that extractInputAndOutput's filteredAttributes correctly removes+ // all known input/output attribute patterns from multiple frameworks+ const traceId = "abcdef1234567890abcdef1234567890";+ const rootSpanId = "1234567890abcdef";++ const multiFrameworkSpan = {+ resource: {+ attributes: [+ {+ key: "service.name",+ value: { stringValue: "test-service" },+ },+ ],+ },+ scopeSpans: [+ {+ scope: {+ name: "test-scope",+ version: "1.0.0",+ },+ spans: [+ {+ traceId: Buffer.from(traceId, "hex"),+ spanId: Buffer.from(rootSpanId, "hex"),+ name: "multi-framework-span",+ kind: 1,+ startTimeUnixNano: { low: 0, high: 406528574, unsigned: true },+ endTimeUnixNano: {+ low: 1000000,+ high: 406528574,+ unsigned: true,+ },+ attributes: [+ // Input attribute that will be mapped+ {+ key: "input",+ value: { stringValue: '{"query": "test input"}' },+ },+ // gen_ai.prompt.* pattern (should be filtered)+ {+ key: "gen_ai.prompt.0.content",+ value: { stringValue: "prompt content" },+ },+ // gen_ai.completion.* pattern (should be filtered)+ {+ key: "gen_ai.completion.0.content",+ value: { stringValue: "completion content" },+ },+ // llm.input_messages.* pattern (should be filtered)+ {+ key: "llm.input_messages.0.message.role",+ value: { stringValue: "user" },+ },+ // llm.output_messages.* pattern (should be filtered)+ {+ key: "llm.output_messages.0.message.role",+ value: { stringValue: "assistant" },+ },+ // Custom attributes (should be preserved)+ {+ key: "custom.request_id",+ value: { stringValue: "req-12345" },+ },+ {+ key: "deployment.region",+ value: { stringValue: "us-west-2" },+ },+ {+ key: "model.version",+ value: { stringValue: "v2.0" },+ },+ ],+ status: {},+ },+ ],+ },+ ],+ };++ const events = await convertOtelSpanToIngestionEvent(+ multiFrameworkSpan,+ new Set(),+ );++ const observation = events.find(+ (e) => e.type.endsWith("-create") && e.type !== "trace-create",+ );++ // Verify input was extracted+ expect(observation?.body.input).toBe('{"query": "test input"}');++ // Verify input attribute is filtered from metadata+ expect(observation?.body.metadata?.attributes?.input).toBeUndefined();++ // Verify gen_ai.prompt.* attributes are filtered+ expect(+ observation?.body.metadata?.attributes?.["gen_ai.prompt.0.content"],+ ).toBeUndefined();++ // Verify gen_ai.completion.* attributes are filtered+ expect(+ observation?.body.metadata?.attributes?.["gen_ai.completion.0.content"],+ ).toBeUndefined();++ // Verify llm.input_messages.* attributes are filtered+ expect(+ observation?.body.metadata?.attributes?.[+ "llm.input_messages.0.message.role"+ ],+ ).toBeUndefined();++ // Verify llm.output_messages.* attributes are filtered+ expect(+ observation?.body.metadata?.attributes?.[+ "llm.output_messages.0.message.role"+ ],+ ).toBeUndefined();++ // Verify custom attributes ARE preserved in metadata.attributes+ expect(+ observation?.body.metadata?.attributes?.["custom.request_id"],+ ).toBe("req-12345");+ expect(+ observation?.body.metadata?.attributes?.["deployment.region"],+ ).toBe("us-west-2");+ expect(observation?.body.metadata?.attributes?.["model.version"]).toBe(+ "v2.0",+ );+ });});describe("Span Counting", () => {