Closed jasonkaedingrhino closed 6 hours ago
In checking the stack trace, I found that there is already a method to "validate headers" that.... does nothing. That would seem to be the place to check the API key?
def _validate_headers(
self,
headers: Headers, # noqa: ARG002
custom_headers: Headers, # noqa: ARG002
) -> None:
"""Validate the given default headers and custom headers.
Does nothing by default.
"""
return
The ._validate_headers()
function is intended for internal SDK level validation, e.g. for missing headers, so I don't think we want to enforce any encoding specific behaviour here.
I opened an issue with httpx to improve the error message for invalid headers so I'm going to close this in favour of that.
Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
Referring to this issue
When the API key is accidentally set with invalid characters, an error occurs within the HTTP client when openai library attempts to set headers.
While eventually one might deduce the problem from the stack trace, having some sort of upstream "check" and more informative error about the API key would seem to help users understand the error. This caused us to spend multiple days trying to find the source of the problem assuming the error was in the message content instead of in the API key.
To Reproduce
UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-34: ordinal not in range(128)
Code snippets
OS
any
Python version
3.11
Library version
openai v1.44.0