Throw exception if API key ENV vars are empty
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
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!
@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.
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.