{"record":{"id":"5f2b687b99b4b077","repo":"mastra-ai/mastra","slug":"vector-dimensions-must-match-vec1-vec1-length","errorCode":null,"errorMessage":"Vector dimensions must match: vec1(${vec1.length}) !== vec2(${vec2.length})","messagePattern":"Vector dimensions must match: vec1\\((.+?)\\) !== vec2\\((.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/rag/src/graph-rag/index.ts","lineNumber":200,"sourceCode":"  private getNeighbors(nodeId: string, edgeType?: string): { id: string; weight: number }[] {\n    return this.edges\n      .filter(edge => edge.source === nodeId && (!edgeType || edge.type === edgeType))\n      .map(edge => ({\n        id: edge.target,\n        weight: edge.weight,\n      }))\n      .filter(node => node !== undefined);\n  }\n\n  // Calculate cosine similarity between two vectors\n  private cosineSimilarity(vec1: number[], vec2: number[]): number {\n    if (!vec1 || !vec2) {\n      throw new Error('Vectors must not be null or undefined');\n    }\n    const vectorLength = vec1.length;\n\n    if (vectorLength !== vec2.length) {\n      throw new Error(`Vector dimensions must match: vec1(${vec1.length}) !== vec2(${vec2.length})`);\n    }\n\n    let dotProduct = 0;\n    let normVec1 = 0;\n    let normVec2 = 0;\n\n    for (let i = 0; i < vectorLength; i++) {\n      const a = vec1[i]!; // Non-null assertion operator\n      const b = vec2[i]!;\n\n      dotProduct += a * b;\n      normVec1 += a * a;\n      normVec2 += b * b;\n    }\n    const magnitudeProduct = Math.sqrt(normVec1 * normVec2);\n\n    if (magnitudeProduct === 0) {\n      return 0;","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/rag/src/graph-rag/index.ts#L182-L218","documentation":"cosineSimilarity requires both vectors to have identical length; it throws with the two mismatched lengths otherwise. Since node embeddings and query embeddings can come from different embedding models, dimension drift between them triggers this inside query().","triggerScenarios":"Querying a graph built with one embedding model/dimension using a query vector of another dimension (e.g. 1536 vs 768 vs 3072); createGraph with ragged embedding arrays of unequal lengths.","commonSituations":"Switching embedding providers (OpenAI text-embedding-3-small 1536 -> large 3072, or a 768-dim local model) without rebuilding the index; configuring the wrong model in one environment; passing un-truncated variable-length vectors.","solutions":["Rebuild the graph with createGraph using embeddings from the same model you will query with.","Make the query embedding dimension match the GraphRAG constructor dimension (default 1536).","Pin a single embedding model across indexing and query code paths and record it in metadata.","Validate vector lengths client-side before calling query()."],"exampleFix":"// before\nconst results = graph.query({ query: await embed(text) });\n// after\nconst q = await embed(text); // same model used at index time\nif (q.length !== 1536) throw new Error(`Rebuild index: embedding dim ${q.length} != 1536`);\nconst results = graph.query({ query: q });","handlingStrategy":"validation","validationCode":"const dim = 1536; // dimension of the model used at index time\nif (!Array.isArray(query) || query.length !== dim) {\n  throw new Error(`Query vector must be ${dim}-dim; got ${query?.length}`);\n}","typeGuard":"const hasCorrectDimension = (v: unknown, dim: number): v is number[] =>\n  Array.isArray(v) && v.length === dim && v.every(x => typeof x === 'number');","tryCatchPattern":"try {\n  const results = graph.query({ query });\n} catch (e) {\n  if ((e as Error).message.startsWith('Vector dimensions must match')) {\n    // embedding model drift: rebuild the graph with the current model\n  } else throw e;\n}","preventionTips":["Pin one embedding model for both indexing and querying","Rebuild the graph after any embedding model change","Record the embedding model/dimension in node metadata or snapshot storage","Validate query vector length against graph dimension before every query"],"tags":["rag","dimension-mismatch","embedding","argument-error"],"backgroundTag":"vector-dimension-mismatch","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}