mastra-ai/mastra · error · Error

Trace not found for scoring, traceId: ${target.traceId}

Error message

Trace not found for scoring, traceId: ${target.traceId}

What it means

After obtaining the observability store, resolveTraceAndSpan calls observabilityStore.getTrace({ traceId }); when no trace record exists for that id it throws this plain Error. Scoring by trace reference requires the trace to still be present in observability storage.

Source

Thrown at packages/core/src/evals/scoreTraces/scoreTracesWorkflow.ts:186

    return {
      trace: target.trace,
      span: resolveTargetSpan({ trace: target.trace, spanId: target.spanId }),
    };
  }

  // TODO: add storage api to get a single span
  const observabilityStore = await storage.getStore('observability');
  if (!observabilityStore) {
    throw new MastraError({
      id: 'MASTRA_OBSERVABILITY_STORAGE_NOT_AVAILABLE',
      domain: ErrorDomain.STORAGE,
      category: ErrorCategory.SYSTEM,
      text: 'Observability storage domain is not available',
    });
  }
  const trace = await observabilityStore.getTrace({ traceId: target.traceId });
  if (!trace) {
    throw new Error(`Trace not found for scoring, traceId: ${target.traceId}`);
  }

  return { trace, span: resolveTargetSpan({ trace, spanId: target.spanId }) };
}

type TraceScoreResult = Awaited<ReturnType<MastraScorer['run']>>;

/**
 * Run a scorer against an already-resolved trace + span.
 *
 * Span tenancy (`organizationId`, `resourceId` → `projectId`) is threaded into
 * the scorer run so any score the scorer emits is correctly multi-tenant.
 */
async function runScorerForTrace({
  scorer,
  trace,
  span,
}: {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Verify the traceId by fetching it: await storage.getStore('observability').getTrace({ traceId }).
  2. Confirm tracing/observability is enabled and traces are persisted to the configured storage.
  3. Check retention/TTL settings if the trace is old.
  4. Ensure you're querying the same storage/environment the trace was written to.
  5. Correct the traceId source — get it from workflowResult.traceId / run traces rather than hardcoding.

Example fix

// before
await score({ target: { traceId: 'hardcoded-trace-id' } });
// after: use the id from the actual run
const result = await run.start({ inputData });
await score({ target: { traceId: result.traceId } });
Defensive patterns

Strategy: validation

Validate before calling

// confirm the trace exists before scoring
const store = await storage.getStore('observability');
const trace = await store?.getTrace({ traceId });
if (!trace) throw new Error(`Trace ${traceId} not persisted; check tracing config and retention`);

Type guard

async function traceIsScorable(storage, traceId) {
  const store = await storage.getStore('observability');
  if (!store) return false;
  const trace = await store.getTrace({ traceId });
  return trace != null && trace.spans.length > 0;
}

Try / catch

try {
  await scoreTraces({ target: { traceId } });
} catch (e) {
  if (typeof e.message === 'string' && e.message.startsWith('Trace not found for scoring')) {
    console.error(`Trace ${traceId} missing — was tracing enabled and retention sufficient?`);
  }
  throw e;
}

Prevention

When it happens

Trigger: scoreTraces workflow invoked with target { traceId } where the traceId was never persisted (tracing disabled), was pruned by retention, belongs to a different environment/storage, or the id is mistyped.

Common situations: Passing a traceId from local dev against a staging database; observability exporter not configured so traces never persist; retention TTL expired the trace; typo'd or fabricated traceId; storage region/project mismatch in multi-tenant setups.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/56dfe8741831409c. Report an issue: GitHub.