{"record":{"id":"2ff37aab3e82def4","repo":"mastra-ai/mastra","slug":"dataset-not-found-input-datasetid","errorCode":null,"errorMessage":"Dataset not found: ${input.datasetId}","messagePattern":"Dataset not found: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/datasets/base.ts","lineNumber":236,"sourceCode":"  abstract listItems(args: ListDatasetItemsInput): Promise<ListDatasetItemsOutput>;\n  abstract getItemById(args: { id: string; datasetVersion?: number }): Promise<DatasetItem | null>;\n\n  // SCD-2 queries\n  abstract getItemsByVersion(args: { datasetId: string; version: number }): Promise<DatasetItem[]>;\n  abstract getItemHistory(itemId: string): Promise<DatasetItemRow[]>;\n\n  // Dataset version methods\n  abstract createDatasetVersion(datasetId: string, version: number): Promise<DatasetVersion>;\n  abstract listDatasetVersions(input: ListDatasetVersionsInput): Promise<ListDatasetVersionsOutput>;\n\n  /**\n   * Batch insert items to a dataset. Validates all items against dataset schemas,\n   * then delegates to subclass which handles SCD-2 versioning internally.\n   */\n  async batchInsertItems(input: BatchInsertItemsInput): Promise<DatasetItem[]> {\n    const dataset = await this.getDatasetById({ id: input.datasetId, filters: input.filters });\n    if (!dataset) {\n      throw new Error(`Dataset not found: ${input.datasetId}`);\n    }\n\n    // Validate all items against schemas\n    const validator = getSchemaValidator();\n    const cacheKey = `dataset:${input.datasetId}`;\n\n    for (const [index, itemData] of input.items.entries()) {\n      validateDatasetItemExternalId(itemData.externalId);\n      validateDatasetItemPayloadSerialization(itemData, `items[${index}]`);\n      if (dataset.inputSchema) {\n        validator.validate(itemData.input, dataset.inputSchema, 'input', `${cacheKey}:input`);\n      }\n      if (dataset.groundTruthSchema && itemData.groundTruth !== undefined) {\n        validator.validate(itemData.groundTruth, dataset.groundTruthSchema, 'groundTruth', `${cacheKey}:output`);\n      }\n    }\n\n    return this._doBatchInsertItems(input);","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/datasets/base.ts#L218-L254","documentation":"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.","triggerScenarios":"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.).","commonSituations":"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.","solutions":["Call getDatasetById({id: datasetId, filters}) first and create the dataset via createDataset() when it returns null.","Log/verify the exact datasetId string being passed — check for typos or IDs from a different database.","If using tenancy filters, confirm the organizationId/projectId values match the dataset row's stored values.","Catch the error and surface a user-facing 'dataset does not exist (or is not accessible)' message."],"exampleFix":"// before\nawait storage.datasets.addItems({ datasetId: 'my-ds', items: [{ input: x }] });\n// after\nlet ds = await storage.datasets.getDatasetById({ id: 'my-ds' });\nif (!ds) ds = await storage.datasets.createDataset({ id: 'my-ds', name: 'My Dataset' });\nawait storage.datasets.addItems({ datasetId: 'my-ds', items: [{ input: x }] });","handlingStrategy":"validation","validationCode":"const ds = await storage.datasets.getDatasetById({ id: datasetId, filters });\nif (!ds) throw new Error(`Refusing to insert: dataset '${datasetId}' does not exist (or filters exclude it)`);","typeGuard":"function datasetExists(ds: Awaited<ReturnType<typeof storage.datasets.getDatasetById>>): ds is NonNullable<typeof ds> {\n  return ds !== null;\n}","tryCatchPattern":"try {\n  await storage.datasets.batchInsertItems({ datasetId, filters, items });\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Dataset not found:')) {\n    // create the dataset or surface a 'dataset unavailable' state\n  } else throw e;\n}","preventionTips":["Always create datasets via createDataset() and keep the returned record/ID as the single source of truth.","When using tenancy filters, derive them from the same context that created the dataset.","Pre-check getDatasetById before batch writes in long-lived jobs."],"tags":["storage","datasets","not-found"],"backgroundTag":"dataset-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}