Hi there! I've made some improvements to how API keys are handled for… #1060
+245
−42
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
… different providers in your project.
This update takes care of an issue where the command-line interface wasn't correctly managing API keys for providers other than OpenAI.
Here's a summary of what I did:
Prioritized Provider-Specific Keys: I adjusted the code in
codex-cli/src/cli.tsx
to first try and get the API key using the specific provider's name. This means that if you've set up an environment variable like<PROVIDER>_API_KEY
or configured a key for a specific provider inconfig.json
, that will be used before any logic specific to OpenAI kicks in.Conditional OpenAI Logic: The existing way of getting OpenAI API keys (by checking
~/.codex/auth.json
or asking you for it) will now only happen if the provider you've chosen is 'openai' and I couldn't find a key for it through its direct environment variable or configuration.Corrected Fallback in
config.ts
: I've updatedutils/config.ts
so it no longer mistakenly uses theOPENAI_API_KEY
environment variable if a key for a different, specified provider isn't found. The OpenAI key will now only be considered as a fallback if the provider you originally asked for was 'openai'.Improved Error Messaging: I've refined the error message in
cli.tsx
that appears when an API key is missing. It will now suggest the correct environment variable name based on the provider you're using (for example,TEST_API_KEY
if you're using--provider test
, or a configuredenvKey
).Comprehensive Tests: I've added a new set of tests in
codex-cli/tests/config.test.tsx
to cover various scenarios for getting API keys. This includes custom providers, known providers from the default list, interaction withconfig.json
, and the order in which keys are checked. These tests help ensure the corrected logic works as expected and will prevent issues in the future.These changes should ensure that your project correctly uses API keys specified through provider-specific environment variables or configurations, and doesn't incorrectly prompt for or use OpenAI keys when other providers are active.