dataset.createExperiment()
Added in: @mastra/core@1.61.0
Creates an experiment without starting a run. Your own orchestrator (for example a Temporal workflow) drives the loop in one of two shapes:
- With a target: pass
targetTypeandtargetId, then callrunExperimentItem()per item. Mastra executes the target and runs the scorers server-side. - Without a target: omit both and run everything on your own infrastructure, ingesting each result with
submitExperimentResult().
Both shapes finish with finalizeExperiment().
The experiment pins the dataset version at creation time, so submissions are validated against a stable set of items even if the dataset changes afterwards.
Usage exampleDirect link to Usage example
import { Mastra } from '@mastra/core'
const mastra = new Mastra({/* storage config */})
const dataset = await mastra.datasets.get({ id: 'dataset-id' })
const { experimentId, totalItems, datasetVersion } = await dataset.createExperiment({
id: 'temporal-wf-run-42', // optional: idempotent create on retry
targetType: 'agent',
targetId: 'translation-agent',
scorers: ['accuracy'],
})
Passing your own id makes creation idempotent: calling it again with the same id returns the existing experiment instead of failing, so a retried workflow activity is safe. If the id belongs to an experiment on another dataset or an experiment with a different target, the call throws an EXPERIMENT_ID_CONFLICT error.
targetType and targetId must be provided together, and the target must exist in the Mastra registry at create time. scorers requires a target because Mastra never scores target-less experiments; submit flat scores through submitExperimentResult instead.
ParametersDirect link to Parameters
id?:
targetType?:
runExperimentItem() executes. Provide together with targetId, or omit both for pure ingestion.targetId?:
targetType.scorers?:
runExperimentItem(). Requires a target.name?:
description?:
metadata?:
provenance?:
grouping?:
experimentSetId, comparisonId, variantId, trialIndex) for organizing related runs.version?:
ReturnsDirect link to Returns
result:
experimentId:
status:
'running' for a new experiment; the stored status when an existing experiment is returned.