Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/openai/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand All @@ -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:
Expand Down