Skip to main content

Dataset class

Added in: @mastra/core@1.4.0

The Dataset class provides methods for item CRUD, versioning, and experiment management on a single dataset. Obtained via mastra.datasets.get() or mastra.datasets.create().

Usage examples
Direct link to Usage examples

Add items and run an experiment
Direct link to Add items and run an experiment

src/mastra/datasets/usage.ts
import { Mastra } from '@mastra/core'

const mastra = new Mastra({/* storage config */})

const dataset = await mastra.datasets.create({
name: 'QA pairs',
})

// Add items
await dataset.addItems({
items: [
{ input: { question: 'What is AI?' }, groundTruth: { answer: 'Artificial Intelligence' } },
{ input: { question: 'What is ML?' }, groundTruth: { answer: 'Machine Learning' } },
],
})

// Run an experiment
const summary = await dataset.startExperiment({
targetType: 'agent',
targetId: 'my-agent',
scorers: ['accuracy'],
})

console.log(`${summary.succeededCount}/${summary.totalItems} succeeded`)

Manage versions and history
Direct link to Manage versions and history

src/mastra/datasets/versioning.ts
import { Mastra } from '@mastra/core'

const mastra = new Mastra({/* storage config */})

const dataset = await mastra.datasets.get({ id: 'dataset-id' })

// List versions
const { versions } = await dataset.listVersions()

// List items at a specific version
const items = await dataset.listItems({ version: 2 })

// Get item change history
const history = await dataset.getItemHistory({ itemId: 'item-id' })

Access
Direct link to Access

Obtain a Dataset through DatasetsManager, which resolves the dataset and applies tenancy scope:

const dataset = await mastra.datasets.get({ id: 'dataset-id' })
// or
const dataset = await mastra.datasets.create({ name: 'My dataset' })

Dataset also has a public constructor (new Dataset(id, mastra)) that skips the manager's lookup and tenancy scoping.

Properties
Direct link to Properties

For the full dataset record (name, description, schemas, version, timestamps), call dataset.getDetails().

id:

string
The unique identifier of the dataset. Read-only.