Codex does not respect API keys specified through environment variables for other vendors
What version of Codex is running?
0.1.2505172129
Which model were you using?
N/A
What platform is your computer?
Darwin 23.6.0 arm64 arm
What steps can reproduce the bug?
Case 1: codex should not ask for API key when using a non-openai provider
After installing codex, without running it at all or after deleting ~/.codex
TEST_BASE_URL=http://127.0.0.1:12345 TEST_API_KEY=test codex --provider test --model test/test
Case 2: codex should use the provider-specific API_KEY environment variable instead of OPENAI_API_KEY
First, run the following
socat TCP-LISTEN:12345,fork,reuseport -
Then
TEST_BASE_URL=http://127.0.0.1:12345 TEST_API_KEY=right OPENAI_API_KEY=wrong codex --provider test --model test/test
Enter a prompt after codex starts
What is the expected behavior?
Case 1: codex starts
Case 2: socat prints a request with Authorization: Bearer right
What do you see instead?
Case 1:
Sign in with ChatGPT to generate an API key or paste one you already have.
[use arrows to move, enter to select]
❯ Sign in with ChatGPT
Paste an API key (or set as OPENAI_API_KEY)
Case 2:
socat prints
...
POST /chat/completions HTTP/1.1
...
authorization: Bearer wrong
In other words: codex uses OPENAI_API_KEY for the custom provider, even though it asks for the provider-specific <provider>_API_KEY environment variable.
Additional information
Also, if you run the above tests with a ~/.codex/auth.json file present containing an API key, the api key from that file is used, despite specifying OPENAI_API_KEY, and TEST_API_KEY.
3 Comments
I noticed this today and saw this issue already opened. This looks like a regression introduced with #963 (c6e08ad) which should likely only run the added sign in workflow if the provider is "openai". @fouad-openai
See https://github.com/openai/codex/issues/987
Thank you for the detailed feedback! Please try again on the latest version of Codex CLI. If the issue persists, we'd appreciate it if you could report it or contribute to the
codex-rs/implementation. We're primarily focused on the native/Rust implementation of Codex CLI.