diff --git a/src/openai/_client.py b/src/openai/_client.py index c784694f20..fa792c9479 100644 --- a/src/openai/_client.py +++ b/src/openai/_client.py @@ -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 @@ -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: @@ -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: diff --git a/src/openai/_utils/__init__.py b/src/openai/_utils/__init__.py index bd01c088dc..d98b656e6b 100644 --- a/src/openai/_utils/__init__.py +++ b/src/openai/_utils/__init__.py @@ -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, diff --git a/src/openai/_utils/_utils.py b/src/openai/_utils/_utils.py index d6734e6b8f..7f83cf5b87 100644 --- a/src/openai/_utils/_utils.py +++ b/src/openai/_utils/_utils.py @@ -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 diff --git a/src/openai/lib/azure.py b/src/openai/lib/azure.py index f857d76e51..ad98152686 100644 --- a/src/openai/lib/azure.py +++ b/src/openai/lib/azure.py @@ -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 @@ -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: @@ -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: