{"record":{"id":"384a998ae7440d18","repo":"mastra-ai/mastra","slug":"persisted-score-is-missing-spanid-for-traceid-t","errorCode":null,"errorMessage":"Persisted score is missing spanId for traceId: ${target.traceId}","messagePattern":"Persisted score is missing spanId for traceId: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/evals/scoreTraces/scoreTracesWorkflow.ts","lineNumber":337,"sourceCode":"  failedCount: number;\n  results: ScoreTraceBatchResult[];\n}> {\n  const results = await pMap(\n    targets,\n    async (target, index): Promise<ScoreTraceBatchResult> => {\n      try {\n        const score = await scoreTrace({\n          storage,\n          scorer,\n          target,\n          batchId,\n          datasetId,\n          datasetItemId: target.datasetItemId,\n        });\n        const spanId = score.spanId ?? target.spanId;\n\n        if (!spanId) {\n          throw new Error(`Persisted score is missing spanId for traceId: ${target.traceId}`);\n        }\n\n        return {\n          ok: true,\n          index,\n          traceId: score.traceId ?? target.traceId,\n          spanId,\n          ...(target.datasetItemId ? { datasetItemId: target.datasetItemId } : {}),\n          score,\n        };\n      } catch (error) {\n        return {\n          ok: false,\n          index,\n          traceId: target.traceId,\n          ...(target.spanId ? { spanId: target.spanId } : {}),\n          ...(target.datasetItemId ? { datasetItemId: target.datasetItemId } : {}),\n          error: toBatchResultError(error),","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/evals/scoreTraces/scoreTracesWorkflow.ts#L319-L355","documentation":"In scoreTraceBatch, after a scorer runs, the persisted spanId is resolved as score.spanId ?? target.spanId. If both are absent, this Error is thrown because a persisted score row must be attributable to a span. It indicates the scorer run produced a score without a span association and the caller did not supply one either.","triggerScenarios":"Batch scoring trace targets where the scorer's run() result has no spanId (scorer invoked without span context) and the target { traceId } was given without spanId — e.g. the trace's span resolution was skipped or the scorer dropped span metadata.","commonSituations":"Scorer implementations returning custom results that omit spanId; targets built manually with only traceId; traces whose spans failed to resolve earlier but scoring proceeded; custom scorer pipelines bypassing runScorerForTrace's tenancy threading.","solutions":["Always include spanId in the score target: { traceId, spanId } resolved via resolveTargetSpan/root span.","Check why score.spanId was undefined — ensure the scorer ran with trace/span context (not a bare scorer.run without ids).","Update custom scorer code to propagate spanId into its returned score.","Persist traces fully so span resolution succeeds before batch scoring.","Validate targets before batch scoring to fail fast on missing spanId."],"exampleFix":"// before\nconst targets = traces.map(t => ({ traceId: t.traceId }));\n// after\nconst targets = traces.map(t => ({ traceId: t.traceId, spanId: t.spans.find(s => s.parentSpanId === null)?.spanId }));","handlingStrategy":"validation","validationCode":"// require spanId on batch targets up front\ntargets.forEach(t => {\n  if (!t.spanId) throw new Error(`Target for trace ${t.traceId} is missing spanId`);\n});","typeGuard":"function hasSpanId(target) {\n  return typeof target.spanId === 'string' && target.spanId.length > 0;\n}","tryCatchPattern":"try {\n  const results = await scoreTraceBatchTargets(targets);\n} catch (e) {\n  if (typeof e.message === 'string' && e.message.startsWith('Persisted score is missing spanId')) {\n    console.error('Scorer dropped span metadata — run scorers with trace/span context');\n  }\n  throw e;\n}","preventionTips":["Always build score targets with both traceId and spanId","Ensure scorers run with trace/span context so results carry spanId","Propagate spanId in custom scorer result objects","Validate batch targets before scoring to fail fast"],"tags":["observability","scorer","score-traces","span-lookup"],"backgroundTag":"span-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}