googleapis / googleapis/python-genai
Skip environment variable check when api_key is explicitly passed
- Dominant language
- Python
- Stars
- 4k
- Forks
- 1k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 40
Description
**Is your feature request related to a problem?**
When initializing the `Client` with an explicit `api_key` argument (i.e., `genai.Client(api_key="...")`), the code still executes the logic to check and warn about the priority between environment variables like `GOOGLE_API_KEY` and `GEMINI_API_KEY`.
https://github.com/googleapis/python-genai/blob/4b5720a7fb1a75d850dd8a182e3d31a9f587e57f/google/genai/_api_client.py#L592-L595
https://github.com/googleapis/python-genai/blob/4b5720a7fb1a75d850dd8a182e3d31a9f587e57f/google/genai/_api_client.py#L95-L109
**Describe the solution you'd like**
If the `api_key` argument passed to the `Client` constructor is **not None**, the entire logic block responsible for resolving API keys from environment variables and logging related warnings (`get_env_api_key()` or similar implementation) should be **skipped entirely**.
**Describe alternatives you've considered**
Ignoring the warning: This is the current workaround, but it clutters logs for users who prefer to set environment variables for other tools/processes but explicitly provide the key for a specific script/client instance.
**Additional context**
No.
Contributor guide
Assessment
This issue has not been assessed yet.