Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): implement get_api_key utility for API key validation #2114

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/openai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
ProxiesTypes,
RequestOptions,
)
from ._utils import (
is_given,
is_mapping,
get_async_library,
)
from ._utils import is_given, is_mapping, get_async_library, verify_api_key
from ._version import __version__
from .resources import files, images, models, batches, embeddings, completions, moderations
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
Expand Down Expand Up @@ -104,8 +100,8 @@ def __init__(
- `organization` from `OPENAI_ORG_ID`
- `project` from `OPENAI_PROJECT_ID`
"""
if api_key is None:
api_key = os.environ.get("OPENAI_API_KEY")
api_key = verify_api_key(api_key, "OPENAI_API_KEY")

if api_key is None:
raise OpenAIError(
"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"
Expand Down Expand Up @@ -331,8 +327,7 @@ def __init__(
- `organization` from `OPENAI_ORG_ID`
- `project` from `OPENAI_PROJECT_ID`
"""
if api_key is None:
api_key = os.environ.get("OPENAI_API_KEY")
api_key = api_key = verify_api_key(api_key, "OPENAI_API_KEY")
if api_key is None:
raise OpenAIError(
"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"
Expand Down
1 change: 1 addition & 0 deletions src/openai/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
maybe_coerce_boolean as maybe_coerce_boolean,
maybe_coerce_integer as maybe_coerce_integer,
is_async_azure_client as is_async_azure_client,
verify_api_key as verify_api_key,
)
from ._typing import (
is_list_type as is_list_type,
Expand Down
7 changes: 7 additions & 0 deletions src/openai/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,10 @@ def is_async_azure_client(client: object) -> TypeGuard[AsyncAzureOpenAI]:
from ..lib.azure import AsyncAzureOpenAI

return isinstance(client, AsyncAzureOpenAI)


def verify_api_key(api_key: str, env_name: str) -> str | None:
if api_key is None or api_key.strip() == "":
env_api_key = os.environ.get(env_name)
api_key = env_api_key if env_api_key and env_api_key.strip() != "" else None
return api_key
8 changes: 3 additions & 5 deletions src/openai/lib/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import httpx

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

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

if azure_ad_token is None:
azure_ad_token = os.environ.get("AZURE_OPENAI_AD_TOKEN")
Expand Down