openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Invalid API key environment variables lead to confusing encoding errors

Open jasonkaedingrhino opened this issue 1 year ago • 1 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [X] This is an issue with the Python library

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 avatar Oct 16 '24 01:10 jasonkaedingrhino

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

jasonkaedingrhino avatar Oct 16 '24 01:10 jasonkaedingrhino

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.

RobertCraigie avatar Nov 22 '24 12:11 RobertCraigie