mastra-ai/mastra · error · Error
Experiment not found: ${experimentIdA}
Error message
Experiment not found: ${experimentIdA} What it means
compareExperiments fetches both experiments by ID before comparing their scores. This error means the first experiment (experimentIdA) does not exist in the experiments store — getExperimentById returned undefined. It is thrown as a plain Error so the caller can distinguish a bad ID from a comparison failure.
Source
Thrown at packages/core/src/datasets/experiment/analytics/compare.ts:82
const experimentsStore = await storage.getStore('experiments');
const scoresStore = await storage.getStore('scores');
if (!experimentsStore) {
throw new Error('ExperimentsStorage not configured.');
}
if (!scoresStore) {
throw new Error('ScoresStorage not configured.');
}
// 2. Load both experiments
const [experimentA, experimentB] = await Promise.all([
experimentsStore.getExperimentById({ id: experimentIdA }),
experimentsStore.getExperimentById({ id: experimentIdB }),
]);
if (!experimentA) {
throw new Error(`Experiment not found: ${experimentIdA}`);
}
if (!experimentB) {
throw new Error(`Experiment not found: ${experimentIdB}`);
}
// 3. Check version mismatch
const versionMismatch = experimentA.datasetVersion !== experimentB.datasetVersion;
if (versionMismatch) {
warnings.push(
`Experiments have different dataset versions: ${experimentA.datasetVersion} vs ${experimentB.datasetVersion}`,
);
}
// 4. Load results for both experiments
const [resultsA, resultsB] = await Promise.all([
experimentsStore.listExperimentResults({ experimentId: experimentIdA, pagination: { page: 0, perPage: false } }),
experimentsStore.listExperimentResults({ experimentId: experimentIdB, pagination: { page: 0, perPage: false } }),
]);View on GitHub (pinned to 75dd419e61)
Solutions
- Log/verify experimentIdA exists via experimentsStore.getExperimentById({ id: experimentIdA }) before comparing
- Check you are connected to the same Mastra storage instance that recorded the experiment
- Re-run the experiment if it was deleted, and compare using the new ID
- Confirm the ID string has no whitespace/truncation (copy full UUID)
Example fix
// before
await mastra.compareExperiments({ experimentA: staleId, experimentB: idB });
// after
const a = await experimentsStore.getExperimentById({ id: staleId });
if (!a) throw new Error(`Experiment ${staleId} missing; re-run experiment first`);
await mastra.compareExperiments({ experimentA: staleId, experimentB: idB }); Defensive patterns
Strategy: validation
Validate before calling
const a = await experimentsStore.getExperimentById({ id: experimentIdA });
if (!a) throw new Error(`Experiment A not found: ${experimentIdA}`); Type guard
function isExperiment(e: unknown): e is NonNullable<Awaited<ReturnType<typeof experimentsStore.getExperimentById>>> {
return !!e && typeof e === 'object' && 'id' in e;
} Try / catch
try {
await mastra.compareExperiments({ experimentA, experimentB });
} catch (err) {
if (err instanceof Error && err.message.startsWith('Experiment not found')) {
// re-run or re-create the missing experiment
} else throw err;
} Prevention
- Store experiment IDs centrally (config/DB) right after creation
- Pre-check both experiment IDs before comparing
- Pin the storage instance used for experiments per environment
- Never hardcode experiment IDs across environments
When it happens
Trigger: Calling compareExperiments({ experimentA, experimentB }) (or the equivalent options object) where the first experiment ID was deleted, never created, or belongs to a different storage backend than the one wired into the Mastra instance.
Common situations: Using an experiment ID from a dev database in a prod environment; an experiment removed by a cleanup/retention job; a typo'd or truncated UUID; storage class swapped (e.g. InMemory store restarted, wiping experiments) between run and compare.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- Experiment not found: ${experimentIdB}
- EXPERIMENT_NOT_FOUND
- EXPERIMENT_TARGET_NOT_FOUND
- DATASET_NOT_FOUND
- Dataset not found: ${args.id}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/6f156e63e13ccd9e.
Report an issue: GitHub.