{"record":{"id":"c61b68b17f09d388","repo":"mastra-ai/mastra","slug":"no-model-generation-span-found-in-trace","errorCode":null,"errorMessage":"No model generation span found in trace","messagePattern":"No model generation span found in trace","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/evals/scoreTraces/utils.ts","lineNumber":247,"sourceCode":"  const spanIds = new Set(trace.spans.map(span => span.spanId));\n  for (const span of trace.spans) {\n    if (span.parentSpanId && !spanIds.has(span.parentSpanId)) {\n      throw new Error(`Span ${span.spanId} references non-existent parent ${span.parentSpanId}`);\n    }\n  }\n}\n\n/**\n * Find the most recent model span that contains conversation history\n */\nfunction findPrimaryLLMSpan(spanTree: SpanTree, rootAgentSpan: SpanRecord): SpanRecord {\n  const directLLMSpans = getChildrenOfType<SpanRecord>(spanTree, rootAgentSpan.spanId, SpanType.MODEL_GENERATION);\n  if (directLLMSpans.length > 0) {\n    // There should only be one model generation span per agent run which is a direct child of the root agent span\n    return directLLMSpans[0]!;\n  }\n\n  throw new Error('No model generation span found in trace');\n}\n\n/**\n * Extract common trace validation and span tree building logic\n */\nfunction prepareTraceForTransformation(trace: TraceRecord) {\n  validateTrace(trace);\n  const spanTree = buildSpanTree(trace.spans);\n\n  // Find the root agent run span\n  const rootAgentSpan = spanTree.rootSpans.find(span => span.spanType === 'agent_run') as SpanRecord | undefined;\n\n  if (!rootAgentSpan) {\n    throw new Error('No root agent_run span found in trace');\n  }\n\n  return { spanTree, rootAgentSpan };\n}","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/evals/scoreTraces/utils.ts#L229-L265","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst { input, output } = transformTraceToScorerInputAndOutput(failedRunTrace);\n// after\nif (!findModelSpan(failedRunTrace)) skipScoring(failedRunTrace.traceId);\nelse transformTraceToScorerInputAndOutput(failedRunTrace);","handlingStrategy":"type-guard","validationCode":"const tree = buildSpanTree(trace.spans);\nconst root = tree.rootSpans.find(s => s.spanType === 'agent_run');\nif (!root) throw new Error('No agent_run root');\nconst hasModelSpan = getChildrenOfType(tree, root.spanId, SpanType.MODEL_GENERATION).length > 0;\nif (!hasModelSpan) skipScoring(trace.traceId);","typeGuard":"function hasModelGenerationSpan(tree: SpanTree, root: SpanRecord): boolean {\n  return getChildrenOfType<SpanRecord>(tree, root.spanId, SpanType.MODEL_GENERATION).length > 0;\n}","tryCatchPattern":"try {\n  const { input, output } = transformTraceToScorerInputAndOutput(trace);\n} catch (e) {\n  if (e instanceof Error && e.message === 'No model generation span found in trace') {\n    logger.warn('Skipping scoring: no LLM span in trace', { traceId: trace.traceId });\n  } else throw e;\n}","preventionTips":["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"],"tags":["trace","llm-span","scoring"],"backgroundTag":"missing-model-generation-span","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}