mastra-ai/mastra · error · HTTPException

Invalid request query. indexName and queryVector array are r

Error message

Invalid request query. indexName and queryVector array are required.

What it means

This HTTP 400 error is thrown by the queryVectors handler when the request lacks an indexName or a queryVector, or when queryVector is present but is not an array. The handler validates these two required fields before calling the vector store's query. It prevents confusing downstream errors like 'cannot read length of undefined' inside the store adapter.

Source

Thrown at packages/server/src/server/handlers/vector.ts:123

    return { success: true };
  } catch (error) {
    return handleError(error, 'Error creating index');
  }
}

// Query vectors
export async function queryVectors({
  mastra,
  vectorName,
  indexName,
  queryVector,
  topK,
  filter,
  includeVector,
}: Pick<VectorContext, 'mastra' | 'vectorName'> & QueryRequest) {
  try {
    if (!indexName || !queryVector || !Array.isArray(queryVector)) {
      throw new HTTPException(400, { message: 'Invalid request query. indexName and queryVector array are required.' });
    }

    const vector = getVector(mastra, vectorName);
    const results: QueryResult[] = await vector.query({ indexName, queryVector, topK, filter, includeVector });
    return results;
  } catch (error) {
    return handleError(error, 'Error querying vectors');
  }
}

// List indexes
export async function listIndexes({ mastra, vectorName }: Pick<VectorContext, 'mastra' | 'vectorName'>) {
  try {
    const vector = getVector(mastra, vectorName);

    const indexes = await vector.listIndexes();
    return indexes.filter(Boolean);
  } catch (error) {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Include indexName in the request body.
  2. Pass queryVector as a plain JSON array of numbers, e.g. [0.1, 0.2, ...].
  3. Confirm the embedding generation step succeeded and actually returned an array before querying.
  4. Match the embedding dimension to the index dimension to avoid the next error you'd hit.

Example fix

// before
const results = await query({ indexName: 'docs', queryVector: embedding?.values });
// after
if (!Array.isArray(embedding?.values)) throw new Error('embedding not ready');
const results = await query({ indexName: 'docs', queryVector: embedding.values });
Defensive patterns

Strategy: validation

Validate before calling

function assertQueryRequest(body) {
  const { indexName, queryVector } = body ?? {};
  if (typeof indexName !== 'string' || indexName.length === 0) throw new TypeError('indexName is required');
  if (!Array.isArray(queryVector) || queryVector.length === 0 || queryVector.some(n => typeof n !== 'number')) throw new TypeError('queryVector must be a non-empty array of numbers');
  return body;
}

Type guard

function isNumberArray(v) { return Array.isArray(v) && v.length > 0 && v.every(n => typeof n === 'number' && Number.isFinite(n)); }

Try / catch

try {
  return await client.query({ indexName, queryVector, topK });
} catch (e) {
  if (e.status === 400 && /Invalid request query/.test(e.message)) {
    console.error('query rejected — check indexName and that queryVector is a number[]');
  }
  throw e;
}

Prevention

When it happens

Trigger: POSTing to the query route with a body missing indexName; omitting queryVector; sending queryVector as an object or a comma-separated string instead of a JSON array; sending queryVector: null.

Common situations: Generating embeddings asynchronously and passing undefined because the embedding call failed silently; sending the embedding as {values: [...]} because code was copied from a different provider SDK; forgetting that indexName is a body field, not a URL path param on this route.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/bb212fa66003b702. Report an issue: GitHub.