Skip to content

Commit 81a459b

Browse files
fix(api): implement verify_api_key utility for API key validation
1 parent 7193688 commit 81a459b

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

Diff for: src/openai/_client.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
ProxiesTypes,
2020
RequestOptions,
2121
)
22-
from ._utils import (
23-
is_given,
24-
is_mapping,
25-
get_async_library,
26-
)
22+
from ._utils import is_given, is_mapping, get_async_library, verify_api_key
2723
from ._version import __version__
2824
from .resources import files, images, models, batches, embeddings, completions, moderations
2925
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
@@ -104,8 +100,8 @@ def __init__(
104100
- `organization` from `OPENAI_ORG_ID`
105101
- `project` from `OPENAI_PROJECT_ID`
106102
"""
107-
if api_key is None:
108-
api_key = os.environ.get("OPENAI_API_KEY")
103+
api_key = verify_api_key(api_key, "OPENAI_API_KEY")
104+
109105
if api_key is None:
110106
raise OpenAIError(
111107
"The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable"
@@ -331,8 +327,7 @@ def __init__(
331327
- `organization` from `OPENAI_ORG_ID`
332328
- `project` from `OPENAI_PROJECT_ID`
333329
"""
334-
if api_key is None:
335-
api_key = os.environ.get("OPENAI_API_KEY")
330+
api_key = api_key = verify_api_key(api_key, "OPENAI_API_KEY")
336331
if api_key is None:
337332
raise OpenAIError(
338333
"The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable"

Diff for: src/openai/_utils/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
maybe_coerce_boolean as maybe_coerce_boolean,
3535
maybe_coerce_integer as maybe_coerce_integer,
3636
is_async_azure_client as is_async_azure_client,
37+
verify_api_key as verify_api_key,
3738
)
3839
from ._typing import (
3940
is_list_type as is_list_type,

Diff for: src/openai/_utils/_utils.py

+7
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,10 @@ def is_async_azure_client(client: object) -> TypeGuard[AsyncAzureOpenAI]:
428428
from ..lib.azure import AsyncAzureOpenAI
429429

430430
return isinstance(client, AsyncAzureOpenAI)
431+
432+
433+
def verify_api_key(api_key: str, env_name: str) -> str | None:
434+
if api_key is None or api_key.strip() == "":
435+
env_api_key = os.environ.get(env_name)
436+
api_key = env_api_key if env_api_key and env_api_key.strip() != "" else None
437+
return api_key

Diff for: src/openai/lib/azure.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import httpx
99

1010
from .._types import NOT_GIVEN, Omit, Query, Timeout, NotGiven
11-
from .._utils import is_given, is_mapping
11+
from .._utils import is_given, is_mapping, verify_api_key
1212
from .._client import OpenAI, AsyncOpenAI
1313
from .._compat import model_copy
1414
from .._models import FinalRequestOptions
@@ -163,8 +163,7 @@ def __init__(
163163
azure_deployment: A model deployment, if given sets the base client URL to include `/deployments/{azure_deployment}`.
164164
Note: this means you won't be able to use non-deployment endpoints. Not supported with Assistants APIs.
165165
"""
166-
if api_key is None:
167-
api_key = os.environ.get("AZURE_OPENAI_API_KEY")
166+
api_key = verify_api_key(api_key, "AZURE_OPENAI_API_KEY")
168167

169168
if azure_ad_token is None:
170169
azure_ad_token = os.environ.get("AZURE_OPENAI_AD_TOKEN")
@@ -425,8 +424,7 @@ def __init__(
425424
azure_deployment: A model deployment, if given sets the base client URL to include `/deployments/{azure_deployment}`.
426425
Note: this means you won't be able to use non-deployment endpoints. Not supported with Assistants APIs.
427426
"""
428-
if api_key is None:
429-
api_key = os.environ.get("AZURE_OPENAI_API_KEY")
427+
api_key = verify_api_key(api_key, "AZURE_OPENAI_API_KEY")
430428

431429
if azure_ad_token is None:
432430
azure_ad_token = os.environ.get("AZURE_OPENAI_AD_TOKEN")

0 commit comments

Comments
 (0)