{"record":{"id":"abedde87fcc4ace3","repo":"mastra-ai/mastra","slug":"invalid-metric-must-be-one-of-cosine-euclidean","errorCode":null,"errorMessage":"Invalid metric. Must be one of: cosine, euclidean, dotproduct","messagePattern":"Invalid metric\\. Must be one of: cosine, euclidean, dotproduct","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"packages/server/src/server/handlers/vector.ts","lineNumber":100,"sourceCode":"}\n\n// Create index\nexport async function createIndex({\n  mastra,\n  vectorName,\n  indexName,\n  dimension,\n  metric,\n}: Pick<VectorContext, 'mastra' | 'vectorName'> & CreateIndexRequest) {\n  try {\n    if (!indexName || typeof dimension !== 'number' || dimension <= 0) {\n      throw new HTTPException(400, {\n        message: 'Invalid request index, indexName and positive dimension number are required.',\n      });\n    }\n\n    if (metric && !['cosine', 'euclidean', 'dotproduct'].includes(metric)) {\n      throw new HTTPException(400, { message: 'Invalid metric. Must be one of: cosine, euclidean, dotproduct' });\n    }\n\n    const vector = getVector(mastra, vectorName);\n    await vector.createIndex({ indexName, dimension, metric });\n    return { success: true };\n  } catch (error) {\n    return handleError(error, 'Error creating index');\n  }\n}\n\n// Query vectors\nexport async function queryVectors({\n  mastra,\n  vectorName,\n  indexName,\n  queryVector,\n  topK,\n  filter,","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/vector.ts#L82-L118","documentation":"This HTTP 400 error is thrown by the createIndex handler when a metric is supplied but is not one of the three supported similarity metrics: 'cosine', 'euclidean', or 'dotproduct'. The check runs after the indexName/dimension validation and before any vector store call. Omitting metric entirely is allowed (the store default applies); only an invalid explicit value triggers it.","triggerScenarios":"POSTing to create-index with metric set to values like 'dot', 'euclidean2', 'COSINE' (case-sensitive), 'cosine similarity', or a metric copied from a different vector provider (e.g. 'inner_product', 'l2').","commonSituations":"Porting code from another vector database (pgvector/Pinecone use different metric names); uppercase metric from an enum or UI dropdown; typos like 'dotProduct' (camelCase not accepted).","solutions":["Use exactly one of: 'cosine', 'euclidean', 'dotproduct' (lowercase).","Map provider-specific metric names to these three values before sending.","If you don't need a specific metric, omit the metric field entirely to use the store default.","Check for stray whitespace or casing in the metric value (trim and lowercase)."],"exampleFix":"// before\nawait createIndex({ indexName: 'docs', dimension: 1536, metric: 'inner_product' });\n// after\nawait createIndex({ indexName: 'docs', dimension: 1536, metric: 'dotproduct' });","handlingStrategy":"validation","validationCode":"const METRICS = ['cosine', 'euclidean', 'dotproduct'] as const;\nfunction assertMetric(metric) {\n  if (metric !== undefined && !METRICS.includes(metric)) throw new TypeError(`metric must be one of ${METRICS.join(', ')}`);\n}","typeGuard":"function isVectorMetric(m) { return m === undefined || m === 'cosine' || m === 'euclidean' || m === 'dotproduct'; }","tryCatchPattern":"try {\n  await client.createIndex({ indexName, dimension, metric });\n} catch (e) {\n  if (e.status === 400 && /Invalid metric/.test(e.message)) {\n    console.error(`Unsupported metric '${metric}'; use cosine, euclidean, or dotproduct`);\n  }\n  throw e;\n}","preventionTips":["Normalize provider metric names (trim + lowercase, map 'dot'/'inner_product' to 'dotproduct') before sending.","Use a string-literal union type for metric so TS catches invalid values at compile time.","Omit metric when you want the store default.","Compare against the exact accepted strings; values are case-sensitive."],"tags":["http-400","validation","vector","rest-api"],"backgroundTag":"invalid-enum-value","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}