mastra-ai/mastra · error · Error

Dataset not found: ${input.datasetId}

Error message

Dataset not found: ${input.datasetId}

What it means

batchInsertItems() looks up the dataset with getDatasetById({id, filters}) before validating/inserting items; if no dataset row matches (wrong ID or tenancy-filter mismatch), it throws this plain Error. getDatasetById returns null rather than throwing on filter mismatch, so a tenancy mismatch is indistinguishable from a missing row. addItems() delegates here, so a failed single-item add reports the same message.

Source

Thrown at packages/core/src/storage/domains/datasets/base.ts:236

  abstract listItems(args: ListDatasetItemsInput): Promise<ListDatasetItemsOutput>;
  abstract getItemById(args: { id: string; datasetVersion?: number }): Promise<DatasetItem | null>;

  // SCD-2 queries
  abstract getItemsByVersion(args: { datasetId: string; version: number }): Promise<DatasetItem[]>;
  abstract getItemHistory(itemId: string): Promise<DatasetItemRow[]>;

  // Dataset version methods
  abstract createDatasetVersion(datasetId: string, version: number): Promise<DatasetVersion>;
  abstract listDatasetVersions(input: ListDatasetVersionsInput): Promise<ListDatasetVersionsOutput>;

  /**
   * Batch insert items to a dataset. Validates all items against dataset schemas,
   * then delegates to subclass which handles SCD-2 versioning internally.
   */
  async batchInsertItems(input: BatchInsertItemsInput): Promise<DatasetItem[]> {
    const dataset = await this.getDatasetById({ id: input.datasetId, filters: input.filters });
    if (!dataset) {
      throw new Error(`Dataset not found: ${input.datasetId}`);
    }

    // Validate all items against schemas
    const validator = getSchemaValidator();
    const cacheKey = `dataset:${input.datasetId}`;

    for (const [index, itemData] of input.items.entries()) {
      validateDatasetItemExternalId(itemData.externalId);
      validateDatasetItemPayloadSerialization(itemData, `items[${index}]`);
      if (dataset.inputSchema) {
        validator.validate(itemData.input, dataset.inputSchema, 'input', `${cacheKey}:input`);
      }
      if (dataset.groundTruthSchema && itemData.groundTruth !== undefined) {
        validator.validate(itemData.groundTruth, dataset.groundTruthSchema, 'groundTruth', `${cacheKey}:output`);
      }
    }

    return this._doBatchInsertItems(input);

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Call getDatasetById({id: datasetId, filters}) first and create the dataset via createDataset() when it returns null.
  2. Log/verify the exact datasetId string being passed — check for typos or IDs from a different database.
  3. If using tenancy filters, confirm the organizationId/projectId values match the dataset row's stored values.
  4. Catch the error and surface a user-facing 'dataset does not exist (or is not accessible)' message.

Example fix

// before
await storage.datasets.addItems({ datasetId: 'my-ds', items: [{ input: x }] });
// after
let ds = await storage.datasets.getDatasetById({ id: 'my-ds' });
if (!ds) ds = await storage.datasets.createDataset({ id: 'my-ds', name: 'My Dataset' });
await storage.datasets.addItems({ datasetId: 'my-ds', items: [{ input: x }] });
Defensive patterns

Strategy: validation

Validate before calling

const ds = await storage.datasets.getDatasetById({ id: datasetId, filters });
if (!ds) throw new Error(`Refusing to insert: dataset '${datasetId}' does not exist (or filters exclude it)`);

Type guard

function datasetExists(ds: Awaited<ReturnType<typeof storage.datasets.getDatasetById>>): ds is NonNullable<typeof ds> {
  return ds !== null;
}

Try / catch

try {
  await storage.datasets.batchInsertItems({ datasetId, filters, items });
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Dataset not found:')) {
    // create the dataset or surface a 'dataset unavailable' state
  } else throw e;
}

Prevention

When it happens

Trigger: Calling storage.datasets.addItems() or batchInsertItems() with a datasetId that was never created, was deleted (deleteDataset), or whose row does not match the supplied DatasetTenancyFilters (organizationId/projectId etc.).

Common situations: Using a dataset ID from another environment (dev vs prod DB), a typo'd or truncated ID, the dataset being deleted by another process before the insert, or multi-tenant code passing filters that don't match the row actually stored.

Related errors


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