Skip to content

fix: resolve api_key from env var with runtime assertion#763

Closed
kraenhansen wants to merge 4 commits intomainfrom
kh/fix-wrapper-optional-api-key
Closed

fix: resolve api_key from env var with runtime assertion#763
kraenhansen wants to merge 4 commits intomainfrom
kh/fix-wrapper-optional-api-key

Conversation

@kraenhansen
Copy link
Copy Markdown
Member

@kraenhansen kraenhansen commented Apr 8, 2026

Summary

  • Prepares the wrapper client for SDK regeneration (🌿 Fern Regeneration -- April 7, 2026 #761) which makes api_key required in the generated base client
  • Adds a _resolve_api_key() helper that resolves from the parameter or ELEVENLABS_API_KEY env var, raising a clear ValueError if neither is set
  • Only touches src/elevenlabs/client.py (in .fernignore) and tests/test_client_init.py — no generated code modified
  • Public API stays backwards-compatible: api_key remains Optional[str] on ElevenLabs and AsyncElevenLabs
  • Also fixes a pre-existing bug: the old api_key=os.getenv("ELEVENLABS_API_KEY") default was evaluated once at import time, so env var changes after import were silently ignored. The env var is now read fresh on every client construction.

Mirrors the approach from elevenlabs-js#368.

Test plan

  • ElevenLabs(api_key="key") — works as before
  • ElevenLabs() with ELEVENLABS_API_KEY env var set — resolves from env
  • ElevenLabs() without env var — raises clear ValueError
  • Unit tests for _resolve_api_key, ElevenLabs, and AsyncElevenLabs (11 tests)
  • Existing tests pass (webhooks, recursive models)
  • Verify compatibility after merging 🌿 Fern Regeneration -- April 7, 2026 #761 (regeneration makes api_key: str required in base)

🤖 Generated with Claude Code


Note

Medium Risk
Changes how ElevenLabs/AsyncElevenLabs obtain and validate API keys, which can break callers that relied on empty/missing keys or import-time env evaluation. Risk is limited to client construction/auth header setup and is covered by new unit tests.

Overview
API key handling is now resolved at runtime. ElevenLabs and AsyncElevenLabs no longer default api_key from os.getenv(...) at import time; instead they call a new internal _resolve_api_key() on construction to use the passed value or fall back to ELEVENLABS_API_KEY.

Missing/empty keys now fail fast. _resolve_api_key() raises a clear ValueError when neither an explicit key nor a non-empty env var is available (and treats "" as invalid), with new tests validating precedence and error cases; a convai import test was updated to stop passing an empty key.

Reviewed by Cursor Bugbot for commit 6709ca1. Bugbot is set up for automated code reviews on this repo. Configure here.

The upcoming SDK regeneration (#761) makes api_key required in the
generated base client. This updates the wrapper to resolve api_key from
the parameter or ELEVENLABS_API_KEY env var, raising a clear error if
neither is set — keeping the public API backwards-compatible.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 18e76d2. Configure here.

Comment thread src/elevenlabs/client.py Outdated
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@kraenhansen kraenhansen requested a review from PaulAsjes April 8, 2026 10:14
kraenhansen and others added 2 commits April 8, 2026 12:16
…n by env var

Addresses review feedback: `api_key or os.getenv(...)` treated an
explicitly-passed empty string the same as None, silently falling
through to the env var. Now uses `is not None` so only an unset
parameter falls back.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This test only checks that the import/attribute chain works, not API
calls. Use a non-empty dummy key to satisfy the new validation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@kraenhansen
Copy link
Copy Markdown
Member Author

Closing this as obsolete with https://github.com/elevenlabs/elevenlabs-dx/pull/2097 merged.

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.

1 participant