{"record":{"id":"815e69758c5e03c1","repo":"mastra-ai/mastra","slug":"item-not-found-in-dataset-itemid","errorCode":null,"errorMessage":"Item not found in dataset: ${itemId}","messagePattern":"Item not found in dataset: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"packages/server/src/server/handlers/datasets.ts","lineNumber":1135,"sourceCode":"\nexport const LIST_ITEM_VERSIONS_ROUTE = createRoute({\n  method: 'GET',\n  path: '/datasets/:datasetId/items/:itemId/history',\n  responseType: 'json',\n  pathParamSchema: datasetAndItemIdPathParams,\n  responseSchema: listItemVersionsResponseSchema,\n  summary: 'Get item history',\n  description: 'Returns the full SCD-2 history of the item across all dataset versions',\n  tags: ['Datasets'],\n  requiresAuth: true,\n  handler: async ({ mastra, datasetId, itemId }) => {\n    assertDatasetsAvailable();\n    try {\n      const ds = await mastra.datasets.get({ id: datasetId });\n      const rows = await ds.getItemHistory({ itemId });\n      // Check rows belong to this dataset\n      if (rows.length > 0 && rows[0]?.datasetId !== datasetId) {\n        throw new HTTPException(404, { message: `Item not found in dataset: ${itemId}` });\n      }\n      return { history: rows };\n    } catch (error) {\n      if (error instanceof MastraError) {\n        throw new HTTPException(getHttpStatusForMastraError(error.id) as StatusCode, { message: error.message });\n      }\n      return handleError(error, 'Error listing item history');\n    }\n  },\n});\n\nexport const GET_ITEM_VERSION_ROUTE = createRoute({\n  method: 'GET',\n  path: '/datasets/:datasetId/items/:itemId/versions/:datasetVersion',\n  responseType: 'json',\n  pathParamSchema: datasetItemVersionPathParams,\n  responseSchema: datasetItemResponseSchema.nullable(),\n  summary: 'Get item at specific dataset version',","sourceCodeStart":1117,"sourceCodeEnd":1153,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/datasets.ts#L1117-L1153","documentation":"The LIST_ITEM_VERSIONS route (GET /datasets/:datasetId/items/:itemId/history) returns the SCD-2 history rows for an item. Because getItemHistory looks up rows by itemId globally, the handler verifies the first returned row's datasetId matches the path's datasetId; if it doesn't, the item exists but belongs to a different dataset, so a 404 is raised rather than leaking the item's existence in another dataset.","triggerScenarios":"Call GET /api/datasets/:datasetId/items/:itemId/history with an itemId that exists in some dataset but not the one named in the path. getItemHistory returns rows whose datasetId differs from the path datasetId.","commonSituations":"Copy-pasting an itemId from one dataset while calling against another datasetId in the URL; stale cached IDs after a dataset was re-created with a new ID; multi-tenant setups where two datasets contain items with the same itemId.","solutions":["Verify the itemId actually belongs to the datasetId in the request path (list items via GET /datasets/:datasetId/items and cross-check).","Fix a mixed-up datasetId/itemId pair in the client code or saved configuration.","If the item was moved or the dataset recreated, use the new dataset's ID or re-add the item.","Ensure you're pointing at the correct Mastra server/deployment (IDs may differ across environments)."],"exampleFix":"// before\nawait fetch(`/api/datasets/${wrongDatasetId}/items/${itemId}/history`);\n// after\nconst items = await fetch(`/api/datasets/${datasetId}/items`).then(r => r.json());\nif (!items.items.some(i => i.id === itemId)) throw new Error('item not in this dataset');\nawait fetch(`/api/datasets/${datasetId}/items/${itemId}/history`);","handlingStrategy":"try-catch","validationCode":"const items = await fetch(`/api/datasets/${datasetId}/items?perPage=100`).then(r => r.json());\nif (!items.items.some(i => i.id === itemId)) {\n  throw new Error(`Item ${itemId} does not belong to dataset ${datasetId}`);\n}","typeGuard":"function isItemInDataset(history: Array<{ datasetId: string }>, datasetId: string): boolean {\n  return history.length === 0 || history[0]?.datasetId === datasetId;\n}","tryCatchPattern":"try {\n  const res = await fetch(`/api/datasets/${datasetId}/items/${itemId}/history`);\n  if (res.status === 404) {\n    console.warn(`Item ${itemId} not in dataset ${datasetId}; verify IDs`);\n    return null;\n  }\n  if (!res.ok) throw new Error(await res.text());\n  return await res.json();\n} catch (err) {\n  console.error('listItemVersions failed', err);\n  throw err;\n}","preventionTips":["Always source itemId and datasetId from the same API response so they can't drift apart.","Keep a client-side map of datasetId -> itemIds and check membership before cross-dataset calls.","After recreating a dataset, invalidate cached item IDs.","Log the full URL on 404s to quickly spot mixed-up path parameters."],"tags":["http-404","datasets","rest-api","identifier-mismatch"],"backgroundTag":"resource-not-found-in-scope","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}