You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DeepgramClient() reads DEEPGRAM_API_KEY at import time, not at instantiation #734
api_key defaults to os.getenv("DEEPGRAM_API_KEY") directly in the __init__ signature, so Python evaluates it once, when the module is imported, not when the client is constructed. The idiomatic pattern (imports at the top of a file, load_dotenv() below them) therefore fails: the default is captured as None before the key is set, and DeepgramClient() raises even though DEEPGRAM_API_KEY is present by the time you instantiate.
api_key: typing.Optional[str] =os.getenv("DEEPGRAM_API_KEY"), # evaluated at import
...
ifapi_keyisNone:
raiseApiError(body="The client must be instantiated be either passing in api_key or setting DEEPGRAM_API_KEY")
os.getenv(...) as a default argument runs once, at function-definition (import) time, not per call.
Reproduction
importosos.environ.pop("DEEPGRAM_API_KEY", None) # not set in the shell at process startfromdeepgramimportDeepgramClient# default captured as None HEREos.environ["DEEPGRAM_API_KEY"] ="dummy-key"# set afterward (what load_dotenv() does)DeepgramClient() # raises, despite the var being set
Output on 7.3.1:
set at instantiation: True
DeepgramClient() -> ApiError: ... The client must be instantiated be either passing in api_key or setting DEEPGRAM_API_KEY
DeepgramClient(api_key=...) -> constructed OK
Impact
The documented zero-config pattern (DeepgramClient() reading DEEPGRAM_API_KEY) only works if the variable is in the OS environment before the deepgram import runs. With python-dotenv (very common), load_dotenv() runs after the import, so the key is invisible. The error message ("set DEEPGRAM_API_KEY") is misleading because the variable is already set, which sends you to debug the wrong thing.
Suggested fix
Default the parameter to None and resolve the env var inside __init__ so it's read at call time:
def__init__(self, *, api_key: typing.Optional[str] =None, ...):
ifapi_keyisNone:
api_key=os.getenv("DEEPGRAM_API_KEY")
ifapi_keyisNone:
raiseApiError(body="The client must be instantiated by either passing in api_key or setting DEEPGRAM_API_KEY")
(Also worth fixing the grammar in the error string: "must be instantiated be either" -> "by either".) Since base_client.py is Fern-generated, the change belongs in the generator template.
Workaround
Pass the key explicitly, DeepgramClient(api_key=os.getenv("DEEPGRAM_API_KEY")), or call load_dotenv() before importing deepgram.
Activity
fix: preserve explicit API key semantics (#778)
chore(main): release 7.8.1 (#777)
fix: resolve DEEPGRAM_API_KEY when the client is constructed (closes d…