Skip to content

Commit 931b4d2

Browse files
authored
Handle OpenAIErrors created with 'str' json_body (#356)
`api_requestor.py` recently started respecting `text/plain` response `Content-Type`s. Azure OpenAI can sometimes return errors with that Content-Type that are still JSON strings with error info, which can break OpenAIErrors default behavior of checking for `error` being `in` the supplied `json_body`. Classic Python, `in` works for `dict` or `str` but `json_body["error"]` will only work if `json_body` is a `dict`. This is the minimal fix to now throw KeyError and let the unparsed json_body + message bubble up in the OpenAIError.
1 parent 237448d commit 931b4d2

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

openai/error.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __repr__(self):
5858
def construct_error_object(self):
5959
if (
6060
self.json_body is None
61+
or not isinstance(self.json_body, dict)
6162
or "error" not in self.json_body
6263
or not isinstance(self.json_body["error"], dict)
6364
):

0 commit comments

Comments
 (0)