{"record":{"id":"bd581b722ee286d1","repo":"mastra-ai/mastra","slug":"compare-invalid-input","errorCode":"COMPARE_INVALID_INPUT","errorMessage":"compareExperiments requires at least 2 experiment IDs.","messagePattern":"compareExperiments requires at least 2 experiment IDs\\.","errorType":"error_code","errorClass":"MastraError","httpStatus":null,"severity":"error","filePath":"packages/core/src/datasets/manager.ts","lineNumber":249,"sourceCode":"  async getExperiment(args: { experimentId: string; organizationId?: string; projectId?: string }) {\n    const experimentsStore = await this.#getExperimentsStore();\n    return experimentsStore.getExperimentById({\n      id: args.experimentId,\n      filters: scopeFromArgs(args),\n    });\n  }\n\n  /**\n   * Compare two or more experiments.\n   *\n   * Uses the internal `compareExperiments` function for pairwise comparison,\n   * then enriches results with per-item input/groundTruth/output data.\n   */\n  async compareExperiments(args: { experimentIds: string[]; baselineId?: string }) {\n    const { experimentIds, baselineId } = args;\n\n    if (experimentIds.length < 2) {\n      throw new MastraError({\n        id: 'COMPARE_INVALID_INPUT',\n        text: 'compareExperiments requires at least 2 experiment IDs.',\n        domain: 'STORAGE',\n        category: 'USER',\n      });\n    }\n\n    const resolvedBaseline = baselineId ?? experimentIds[0]!;\n    const otherExperimentId = experimentIds.find(id => id !== resolvedBaseline) ?? experimentIds[1]!;\n\n    const internal = await compareExperimentsInternal(this.#mastra, {\n      experimentIdA: resolvedBaseline,\n      experimentIdB: otherExperimentId,\n    });\n\n    // Load results for both runs to get input/groundTruth/output\n    const experimentsStore = await this.#getExperimentsStore();\n    const [resultsA, resultsB] = await Promise.all([","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/datasets/manager.ts#L231-L267","documentation":"MastraStorage's compareExperiments() compares run results across at least two experiment IDs and enriches the comparison with per-item input/groundTruth/output data. The library throws COMPARE_INVALID_INPUT when fewer than 2 experiment IDs are passed, since a comparison needs at least two experiments to produce rows. This is a user-input error, thrown before any storage access.","triggerScenarios":"Calling compareExperiments({ experimentIds }) with an empty array, a single-element array, or an array that was dynamically filtered down to fewer than 2 valid IDs.","commonSituations":"Building a UI that compares experiments but letting the user click 'Compare' with only one experiment selected; deriving experiment IDs from query results where most rows were filtered out; a fresh project where only one experiment has run yet.","solutions":["Ensure the caller collects and passes at least 2 distinct experiment IDs before invoking compareExperiments.","Guard the call site: check `experimentIds.length >= 2` and short-circuit with a friendly message otherwise.","If IDs are derived dynamically, validate the filtered list length before calling and surface a 'select more experiments' state in the UI."],"exampleFix":"// before\nconst result = await storage.compareExperiments({ experimentIds: [selected[0]] });\n// after\nif (selected.length < 2) throw new Error('Select at least 2 experiments to compare');\nconst result = await storage.compareExperiments({ experimentIds: selected });","handlingStrategy":"validation","validationCode":"if (!Array.isArray(ids) || ids.length < 2) {\n  throw new TypeError('compareExperiments requires at least 2 experiment IDs');\n}\nawait storage.compareExperiments({ experimentIds: ids });","typeGuard":"function hasAtLeastTwoIds(ids: unknown): ids is [string, string, ...string[]] {\n  return Array.isArray(ids) && ids.length >= 2 && ids.every((id): id is string => typeof id === 'string');\n}","tryCatchPattern":"try {\n  const result = await storage.compareExperiments({ experimentIds: ids });\n} catch (e) {\n  if (e instanceof MastraError && e.id === 'COMPARE_INVALID_INPUT') {\n    return { error: 'Please select at least 2 experiments to compare.' };\n  }\n  throw e;\n}","preventionTips":["Guard with `ids.length >= 2` before calling.","In UIs, disable the compare action until 2+ experiments are selected.","Filter IDs first, then check the filtered array, not the original selection."],"tags":["storage","validation","datasets","argument-error"],"backgroundTag":"invalid-argument-count","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}