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) calls transformAiSdkSpanNames(span) which transforms the span name from "ai.generateText" to "run.ai" or "<agentName>.agent"
-
onSpanEnd calls transformAiSdkSpanAttributes(span) which eventually calls transformLLMSpans(span.attributes, span.name)
-
transformOperationName checks if spanName.includes("generateText") to determine if it should set operationName = "chat"
-
But by the time onSpanEnd runs, span.name has already been changed from "ai.generateText" to "run.ai", so the condition spanName.includes("generateText") fails and gen_ai.operation.name is 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
transformOperationName to run in onSpanStart before the span name is transformed
- Check
ai.operationId attribute instead of (or in addition to) the span name
Environment
@traceloop/node-server-sdk: 0.22.6
ai (Vercel AI SDK): 5.0.108
- Node.js: v20+
Reproduction
- Initialize traceloop with
traceloop.initialize()
- Use Vercel AI SDK's
generateText or streamText with experimental_telemetry: {isEnabled: true}
- Observe the exported spans -
gen_ai.operation.name is missing while other gen_ai.* attributes are present
Description
When using
@traceloop/node-server-sdkwith the Vercel AI SDK, thegen_ai.operation.nameattribute is not being properly set on spans, even though other transformations (likegen_ai.prompt.0.content,gen_ai.completion.0.content, etc.) are working correctly.Expected Behavior
For a span with original name
ai.generateText, thegen_ai.operation.nameattribute should be set to"chat"according to thetransformOperationNamefunction inai-sdk-transformations.ts.Actual Behavior
The
gen_ai.operation.nameattribute is not present on the span. Instead, only the originalai.operationIdattribute 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
Suggested Fix
Either:
transformOperationNametransformOperationNameto run inonSpanStartbefore the span name is transformedai.operationIdattribute instead of (or in addition to) the span nameEnvironment
@traceloop/node-server-sdk: 0.22.6ai(Vercel AI SDK): 5.0.108Reproduction
traceloop.initialize()generateTextorstreamTextwithexperimental_telemetry: {isEnabled: true}gen_ai.operation.nameis missing while othergen_ai.*attributes are present