Skip to content

fix: type APIStatusError.code as int | str | None (gh-3531) - #3652

Open
Siraj637909 wants to merge 1 commit into
openai:mainfrom
Siraj637909:fix-3531-APIStatusError-code-int-or-str
Open

fix: type APIStatusError.code as int | str | None (gh-3531)#3652
Siraj637909 wants to merge 1 commit into
openai:mainfrom
Siraj637909:fix-3531-APIStatusError-code-int-or-str

Conversation

@Siraj637909

@Siraj637909 Siraj637909 commented Aug 19, 2026

Copy link
Copy Markdown

Hi! I've opened a fix for this issue in PR #3652.

The problem: APIStatusError.code is typed Optional[str] but at runtime can hold an int (e.g. {"error": {"code": 404}}). The construct_type call returns the value unchanged when it doesn't match the target type, and cast(Any, ...) suppresses the type checker, so the annotation is misleading. Downstream code that trusts it and calls exc.code.strip() crashes with AttributeError.

The fix: Changed the annotation to int | str | None in src/openai/_exceptions.py:61 to match actual runtime behavior.

PR: #3652

Fixes openai#3531: APIStatusError.code was typed Optional[str] but at runtime
can hold an int (e.g., {'error': {'code': 404}}). Downstream code that
trusts the annotation and calls exc.code.strip() crashes with AttributeError.
This change makes the annotation match actual behavior.
@Siraj637909
Siraj637909 requested a review from a team as a code owner August 19, 2026 11:51
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.

1 participant