chroma-core/chroma · error · ChromaValueError

Expected 'nResults' to be a number, but got ${typeof nResult

Error message

Expected 'nResults' to be a number, but got ${typeof nResults}

What it means

query()'s nResults is validated with typeof === 'number' (utils.ts:752-757) inside prepareQuery. A string '10', NaN, or any non-number throws ChromaValueError reporting the actual typeof in the message. No string-to-number coercion is performed, unlike lenient HTTP-layer APIs.

Source

Thrown at clients/new-js/packages/chromadb/src/utils.ts:754

          ", ",
        )}, but got ${item}`,
      );
    }

    if (exclude?.includes(item)) {
      throw new ChromaValueError(`${item} is not allowed for this operation`);
    }
  });
};

/**
 * Validates the number of results parameter for queries.
 * @param nResults - Number of results to validate
 * @throws ChromaValueError if nResults is not a positive number
 */
export const validateNResults = (nResults: number) => {
  if (typeof (nResults as any) !== "number") {
    throw new ChromaValueError(
      `Expected 'nResults' to be a number, but got ${typeof nResults}`,
    );
  }

  if (nResults <= 0) {
    throw new ChromaValueError("Number of requested results has to positive");
  }
};

export const parseConnectionPath = (path: string) => {
  try {
    const url = new URL(path);

    const ssl = url.protocol === "https:";
    const host = url.hostname;
    const port = url.port;

    return {

View on GitHub (pinned to aecdd12c8a)

Solutions

  1. Convert at the boundary: nResults: Number(raw)
  2. Guard non-finite values: Number.isFinite(n) ? n : 10
  3. Type the query options with the package's exported types so strings are rejected at compile time

Example fix

// before
await col.query({ queryTexts, nResults: process.env.TOP_K }); // string

// after
await col.query({ queryTexts, nResults: Number(process.env.TOP_K) });
Defensive patterns

Strategy: validation

Validate before calling

const nResults = Number(rawTopK);
if (!Number.isFinite(nResults)) throw new TypeError(`nResults must be a finite number, got ${rawTopK}`);
await col.query({ queryTexts, nResults });

Type guard

const isValidNResults = (v: unknown): v is number =>
  typeof v === 'number' && Number.isFinite(v) && v > 0;

Prevention

When it happens

Trigger: collection.query({ queryTexts: [...], nResults: '10' }) — typical when topK comes from process.env, a URL query parameter, or JSON config; nResults: NaN produced by Number(undefined).

Common situations: Reading topK from environment variables or request strings and passing it unconverted; default-parameter logic that yields undefined cast to any.

Related errors


AI-assisted analysis of chroma-core/chroma@aecdd12c8a (2026-08-16). Data as JSON: /api/errors/1d4c48ac261ee619. Report an issue: GitHub.