Skip to content

Environment API key is captured at import time in sync and async clients #33

Open

Description

@Kayvan-Zahiri

The generated Speechify and AsyncSpeechify constructors evaluate os.getenv("SPEECHIFY_API_KEY") in their parameter defaults at module import time. Consequently:

  1. Import the SDK with the variable unset, then load it with dotenv/a secret manager: constructing either client without an explicit token still raises ApiError.
  2. Import with key A, update the environment to key B, then construct a client: its HTTP Authorization header still uses key A.
  3. Import with a key, remove it, then construct a new client: the deleted import-time key is still used.

Reproduction (dummy values only):

import os
os.environ.pop("SPEECHIFY_API_KEY", None)
from speechify import Speechify
os.environ["SPEECHIFY_API_KEY"] = "dummy-late-key"
Speechify()  # ApiError: token missing

The official SDK docs state that clients automatically read SPEECHIFY_API_KEY: https://docs.speechify.ai/build/guides/get-started/official-sdks . Resolve it in each constructor when token is None, preserving explicit strings and token callbacks.

I tested a local proof of concept with 12 offline sync/async public-client cases: late, replaced, removed and absent environment keys plus explicit-string/callable controls. Six fail on the current source; all pass after moving environment lookup into the constructors, and the full suite passes (78 passed, four skipped). Request headers are checked with httpx.MockTransport; no real keys, live API calls, or audio are involved.

This is generated code. Per the README and AGENTS.md, any SDK patch here is a proof of concept for a generator/source-definition fix, not a durable merge-ready change. I can link the tested patch for that discussion. Developed with AI assistance and verified locally.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions