mastra-ai/mastra · error · Error
No model generation span found in trace
Error message
No model generation span found in trace
What it means
findPrimaryLLMSpan throws this when the root agent_run span has no child span of type MODEL_GENERATION. The scorer pipeline needs the model-generation span to extract system messages and model I/O for scoring.
Source
Thrown at packages/core/src/evals/scoreTraces/utils.ts:247
const spanIds = new Set(trace.spans.map(span => span.spanId));
for (const span of trace.spans) {
if (span.parentSpanId && !spanIds.has(span.parentSpanId)) {
throw new Error(`Span ${span.spanId} references non-existent parent ${span.parentSpanId}`);
}
}
}
/**
* Find the most recent model span that contains conversation history
*/
function findPrimaryLLMSpan(spanTree: SpanTree, rootAgentSpan: SpanRecord): SpanRecord {
const directLLMSpans = getChildrenOfType<SpanRecord>(spanTree, rootAgentSpan.spanId, SpanType.MODEL_GENERATION);
if (directLLMSpans.length > 0) {
// There should only be one model generation span per agent run which is a direct child of the root agent span
return directLLMSpans[0]!;
}
throw new Error('No model generation span found in trace');
}
/**
* Extract common trace validation and span tree building logic
*/
function prepareTraceForTransformation(trace: TraceRecord) {
validateTrace(trace);
const spanTree = buildSpanTree(trace.spans);
// Find the root agent run span
const rootAgentSpan = spanTree.rootSpans.find(span => span.spanType === 'agent_run') as SpanRecord | undefined;
if (!rootAgentSpan) {
throw new Error('No root agent_run span found in trace');
}
return { spanTree, rootAgentSpan };
}View on GitHub (pinned to 75dd419e61)
Solutions
- Ensure the agent run completed a model call and that tracing of MODEL_GENERATION spans is enabled
- Verify the trace hierarchy: the model generation span must be a direct child of the root agent_run span
- Re-run the generation to get a fresh, complete trace before scoring
Example fix
// before
const { input, output } = transformTraceToScorerInputAndOutput(failedRunTrace);
// after
if (!findModelSpan(failedRunTrace)) skipScoring(failedRunTrace.traceId);
else transformTraceToScorerInputAndOutput(failedRunTrace); Defensive patterns
Strategy: type-guard
Validate before calling
const tree = buildSpanTree(trace.spans);
const root = tree.rootSpans.find(s => s.spanType === 'agent_run');
if (!root) throw new Error('No agent_run root');
const hasModelSpan = getChildrenOfType(tree, root.spanId, SpanType.MODEL_GENERATION).length > 0;
if (!hasModelSpan) skipScoring(trace.traceId); Type guard
function hasModelGenerationSpan(tree: SpanTree, root: SpanRecord): boolean {
return getChildrenOfType<SpanRecord>(tree, root.spanId, SpanType.MODEL_GENERATION).length > 0;
} Try / catch
try {
const { input, output } = transformTraceToScorerInputAndOutput(trace);
} catch (e) {
if (e instanceof Error && e.message === 'No model generation span found in trace') {
logger.warn('Skipping scoring: no LLM span in trace', { traceId: trace.traceId });
} else throw e;
} Prevention
- Only score traces from completed agent runs
- Verify LLM spans are traced and nested under the agent_run root
- Keep tracing instrumentation versions aligned with @mastra/core
- Handle errored runs (no LLM call) as non-scoreable
When it happens
Trigger: scoreTrace -> primaryLLMSpan on a trace where the agent run never recorded a model generation span as a direct child of the root agent span — e.g. the run failed before the LLM call, an agent configuration without a model, or instrumentation that nested model spans elsewhere.
Common situations: Agent run errored before invoking the LLM; custom workflows where the model call is not under the root agent span; older core versions emitting a different span hierarchy than the scoring code expects.
Related errors
- Trace is null or undefined
- Trace must have a spans array
- Trace has no spans
- Root agent span has no output
- Span ${span.spanId} references non-existent parent ${span.pa
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/c61b68b17f09d388.
Report an issue: GitHub.