Skip to content

Commit 46d8993

Browse files
authored
fix(ai-sdk): support AI SDK v5 renamed tool attributes (inputSchema, input, output) (#849)
1 parent 8e05349 commit 46d8993

File tree

2 files changed

+553
-20
lines changed

2 files changed

+553
-20
lines changed

packages/traceloop-sdk/src/lib/tracing/ai-sdk-transformations.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,21 @@ const transformResponseToolCalls = (attributes: Record<string, any>): void => {
146146
const toolCallParts: any[] = [];
147147
toolCalls.forEach((toolCall: any, index: number) => {
148148
if (toolCall.toolCallType === "function") {
149+
// Support both v4 (args) and v5 (input) formats
150+
// Prefer v5 (input) if present
151+
const toolArgs = toolCall.input ?? toolCall.args;
152+
149153
attributes[`${ATTR_GEN_AI_COMPLETION}.0.tool_calls.${index}.name`] =
150154
toolCall.toolName;
151155
attributes[
152156
`${ATTR_GEN_AI_COMPLETION}.0.tool_calls.${index}.arguments`
153-
] = toolCall.args;
157+
] = toolArgs;
154158

155159
toolCallParts.push({
156160
type: TYPE_TOOL_CALL,
157161
tool_call: {
158162
name: toolCall.toolName,
159-
arguments: toolCall.args,
163+
arguments: toolArgs,
160164
},
161165
});
162166
}
@@ -260,13 +264,13 @@ const transformTools = (attributes: Record<string, any>): void => {
260264
] = tool.description;
261265
}
262266

263-
if (tool.parameters) {
267+
// Support both v4 (parameters) and v5 (inputSchema) formats
268+
// Prefer v5 (inputSchema) if present
269+
const schema = tool.inputSchema ?? tool.parameters;
270+
if (schema) {
264271
attributes[
265272
`${SpanAttributes.LLM_REQUEST_FUNCTIONS}.${index}.parameters`
266-
] =
267-
typeof tool.parameters === "string"
268-
? tool.parameters
269-
: JSON.stringify(tool.parameters);
273+
] = typeof schema === "string" ? schema : JSON.stringify(schema);
270274
}
271275
}
272276
});
@@ -542,14 +546,21 @@ const transformToolCallAttributes = (attributes: Record<string, any>): void => {
542546
delete attributes["ai.toolCall.id"];
543547
}
544548

545-
if ("ai.toolCall.args" in attributes) {
546-
attributes[ATTR_GEN_AI_TOOL_CALL_ARGUMENTS] =
547-
attributes["ai.toolCall.args"];
549+
// Support both v4 (args) and v5 (input) formats
550+
// Prefer v5 (input) if present
551+
const toolArgs =
552+
attributes["ai.toolCall.input"] ?? attributes["ai.toolCall.args"];
553+
if (toolArgs !== undefined) {
554+
attributes[ATTR_GEN_AI_TOOL_CALL_ARGUMENTS] = toolArgs;
548555
// Don't delete yet - transformToolCalls will handle entity input/output
549556
}
550557

551-
if ("ai.toolCall.result" in attributes) {
552-
attributes[ATTR_GEN_AI_TOOL_CALL_RESULT] = attributes["ai.toolCall.result"];
558+
// Support both v4 (result) and v5 (output) formats
559+
// Prefer v5 (output) if present
560+
const toolResult =
561+
attributes["ai.toolCall.output"] ?? attributes["ai.toolCall.result"];
562+
if (toolResult !== undefined) {
563+
attributes[ATTR_GEN_AI_TOOL_CALL_RESULT] = toolResult;
553564
// Don't delete yet - transformToolCalls will handle entity input/output
554565
}
555566
};
@@ -681,16 +692,21 @@ export const transformLLMSpans = (
681692
};
682693

683694
const transformToolCalls = (span: ReadableSpan): void => {
684-
if (
685-
span.attributes["ai.toolCall.args"] &&
686-
span.attributes["ai.toolCall.result"]
687-
) {
688-
span.attributes[SpanAttributes.TRACELOOP_ENTITY_INPUT] =
689-
span.attributes["ai.toolCall.args"];
695+
// Support both v4 (args/result) and v5 (input/output) formats
696+
// Prefer v5 (input/output) if present
697+
const toolInput =
698+
span.attributes["ai.toolCall.input"] ?? span.attributes["ai.toolCall.args"];
699+
const toolOutput =
700+
span.attributes["ai.toolCall.output"] ??
701+
span.attributes["ai.toolCall.result"];
702+
703+
if (toolInput && toolOutput) {
704+
span.attributes[SpanAttributes.TRACELOOP_ENTITY_INPUT] = toolInput;
690705
delete span.attributes["ai.toolCall.args"];
691-
span.attributes[SpanAttributes.TRACELOOP_ENTITY_OUTPUT] =
692-
span.attributes["ai.toolCall.result"];
706+
delete span.attributes["ai.toolCall.input"];
707+
span.attributes[SpanAttributes.TRACELOOP_ENTITY_OUTPUT] = toolOutput;
693708
delete span.attributes["ai.toolCall.result"];
709+
delete span.attributes["ai.toolCall.output"];
694710
span.attributes[SpanAttributes.TRACELOOP_SPAN_KIND] =
695711
TraceloopSpanKindValues.TOOL;
696712

0 commit comments

Comments
 (0)