-
Notifications
You must be signed in to change notification settings - Fork 53
Description
Description
When using @traceloop/node-server-sdk with the Vercel AI SDK, the gen_ai.operation.name attribute is not being properly set on spans, even though other transformations (like gen_ai.prompt.0.content, gen_ai.completion.0.content, etc.) are working correctly.
Expected Behavior
For a span with original name ai.generateText, the gen_ai.operation.name attribute should be set to "chat" according to the transformOperationName function in ai-sdk-transformations.ts.
Actual Behavior
The gen_ai.operation.name attribute is not present on the span. Instead, only the original ai.operationId attribute remains with value "ai.generateText" as well as"operation.name" (missing the gen_ai prefix and with the original non-transformed operation name as well).
Root Cause Analysis
After investigating the code, I believe this is a timing issue:
-
onSpanStart(line ~3326 in bundled index.js) callstransformAiSdkSpanNames(span)which transforms the span name from"ai.generateText"to"run.ai"or"<agentName>.agent" -
onSpanEndcallstransformAiSdkSpanAttributes(span)which eventually callstransformLLMSpans(span.attributes, span.name) -
transformOperationNamechecks ifspanName.includes("generateText")to determine if it should setoperationName = "chat" -
But by the time
onSpanEndruns,span.namehas already been changed from"ai.generateText"to"run.ai", so the conditionspanName.includes("generateText")fails andgen_ai.operation.nameis never set.
Relevant Code
// In transformOperationName:
const transformOperationName = (attributes, spanName) => {
if (!spanName)
return;
let operationName;
if (spanName.includes("generateText") || // <-- This check fails because spanName is now "run.ai"
spanName.includes("streamText") ||
spanName.includes("generateObject") ||
spanName.includes("streamObject")) {
operationName = "chat";
}
// ...
};Suggested Fix
Either:
- Store the original span name before transformation and use it in
transformOperationName - Move
transformOperationNameto run inonSpanStartbefore the span name is transformed - Check
ai.operationIdattribute instead of (or in addition to) the span name
Environment
@traceloop/node-server-sdk: 0.22.6ai(Vercel AI SDK): 5.0.108- Node.js: v20+
Reproduction
- Initialize traceloop with
traceloop.initialize() - Use Vercel AI SDK's
generateTextorstreamTextwithexperimental_telemetry: {isEnabled: true} - Observe the exported spans -
gen_ai.operation.nameis missing while othergen_ai.*attributes are present