Skip to content

fix(exceptions): support integer error codes in APIError validationfix(exceptions): support integer error codes in APIError validation - #3554

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

fix(exceptions): support integer error codes in APIError validationfix(exceptions): support integer error codes in APIError validation#3554
Rajeev91691 wants to merge 1 commit into
openai:mainfrom
Rajeev91691:fix/error-code-type-validation

Conversation

@Rajeev91691

Copy link
Copy Markdown

Summary

This PR fixes an issue where error responses containing integer/numeric error codes (e.g. {"error": {"code": 400}} instead of {"error": {"code": "invalid_request"}}) raise a runtime validation/type error because APIError.code and ErrorObject.code were strictly typed as Optional[str].

This is common when using various OpenAI-compatible gateways or proxy endpoints that return integer codes.

Key Changes

  • Widen type of APIError.code in src/openai/_exceptions.py to Optional[Union[str, int]] and update construct_type to allow both types.
    • Widen type of ErrorObject.code in src/openai/types/shared/error_object.py to Optional[Union[str, int]].

Widen APIError.code and ErrorObject.code types from Optional[str] to Optional[Union[str, int]] to prevent runtime validation failures when API error responses return numeric error codes. Add test to verify correct parsing of numeric error codes.
@Rajeev91691
Rajeev91691 requested a review from a team as a code owner July 30, 2026 17:21

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 295bdf163d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/openai/_exceptions.py

if is_dict(body):
self.code = cast(Any, construct_type(type_=Optional[str], value=body.get("code")))
self.code = cast(Any, construct_type(type_=Optional[Union[str, int]], value=body.get("code")))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve integer error codes under pydantic v1

Because this package still allows pydantic>=1.9.0,<3, environments pinned to pydantic 1.x will run this through v1 Union validation, which tries the str branch first and coerces a JSON integer like 400 to '400' instead of preserving it as an int. In those supported installs, integer API error codes still surface as strings, so callers comparing exc.code == 400 and the new test scenario fail; handle primitive ints explicitly or avoid the str-first Union validation for this field.

Useful? React with 👍 / 👎.

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

1 participant