mastra-ai/mastra · error · Error

Dataset not found: ${input.datasetId}

Error message

Dataset not found: ${input.datasetId}

What it means

batchInsertItems looks up the target dataset before inserting rows and throws when datasetId does not resolve. Unlike single-item paths, no items are written and the dataset version is untouched.

Source

Thrown at packages/core/src/storage/domains/datasets/inmemory.ts:550

    const end = perPageInput === false ? versions.length : start + perPage;

    return {
      versions: versions.slice(start, end),
      pagination: {
        total: versions.length,
        page,
        perPage: perPageForResponse,
        hasMore: perPageInput === false ? false : versions.length > end,
      },
    };
  }

  // --- Bulk operations (SCD-2 internally) ---

  protected async _doBatchInsertItems(input: BatchInsertItemsInput): Promise<DatasetItem[]> {
    const dataset = this.db.datasets.get(input.datasetId);
    if (!dataset) {
      throw new Error(`Dataset not found: ${input.datasetId}`);
    }
    if (input.items.length === 0) return [];

    const acceptedByExternalId = new Map<string, { first: DatasetItemRow; current: DatasetItemRow | null }>();
    for (const rows of this.db.datasetItems.values()) {
      const first = rows[0];
      if (!first || first.datasetId !== input.datasetId || !first.externalId) continue;
      const existing = acceptedByExternalId.get(first.externalId);
      if (existing && existing.first.id !== first.id) {
        throw new Error(`Dataset item identity history is corrupt for externalId: ${first.externalId}`);
      }
      acceptedByExternalId.set(first.externalId, {
        first,
        current: rows.find(row => row.validTo === null && !row.isDeleted) ?? null,
      });
    }

    const conflicts = [];

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Create the dataset first (createDataset) and use the returned id.
  2. Log/verify the datasetId string — check for typos or trimmed/undefined values.
  3. Check the dataset wasn't deleted earlier in your flow; re-create if needed.
  4. Ensure the same storage instance is used throughout the request lifecycle.

Example fix

// before
await storage.datasets.batchInsertItems({ datasetId: maybeId, items });
// after
const ds = await storage.datasets.get(maybeId);
if (!ds) {
  const created = await storage.datasets.create({ name: 'my-dataset' });
  await storage.datasets.batchInsertItems({ datasetId: created.id, items });
}
Defensive patterns

Strategy: validation

Validate before calling

const ds = await storage.datasets.get(datasetId);
if (!ds) {
  const created = await storage.datasets.create({ name: 'auto' });
  datasetId = created.id;
}

Type guard

null

Try / catch

try {
  await storage.datasets.batchInsertItems({ datasetId, items });
} catch (e) {
  if (String(e).startsWith('Dataset not found')) {
    const created = await storage.datasets.create({ name: 'recovered' });
    await storage.datasets.batchInsertItems({ datasetId: created.id, items });
  } else throw e;
}

Prevention

When it happens

Trigger: storage.datasets.batchInsertItems({ datasetId, items }) with a deleted or never-created datasetId; typo in dataset id; calling after store restart; calling with an id from a different storage instance.

Common situations: Seeding a dataset created conditionally (create failed silently upstream); using an id persisted from a previous in-memory run; multi-instance setups where ids belong to another process's memory.

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


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