From 6f27b551273c60a379bc03742b7def48cf85cebe Mon Sep 17 00:00:00 2001 From: Siraj637909 Date: Wed, 19 Aug 2026 17:21:07 +0530 Subject: [PATCH] fix: type APIStatusError.code as int | str | None (gh-3531) Fixes #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. --- src/openai/_exceptions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openai/_exceptions.py b/src/openai/_exceptions.py index 7a30e4a336..2bf8f4ae07 100644 --- a/src/openai/_exceptions.py +++ b/src/openai/_exceptions.py @@ -58,7 +58,7 @@ class APIError(OpenAIError): If there was no response associated with this error then it will be `None`. """ - code: Optional[str] = None + code: int | str | None = None param: Optional[str] = None type: Optional[str] @@ -202,4 +202,4 @@ def __init__(self, message: str, *, unsent_messages: list[str]) -> None: class WebSocketQueueFullError(OpenAIError): """Raised when the outgoing WebSocket message queue exceeds its byte-size limit.""" - pass + pass \ No newline at end of file