openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

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

Open ShenweiFFF opened this issue 3 weeks ago • 3 comments

## 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.

ShenweiFFF avatar Nov 07 '26 04:11 ShenweiFFF

Would you accept a change that makes maxRetries validation strict, throwing on negative, non-integer or non-finite values instead of falling back to the default? If yes, I'm happy to send a PR with tests for each case. If the current fallback is intentional, a short doc note may be enough instead.

yeegz avatar Nov 17 '26 03:11 yeegz

The direction in the issue is the one I want: throw unless maxRetries is a safe non-negative integer, and keep undefined as the default 2. I'll open the PR.

ShenweiFFF avatar Nov 17 '26 04:11 ShenweiFFF

Opened a fix branch for this: https://github.com/openai/openai-node/compare/main...umeraamir69:openai-node:fix/reject-invalid-max-retries?expand=1

It rejects invalid maxRetries (non-integer / negative / non-number) instead of silently coercing to 2, with unit coverage in tests/client-behavior.test.ts. Happy to adjust if you'd rather warn than throw.

umeraamir69 avatar Nov 19 '26 16:11 umeraamir69