{"record":{"id":"61581c3500541979","repo":"mastra-ai/mastra","slug":"dataset-not-found","errorCode":"DATASET_NOT_FOUND","errorMessage":"Dataset not found: ${this.id}","messagePattern":"Dataset not found: (.+?)","errorType":"error_code","errorClass":"MastraError","httpStatus":null,"severity":"error","filePath":"packages/core/src/datasets/dataset.ts","lineNumber":131,"sourceCode":"\n    this.#experimentsStore = store;\n    return store;\n  }\n\n  /**\n   * Preflight tenancy gate for storage APIs whose signatures don't accept\n   * `filters`. When the handle has a `#scope`, a scoped `getDatasetById` is\n   * used to prove the dataset exists in the caller's tenancy; on miss we\n   * throw NOT_FOUND, mirroring {@link Dataset.getDetails}. Callers that must\n   * return a non-throwing empty result (e.g. list endpoints) should catch and\n   * translate.\n   */\n  async #assertScope(): Promise<void> {\n    if (!this.#scope) return;\n    const store = await this.#getDatasetsStore();\n    const record = await store.getDatasetById({ id: this.id, filters: this.#scope });\n    if (!record) {\n      throw new MastraError({\n        id: 'DATASET_NOT_FOUND',\n        text: `Dataset not found: ${this.id}`,\n        domain: 'STORAGE',\n        category: 'USER',\n      });\n    }\n  }\n\n  /**\n   * Ownership gate: verifies a child record's `datasetId` matches `this.id`.\n   * Prevents a valid scoped handle from reading/mutating child records\n   * (items, experiments, results) that live under a different dataset — even\n   * one in the same tenant. Returns `null` when the record is missing or\n   * belongs to a different dataset, so callers can either return null or\n   * translate to NOT_FOUND depending on their contract.\n   */\n  #ownsChild<T extends { datasetId?: string | null }>(record: T | null | undefined): T | null {\n    if (!record) return null;","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/datasets/dataset.ts#L113-L149","documentation":"#assertScope() verifies the Dataset still exists within its configured scope filters before item/version/experiment operations. DATASET_NOT_FOUND is thrown when getDatasetById with the dataset id and scope filters returns no record. This is a user-facing existence check, not a corruption signal.","triggerScenarios":"Calling getItem, listItems, listVersions, getItemHistory, listExperiments (or internal #assertExperimentOwnership) on a Dataset whose id was deleted, or whose id does not match the current scope filters.","commonSituations":"Holding a Dataset object across a delete; constructing a Dataset with a mistyped id; scope filters (e.g. tenant/agent scoping) excluding the dataset; storage wiped between runs.","solutions":["Verify the dataset id exists in storage (list datasets or query the datasets table).","Check the scope filters passed to the Dataset — relax or correct them so the record matches.","Recreate the dataset or re-fetch a fresh Dataset instance via the Mastra datasets API before operating on it."],"exampleFix":"// before\nconst ds = mastra.getDataset('old-id');\nawait ds.listItems(); // DATASET_NOT_FOUND after deletion\n// after\nconst ds = mastra.getDataset('old-id');\nif (!(await mastra.listDatasets()).some(d => d.id === ds.id)) { ds = await mastra.createDataset({...}); }","handlingStrategy":"try-catch","validationCode":"const exists = (await mastra.listDatasets?.() ?? []).some(d => d.id === dataset.id);\nif (!exists) throw new Error(`Dataset ${dataset.id} missing before operation`);","typeGuard":"null","tryCatchPattern":"try { await dataset.getItem(id); } catch (e) { if (e?.id === 'DATASET_NOT_FOUND') return null; throw e; }","preventionTips":["Re-fetch Dataset handles after deletions instead of caching them","Confirm scope filters match how the dataset was created","Guard long-lived Dataset references with periodic existence checks"],"tags":["datasets","not-found","storage"],"backgroundTag":"resource-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}