Skip to content

Commit 6cea6b0

Browse files
feat: fall back to WHISPER_API_KEY for whisper transcription
When neither WDOC_WHISPER_API_KEY nor OPENAI_API_KEY is set, resolve the whisper API key from a WHISPER_API_KEY environment variable if present. Resolution order: WDOC_WHISPER_API_KEY > OPENAI_API_KEY > WHISPER_API_KEY. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 79cacbd commit 6cea6b0

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

wdoc-skill/REFERENCE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ These load multiple documents and can combine different sources:
327327
| Variable | Default | Description |
328328
|----------|---------|-------------|
329329
| `WDOC_WHISPER_ENDPOINT` | `""` | Custom Whisper API endpoint |
330-
| `WDOC_WHISPER_API_KEY` | `""` | Custom Whisper API key |
330+
| `WDOC_WHISPER_API_KEY` | `""` | Custom Whisper API key. When empty, falls back to `OPENAI_API_KEY`, then `WHISPER_API_KEY` |
331331
| `WDOC_WHISPER_MODEL` | `whisper-1` | Whisper model name |
332332
| `WDOC_WHISPER_PARALLEL_SPLITS` | `True` | Parallelize split audio transcription |
333333

wdoc/docs/help.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,8 @@
947947
* `WDOC_WHISPER_API_KEY`, default: `""`
948948
* If provided, sets a custom API key for Whisper transcription services. This is useful when using alternative
949949
Whisper-compatible services that require their own authentication.
950-
* When empty, uses the default OPENAI_API_KEY environment variable.
950+
* When empty, falls back to the `OPENAI_API_KEY` environment variable, and if that is also unset, to the
951+
`WHISPER_API_KEY` environment variable. The resolution order is `WDOC_WHISPER_API_KEY` > `OPENAI_API_KEY` > `WHISPER_API_KEY`.
951952

952953
* `WDOC_WHISPER_MODEL`, default: `"whisper-1"`
953954
* Specifies which Whisper model to use for audio transcription. This can be any model supported by your Whisper endpoint.

wdoc/utils/loaders/shared_audio.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,30 @@ def transcribe_audio_whisper(
210210
)
211211
else:
212212
assert (
213-
"OPENAI_API_KEY" in os.environ
214-
and not os.environ["OPENAI_API_KEY"]
215-
== "REDACTED_BECAUSE_WDOC_IN_PRIVATE_MODE"
216-
) or (
217-
env.WDOC_WHISPER_API_KEY
218-
and not env.WDOC_WHISPER_API_KEY == "REDACTED_BECAUSE_WDOC_IN_PRIVATE_MODE"
219-
), "No environment variable OPENAI_API_KEY nor WDOC_WHISPER_API_KEY found"
213+
(
214+
"OPENAI_API_KEY" in os.environ
215+
and not os.environ["OPENAI_API_KEY"]
216+
== "REDACTED_BECAUSE_WDOC_IN_PRIVATE_MODE"
217+
)
218+
or (
219+
env.WDOC_WHISPER_API_KEY
220+
and not env.WDOC_WHISPER_API_KEY
221+
== "REDACTED_BECAUSE_WDOC_IN_PRIVATE_MODE"
222+
)
223+
or (
224+
os.environ.get("WHISPER_API_KEY")
225+
and not os.environ["WHISPER_API_KEY"]
226+
== "REDACTED_BECAUSE_WDOC_IN_PRIVATE_MODE"
227+
)
228+
), (
229+
"No environment variable OPENAI_API_KEY, WDOC_WHISPER_API_KEY nor WHISPER_API_KEY found"
230+
)
231+
232+
# Resolve the whisper API key. WDOC_WHISPER_API_KEY takes precedence; if it
233+
# is unset and OPENAI_API_KEY is also unset, fall back to WHISPER_API_KEY.
234+
whisper_api_key = env.WDOC_WHISPER_API_KEY
235+
if not whisper_api_key and not os.environ.get("OPENAI_API_KEY"):
236+
whisper_api_key = os.environ.get("WHISPER_API_KEY", "")
220237

221238
try:
222239
t1 = time.time()
@@ -238,8 +255,8 @@ def transcribe_audio_whisper(
238255
f"Using custom whisper endpoint: {env.WDOC_WHISPER_ENDPOINT}"
239256
)
240257

241-
if env.WDOC_WHISPER_API_KEY:
242-
transcription_kwargs["api_key"] = env.WDOC_WHISPER_API_KEY
258+
if whisper_api_key:
259+
transcription_kwargs["api_key"] = whisper_api_key
243260
logger.debug("Using custom whisper API key")
244261

245262
try:
@@ -270,8 +287,8 @@ def transcribe_audio_whisper(
270287
data["language"] = language
271288

272289
headers = {}
273-
if env.WDOC_WHISPER_API_KEY:
274-
headers["Authorization"] = f"Bearer {env.WDOC_WHISPER_API_KEY}"
290+
if whisper_api_key:
291+
headers["Authorization"] = f"Bearer {whisper_api_key}"
275292

276293
# Make the request
277294
endpoint_url = (

0 commit comments

Comments
 (0)