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

Throw exception if API key ENV vars are empty

Open iloveitaly opened this issue 10 months ago • 2 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

If you use an ENV var for setting the API key, and the ENV happens to be empty, no exception is thrown until a call is made and the exception is ambiguous (i.e., not clear that the token is empty).

To Reproduce

export OPENAI_API_KEY=""

and use the completions API

Code snippets


OS

15.3

Python version

v3.13.1

Library version

1.61.0

iloveitaly avatar Feb 05 '25 12:02 iloveitaly

Hi @iloveitaly, thanks for raising this. From what I’ve seen the API itself eventually throws an error when the API key ENV variable is empty. That said, I'll set up a PR to catch this earlier and provide a clearer message. Let's see what the OpenAI team has to say. Thanks for your patience and input!

prdai avatar Feb 12 '25 05:02 prdai

@Programmer-RD-AI it does, but the error is more obscure and you don't end up getting an error until after the code is run, which degrades the DX.

iloveitaly avatar Feb 12 '25 16:02 iloveitaly

Thia is somewhat solved now as we won't try and use the empty API key in the auth header which is what will have caused the cryptic error message, how you'll just get a standard authentication error from the API which makes things more clear.

RobertCraigie avatar Jul 07 '25 14:07 RobertCraigie