Skip to content

Handle OpenAIErrors created with 'str' json_body #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2023
Merged

Conversation

tot0
Copy link
Contributor

@tot0 tot0 commented Mar 29, 2023

api_requestor.py recently started respecting text/plain response Content-Types. 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 not throw KeyError and let the unparsed json_body + message bubble up in the OpenAIError.

`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.
Copy link
Collaborator

@hallacy hallacy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@hallacy hallacy merged commit 931b4d2 into openai:main Mar 29, 2023
@tot0
Copy link
Contributor Author

tot0 commented Mar 29, 2023

Cheers @hallacy!
Any idea when next patch release might be?

@hallacy
Copy link
Collaborator

hallacy commented Apr 4, 2023

0.27.3 just got released today!

davedittrich pushed a commit to davedittrich/openai-python that referenced this pull request Nov 14, 2023
`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.
cgayapr pushed a commit to cgayapr/openai-python that referenced this pull request Dec 14, 2024
`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.
safa0 pushed a commit to safa0/openai-agents-python that referenced this pull request Apr 27, 2025
Towards openai#345

## Summary:
Using a `dict` or `Mapping` isn't strict-mode compliant. But we were
checking for the literal `True` whereas the value can also be an array,
for example. Fix that.

## Test Plan:

Unit tests
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.

2 participants