openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
23.09k stars 3.25k forks source link

Invalid API key environment variables lead to confusing encoding errors #1793

Closed jasonkaedingrhino closed 6 hours ago

jasonkaedingrhino commented 1 month ago

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

  1. Set the API key to a value with non-ASCII characters
  2. Execute a chat completion with the client
  3. Receive encoding error UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-34: ordinal not in range(128)

Code snippets

from openai import OpenAI

#######################
#### CONFIGURATION ####
#######################

configs = dict(
    api_key = 'здравейздравейздравейздравей',
)

client = OpenAI(**configs)

######################
###### RUN TEST ######
######################

message = 'Hello!'
completion = client.chat.completions.create(
    model='gpt-4-turbo-preview',
    messages=[
        {
            'role': 'user',
            'content': message
        }
    ]    
)

print(completion.choices[0].message.content)

OS

any

Python version

3.11

Library version

openai v1.44.0

jasonkaedingrhino commented 1 month 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
RobertCraigie commented 6 hours ago

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.