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 all 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
22 changes: 5 additions & 17 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, get_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,12 +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")
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"
)
api_key = get_api_key(api_key, "OPENAI_API_KEY")

self.api_key = api_key

if organization is None:
Expand Down Expand Up @@ -331,12 +323,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")
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"
)
api_key = api_key = get_api_key(api_key, "OPENAI_API_KEY")

self.api_key = api_key

if organization is None:
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,
get_api_key as get_api_key,
)
from ._typing import (
is_list_type as is_list_type,
Expand Down
11 changes: 11 additions & 0 deletions src/openai/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,14 @@ def is_async_azure_client(client: object) -> TypeGuard[AsyncAzureOpenAI]:
from ..lib.azure import AsyncAzureOpenAI

return isinstance(client, AsyncAzureOpenAI)


def get_api_key(api_key: str, env_name: str) -> str:
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
if api_key is None:
raise OpenAIError(
f"The api_key client option must be set either by passing api_key to the client or by setting the {env_name} environment variable"
)
return api_key
16 changes: 7 additions & 9 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, get_api_key
from .._client import OpenAI, AsyncOpenAI
from .._compat import model_copy
from .._models import FinalRequestOptions
Expand Down Expand Up @@ -163,15 +163,14 @@ 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 = get_api_key(api_key, "AZURE_OPENAI_API_KEY")

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

if api_key is None and azure_ad_token is None and azure_ad_token_provider is None:
if azure_ad_token is None and azure_ad_token_provider is None:
raise OpenAIError(
"Missing credentials. Please pass one of `api_key`, `azure_ad_token`, `azure_ad_token_provider`, or the `AZURE_OPENAI_API_KEY` or `AZURE_OPENAI_AD_TOKEN` environment variables."
"Missing credentials. Please provide either `azure_ad_token` or `azure_ad_token_provider`, or set the environment variable `AZURE_OPENAI_AD_TOKEN`."
)

if api_version is None:
Expand Down Expand Up @@ -425,15 +424,14 @@ 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 = get_api_key(api_key, "AZURE_OPENAI_API_KEY")

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

if api_key is None and azure_ad_token is None and azure_ad_token_provider is None:
if azure_ad_token is None and azure_ad_token_provider is None:
raise OpenAIError(
"Missing credentials. Please pass one of `api_key`, `azure_ad_token`, `azure_ad_token_provider`, or the `AZURE_OPENAI_API_KEY` or `AZURE_OPENAI_AD_TOKEN` environment variables."
"Missing credentials. Please provide either `azure_ad_token` or `azure_ad_token_provider`, or set the environment variable `AZURE_OPENAI_AD_TOKEN`."
)

if api_version is None:
Expand Down