Skip to content

fix: resolve DEEPGRAM_API_KEY when the client is constructed (closes #734) - #767

Open
Kayvan-Zahiri wants to merge 1 commit into
deepgram:mainfrom
Kayvan-Zahiri:fix/734-api-key-env-at-construction
Open

fix: resolve DEEPGRAM_API_KEY when the client is constructed (closes #734)#767
Kayvan-Zahiri wants to merge 1 commit into
deepgram:mainfrom
Kayvan-Zahiri:fix/734-api-key-env-at-construction

Conversation

@Kayvan-Zahiri

Copy link
Copy Markdown

Closes #734.

api_key defaults to os.getenv("DEEPGRAM_API_KEY") in the generated base client's signature (base_client.py:78, :254). Python evaluates default arguments once, at import, so the idiomatic layout fails:

from deepgram import DeepgramClient   # default captured as None here
load_dotenv()                         # key exists only now
DeepgramClient()                      # raises, though the variable is set

The fix re-reads the variable in DeepgramClient.__init__ / AsyncDeepgramClient.__init__ when no api_key was passed.

Why client.py and not the generated file. base_client.py is Fern output and a regen would drop the change. src/deepgram/client.py is in .fernignore, and AGENTS.md lists it under "Never unfreeze (permanently frozen)", so it is the durable place for this.

Behaviour that does not change, each covered by a test: an explicit api_key= still wins over the environment, access_token= still takes precedence and still gets the "token" placeholder, and with no key anywhere the same ApiError is raised.

Test added at tests/custom/test_api_key_env_resolution.py, registered in .fernignore and in the AGENTS.md frozen-files list. Verified non-vacuous: with the fix reverted, exactly one of the four fails. pytest tests/custom is 858 passed, 1 skipped; ruff check clean.

The generated base client takes os.getenv("DEEPGRAM_API_KEY") as a default
argument, so Python evaluates it once at import. The idiomatic layout of
imports at the top and load_dotenv() below them therefore fails: the default
is captured as None before the key exists, and DeepgramClient() raises even
though the variable is set by the time it is called.

client.py is permanently frozen in .fernignore, so the re-read goes there
rather than in the generated file.

Closes deepgram#734
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeepgramClient() reads DEEPGRAM_API_KEY at import time, not at instantiation

1 participant