Skip to content

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

fix(otel): map GenAI IO on AI SDK spans (#14655)

Hassieb Pakzad9eadae595d54
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index bca4f1190a..8837846402 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -1607,6 +1607,11 @@ export class OtelIngestionProcessor {
: undefined;
}
+ const genAiInputOutput =
+ this.extractOpenTelemetryGenAiInputAndOutput(attributes);
+ input ??= genAiInputOutput?.input;
+ output ??= genAiInputOutput?.output;
+
return { input, output, filteredAttributes };
}
@@ -1897,27 +1902,42 @@ export class OtelIngestionProcessor {
};
}
+ const genAiInputOutput =
+ this.extractOpenTelemetryGenAiInputAndOutput(attributes);
+ if (genAiInputOutput) {
+ return { ...genAiInputOutput, filteredAttributes };
+ }
+
+ return { input: null, output: null, filteredAttributes };
+ }
+
+ private extractOpenTelemetryGenAiInputAndOutput(
+ attributes: Record<string, unknown>,
+ ): { input: unknown; output: unknown } | null {
// 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"];
+ let input = attributes["gen_ai.input.messages"];
+ let output = attributes["gen_ai.output.messages"];
+
if (input && attributes["gen_ai.system_instructions"]) {
input = this.prependSystemInstructions(
input,
attributes["gen_ai.system_instructions"],
);
}
+
if (input || output) {
- return { input, output, filteredAttributes };
+ return { input, output };
}
// OpenTelemetry tools (https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans)
input = attributes["gen_ai.tool.call.arguments"];
output = attributes["gen_ai.tool.call.result"];
+
if (input || output) {
- return { input, output, filteredAttributes };
+ return { input, output };
}
- return { input: null, output: null, filteredAttributes };
+ return null;
}
/**
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index 24ba6030ab..6694908c84 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -4612,6 +4612,158 @@ describe("OTel Resource Span Mapping", () => {
);
});
+ it("should map AI SDK v7 GenAI semantic convention input/output attributes", async () => {
+ const traceId = "abcdef1234567890abcdef1234567894";
+ const generationSpanId = "1234567890abcded";
+ const toolSpanId = "1234567890abcdee";
+ const resourceSpan = {
+ resource: {
+ attributes: [
+ {
+ key: "service.name",
+ value: { stringValue: "otel-test-service" },
+ },
+ ],
+ },
+ scopeSpans: [
+ {
+ scope: {
+ name: "ai",
+ version: "7.0.0",
+ },
+ spans: [
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from(generationSpanId, "hex"),
+ name: "chat test-model",
+ kind: 1,
+ startTimeUnixNano: { low: 0, high: 406528574, unsigned: true },
+ endTimeUnixNano: {
+ low: 1000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [
+ {
+ key: "gen_ai.operation.name",
+ value: { stringValue: "chat" },
+ },
+ {
+ key: "gen_ai.request.model",
+ value: { stringValue: "test-model" },
+ },
+ {
+ key: "gen_ai.system_instructions",
+ value: { stringValue: "Answer concisely." },
+ },
+ {
+ key: "gen_ai.input.messages",
+ value: {
+ stringValue: JSON.stringify([
+ { role: "user", content: "hello" },
+ ]),
+ },
+ },
+ {
+ key: "gen_ai.output.messages",
+ value: {
+ stringValue: JSON.stringify([
+ { role: "assistant", content: "hi" },
+ ]),
+ },
+ },
+ {
+ key: "custom.generation.attribute",
+ value: { stringValue: "keep-generation" },
+ },
+ ],
+ status: {},
+ },
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from(toolSpanId, "hex"),
+ parentSpanId: Buffer.from(generationSpanId, "hex"),
+ name: "execute_tool get_weather",
+ kind: 1,
+ startTimeUnixNano: {
+ low: 1000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ endTimeUnixNano: {
+ low: 2000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [
+ {
+ key: "gen_ai.operation.name",
+ value: { stringValue: "execute_tool" },
+ },
+ {
+ key: "gen_ai.tool.name",
+ value: { stringValue: "get_weather" },
+ },
+ {
+ key: "gen_ai.tool.call.arguments",
+ value: { stringValue: '{"location":"Berlin"}' },
+ },
+ {
+ key: "gen_ai.tool.call.result",
+ value: { stringValue: '{"temperature":21}' },
+ },
+ {
+ key: "custom.tool.attribute",
+ value: { stringValue: "keep-tool" },
+ },
+ ],
+ status: {},
+ },
+ ],
+ },
+ ],
+ };
+
+ const events = await convertOtelSpanToIngestionEvent(
+ resourceSpan,
+ new Set([traceId]),
+ );
+
+ const generation = events.find((e) => e.type === "generation-create");
+ const tool = events.find((e) => e.type === "tool-create");
+
+ expect(generation?.body.input).toEqual(
+ JSON.stringify([
+ { role: "system", content: "Answer concisely." },
+ { role: "user", content: "hello" },
+ ]),
+ );
+ expect(generation?.body.output).toBe(
+ JSON.stringify([{ role: "assistant", content: "hi" }]),
+ );
+ expect(tool?.body.input).toBe('{"location":"Berlin"}');
+ expect(tool?.body.output).toBe('{"temperature":21}');
+
+ expect(
+ generation?.body.metadata?.attributes?.["gen_ai.input.messages"],
+ ).toBeUndefined();
+ expect(
+ generation?.body.metadata?.attributes?.["gen_ai.output.messages"],
+ ).toBeUndefined();
+ expect(
+ tool?.body.metadata?.attributes?.["gen_ai.tool.call.arguments"],
+ ).toBeUndefined();
+ expect(
+ tool?.body.metadata?.attributes?.["gen_ai.tool.call.result"],
+ ).toBeUndefined();
+ expect(
+ generation?.body.metadata?.attributes?.["custom.generation.attribute"],
+ ).toBe("keep-generation");
+ expect(tool?.body.metadata?.attributes?.["custom.tool.attribute"]).toBe(
+ "keep-tool",
+ );
+ });
+
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