diff --git a/src/openai/_exceptions.py b/src/openai/_exceptions.py index 86f44b0e15..40adff6de2 100644 --- a/src/openai/_exceptions.py +++ b/src/openai/_exceptions.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Optional, cast +from typing import TYPE_CHECKING, Any, Optional, Union, cast from typing_extensions import Literal import httpx @@ -60,7 +60,7 @@ class APIError(OpenAIError): If there was no response associated with this error then it will be `None`. """ - code: Optional[str] = None + code: Optional[Union[str, int]] = None param: Optional[str] = None type: Optional[str] @@ -71,7 +71,8 @@ def __init__(self, message: str, request: httpx.Request, *, body: object | None) self.body = body if is_dict(body): - self.code = cast(Any, construct_type(type_=Optional[str], value=body.get("code"))) + code_value = body.get("code") + self.code = code_value if isinstance(code_value, (str, int)) else None self.param = cast(Any, construct_type(type_=Optional[str], value=body.get("param"))) self.type = cast(Any, construct_type(type_=str, value=body.get("type"))) else: