#normalizeRetries silently coerces invalid maxRetries to 2 (e.g. -1 / 1.5 / 0)

Open Beginner friendly
#3,124 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
79/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript

Research direction

Start in src/client.ts at #normalizeRetries and inspect how client defaults and per request retry accounting use it. Read the existing retry validation tests, if present, and run the relevant client tests. Done means invalid values such as -1, 1.5, and "0" are rejected while undefined still defaults to 2 and 0 remains valid.

Written by the indexing model from the issue text.

Description

Summary

OpenAI silently coerces invalid maxRetries values to the default 2 instead of rejecting them. Callers that pass a typo / wrong type / non-integer (e.g. -1, 1.5, "0") get retries they did not ask for, with no throw and no warning.

Measured behavior

#normalizeRetries in src/client.ts:

#normalizeRetries(value: unknown): number {
  return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : 2;
}

That path is used both for the client default (this.maxRetries = this.#normalizeRetries(options.maxRetries)) and per-request retry accounting.

input result today what callers likely meant
0 0 (correct) disable retries
2 2 (correct) default
-1 2 invalid / “no retries” in some APIs
1.5 2 typo / bad config parse
"0" 2 stringified config that looks like “off”
undefined 2 default — fine

So "0" and -1 both quietly enable two retries.

Comparison

Sibling SDKs fail closed on the same class of input:

  • @ai-sdk/* / Vercel AI: prepareRetries validates the parameter and throws on invalid values
  • @anthropic-ai/sdk: validatePositiveInteger('maxRetries', …) rejects non-integers

Expected

Invalid maxRetries should throw (or at least warn) at client construction / request time, matching the documented contract that maxRetries is a non-negative integer configuration knob — not a soft hint that falls back to 2.

Suggested fix

Reject anything that is not a safe non-negative integer (same shape as Anthropic’s validatePositiveInteger), and keep undefined → default 2.

Happy to open a PR with a small unit test matrix for the cases above if maintainers want that direction.

Dominant language
TypeScript
Stars
11.3k
Forks
1.6k
Avg merge
12h 40m
Merged PRs (30d)
204

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from openai/openai-node

All issues in openai/openai-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.