Skip to content

fix: normalize API error codes to strings - #3532

Open
Sanjays2402 wants to merge 1 commit into
openai:mainfrom
Sanjays2402:fix/api-error-code-type
Open

fix: normalize API error codes to strings#3532
Sanjays2402 wants to merge 1 commit into
openai:mainfrom
Sanjays2402:fix/api-error-code-type

Conversation

@Sanjays2402

Copy link
Copy Markdown
  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

Normalize non-null API error codes to strings so runtime behavior matches the public str | None annotation. A regression test covers numeric error payloads.

Additional context & links

Closes #3531

Numeric API error codes currently contradict the public string annotation. Normalize non-null values and cover the runtime behavior with a regression test.
Copilot AI review requested due to automatic review settings July 23, 2026 07:40
@Sanjays2402
Sanjays2402 requested a review from a team as a code owner July 23, 2026 07:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Normalizes APIError.code to always be a string (when present) so runtime behavior matches the public str | None type annotation and avoids downstream type surprises when servers return numeric error codes.

Changes:

  • Coerce non-None error.code values to str during APIError construction.
  • Add a regression test covering integer error codes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/openai/_exceptions.py Ensures APIError.code is normalized to str when present.
tests/test_client.py Adds a regression test asserting integer error codes become strings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@amirhosseinghanipour

Copy link
Copy Markdown

Fix looks correct. Good addition of test covering the exact repro from #3531. LGTM.

@jbeckwith-oai jbeckwith-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean at 00e6dafb7c1ff654a2917890f987069dcd82014e. Normalizing a non-null wire value at the APIError construction boundary makes runtime behavior match the public str | None contract without adding downstream branching or indirection, and the regression exercises the reported numeric-code case. I found no correctness or maintainability blockers.

Validation: the focused regression passed; changed-file Ruff lint and format checks passed; targeted Pyright passed; full mypy passed; and git diff --check passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

APIStatusError.code is typed Optional[str] but can be an int at runtime

4 participants