upstash/context7 · error · TypeError

retry.retries must be a non-negative integer

Error message

retry.retries must be a non-negative integer

What it means

createRetryPolicy validates the retry configuration before building the policy. If `config.retries` is set and is not a non-negative integer (e.g. negative, fractional, NaN), it throws a TypeError. This fail-fast guard prevents constructing a retry policy with meaningless retry counts.

Solutions

  1. Set retries to a non-negative integer (0 to disable, or omit the option to use the default of 5).
  2. Coerce/validate config values before passing: Number.isInteger(n) && n >= 0.
  3. If loading from env, parse with parseInt/Number and validate before constructing the client.

Example fix

// before
createRetryPolicy({ retries: Number(process.env.RETRIES) }); // NaN if unset
// after
const n = Number(process.env.RETRIES);
const policy = Number.isInteger(n) && n >= 0
  ? createRetryPolicy({ retries: n })
  : undefined; // default policy
Defensive patterns

Strategy: validation

Validate before calling

const n = config?.retries;
if (n !== undefined && (!Number.isInteger(n) || n < 0)) {
  throw new TypeError(`retry.retries must be a non-negative integer, got ${n}`);
}

Type guard

function isValidRetryCount(v: unknown): v is number {
  return typeof v === 'number' && Number.isInteger(v) && v >= 0;
}

Try / catch

try {
  policy = createRetryPolicy(config);
} catch (e) {
  if (e instanceof TypeError) {
    console.warn('Invalid retry config, falling back to defaults');
    policy = createRetryPolicy();
  } else throw e;
}

Prevention

When it happens

Trigger: Passing `{ retries: -1 }`, `{ retries: 2.5 }`, `{ retries: NaN }`, or a non-numeric value from parsed JSON/env into createRetryPolicy (directly or via the client constructor's retry option). Passing `false` is fine (disables retries, treated as 0).

Common situations: Loading retries from an environment variable or config file as a string like "-1" that got coerced, a user entering retries: 0.5 in a config, or a typo where retries defaults to undefined and some other code assigns a bad value.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of upstash/context7@4416fb855b (2026-09-16). Data as JSON: /api/errors/15a7f4cf1b9ad782. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/src/http/retry.ts:8

import type { RetryConfig, RetryPolicy } from "./types";

export const DEFAULT_RETRY_STATUSES = new Set([408, 425, 429, 500, 502, 503, 504]);

export function createRetryPolicy(config?: RetryConfig): RetryPolicy {
  const retries = config === false ? 0 : (config?.retries ?? 5);
  if (!Number.isInteger(retries) || retries < 0) {
    throw new TypeError("retry.retries must be a non-negative integer");
  }

  return {
    retries,
    backoff: config === false ? () => 0 : (config?.backoff ?? defaultBackoff),
    statuses: new Set(config === false ? [] : (config?.statuses ?? DEFAULT_RETRY_STATUSES)),
  };
}

export function isTransientStatus(status: number): boolean {
  return DEFAULT_RETRY_STATUSES.has(status);
}

export function retryDelay(backoff: number, retryAfter?: number): number {
  return Math.max(backoff, retryAfter === undefined ? 0 : retryAfter * 1000);
}

function defaultBackoff(retryCount: number): number {

View on GitHub (pinned to 4416fb855b)