Skip to content

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

fix(otel): map gen_ai tool definitions into input payload (#12624)

Max Komarycheva1ab29445686
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 224d33b94e..fef7d32f7e 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -1304,10 +1304,6 @@ export class OtelIngestionProcessor {
]),
);
- // TODO: Map gen_ai.tool.definitions to input.tools for backend extraction
- // const toolDefs = attributes["gen_ai.tool.definitions"] || attributes["model_request_parameters"]?.function_tools;
- // if (toolDefs && input && typeof input === "object") { input = { ...input, tools: toolDefs }; }
-
// Langfuse
input =
domain === "trace" && attributes[LangfuseOtelSpanAttributes.TRACE_INPUT]
@@ -1635,7 +1631,11 @@ export class OtelIngestionProcessor {
);
}
if (input || output) {
- return { input, output, filteredAttributes };
+ return {
+ input: this.appendOtelToolDefinitionsToInput(input, attributes),
+ output,
+ filteredAttributes,
+ };
}
// OpenTelemetry tools (https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans)
@@ -1648,6 +1648,77 @@ export class OtelIngestionProcessor {
return { input: null, output: null, filteredAttributes };
}
+ private appendOtelToolDefinitionsToInput(
+ input: unknown,
+ attributes: Record<string, unknown>,
+ ): unknown {
+ const toolDefinitions = this.extractOtelToolDefinitions(attributes);
+
+ if (!toolDefinitions || input == null) {
+ return input;
+ }
+
+ const mergeToolDefinitions = (value: unknown): unknown => {
+ if (Array.isArray(value)) {
+ return { messages: value, tools: toolDefinitions };
+ }
+
+ if (typeof value !== "object" || value == null) {
+ return null;
+ }
+
+ const inputObject = value as Record<string, unknown>;
+
+ if (inputObject.tools != null) {
+ return inputObject;
+ }
+
+ return {
+ ...inputObject,
+ tools: toolDefinitions,
+ };
+ };
+
+ if (typeof input === "string") {
+ try {
+ const parsedInput = JSON.parse(input);
+ const mergedInput = mergeToolDefinitions(parsedInput);
+ return mergedInput == null ? input : JSON.stringify(mergedInput);
+ } catch {
+ return input;
+ }
+ }
+
+ return mergeToolDefinitions(input) ?? input;
+ }
+
+ private extractOtelToolDefinitions(
+ attributes: Record<string, unknown>,
+ ): unknown[] | null {
+ const parseJsonString = (value: unknown): unknown => {
+ if (typeof value !== "string") {
+ return value;
+ }
+
+ try {
+ return JSON.parse(value);
+ } catch {
+ return value;
+ }
+ };
+
+ const modelRequestParameters = parseJsonString(
+ attributes["model_request_parameters"],
+ ) as Record<string, unknown> | null;
+
+ const toolDefinitions = parseJsonString(
+ attributes["gen_ai.tool.definitions"] ??
+ modelRequestParameters?.function_tools,
+ );
+
+ return Array.isArray(toolDefinitions) ? toolDefinitions : null;
+ }
+
/**
* Prepends gen_ai.system_instructions as a {role: "system", content} message,
* consistent with how gen_ai.system.message events are mapped.
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index c6373994e6..cf77dbb939 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -3746,6 +3746,103 @@ describe("OTel Resource Span Mapping", () => {
);
});
+ it("should map gen_ai.tool.definitions to input.tools for gen_ai.input.messages", async () => {
+ const traceId = "abcdef1234567890abcdef1234567890";
+ const rootSpanId = "1234567890abcdef";
+
+ const span = {
+ resource: {
+ attributes: [
+ {
+ key: "service.name",
+ value: { stringValue: "otel-test-service" },
+ },
+ ],
+ },
+ scopeSpans: [
+ {
+ scope: {
+ name: "otel-test-scope",
+ version: "1.0.0",
+ },
+ spans: [
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from(rootSpanId, "hex"),
+ name: "otel-genai-span",
+ kind: 1,
+ startTimeUnixNano: { low: 0, high: 406528574, unsigned: true },
+ endTimeUnixNano: {
+ low: 1000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [
+ {
+ key: "gen_ai.input.messages",
+ value: {
+ stringValue: JSON.stringify([
+ { role: "user", content: "What is 2 + 2?" },
+ ]),
+ },
+ },
+ {
+ key: "gen_ai.tool.definitions",
+ value: {
+ stringValue: JSON.stringify([
+ {
+ type: "function",
+ name: "calculator",
+ description: "Do math",
+ parameters: {
+ type: "object",
+ properties: {
+ expression: { type: "string" },
+ },
+ },
+ },
+ ]),
+ },
+ },
+ ],
+ status: {},
+ },
+ ],
+ },
+ ],
+ };
+
+ const events = await convertOtelSpanToIngestionEvent(span, new Set());
+
+ const observation = events.find(
+ (e) => e.type.endsWith("-create") && e.type !== "trace-create",
+ );
+
+ expect(observation?.body.input).toBeDefined();
+
+ const parsedInput =
+ typeof observation?.body.input === "string"
+ ? JSON.parse(observation.body.input)
+ : observation?.body.input;
+
+ expect(parsedInput.messages).toEqual([
+ { role: "user", content: "What is 2 + 2?" },
+ ]);
+ expect(parsedInput.tools).toEqual([
+ {
+ type: "function",
+ name: "calculator",
+ description: "Do math",
+ parameters: {
+ type: "object",
+ properties: {
+ expression: { type: "string" },
+ },
+ },
+ },
+ ]);
+ });
+
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