Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/openai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,15 @@ def __init__(

self.workload_identity = workload_identity

_api_key_explicitly_provided = False
if workload_identity is not None:
self.api_key = WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER
self._api_key_provider = None
self._workload_identity_auth = WorkloadIdentityAuth(
workload_identity=workload_identity,
)
else:
_api_key_explicitly_provided = api_key is not None
if api_key is None:
api_key = os.environ.get("OPENAI_API_KEY")
if callable(api_key):
Expand All @@ -180,13 +182,16 @@ def __init__(
self._api_key_provider = None
self._workload_identity_auth = None

self._api_key_explicitly_provided = _api_key_explicitly_provided

if admin_api_key is None:
admin_api_key = os.environ.get("OPENAI_ADMIN_KEY")
self.admin_api_key = admin_api_key

if (
_enforce_credentials
and not self.api_key
and not _api_key_explicitly_provided
and self._api_key_provider is None
Comment on lines 191 to 195
and workload_identity is None
and self.admin_api_key is None
Expand Down Expand Up @@ -489,6 +494,9 @@ def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
if _has_header(headers, "Authorization") or _has_omitted_header(custom_headers, "Authorization"):
return

if self._api_key_explicitly_provided:
return

raise TypeError(
'"Could not resolve authentication method. Expected either api_key or admin_api_key to be set. Or for one of the `Authorization` or `Authorization` headers to be explicitly omitted"'
)
Expand Down Expand Up @@ -669,13 +677,15 @@ def __init__(

self.workload_identity = workload_identity

_api_key_explicitly_provided = False
if workload_identity is not None:
self.api_key = WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER
self._api_key_provider = None
self._workload_identity_auth = WorkloadIdentityAuth(
workload_identity=workload_identity,
)
else:
_api_key_explicitly_provided = api_key is not None
if api_key is None:
api_key = os.environ.get("OPENAI_API_KEY")
if callable(api_key):
Expand All @@ -686,13 +696,16 @@ def __init__(
self._api_key_provider = None
self._workload_identity_auth = None

self._api_key_explicitly_provided = _api_key_explicitly_provided

if admin_api_key is None:
admin_api_key = os.environ.get("OPENAI_ADMIN_KEY")
self.admin_api_key = admin_api_key

if (
_enforce_credentials
and not self.api_key
and not _api_key_explicitly_provided
and self._api_key_provider is None
and workload_identity is None
and self.admin_api_key is None
Expand Down Expand Up @@ -995,6 +1008,9 @@ def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
if _has_header(headers, "Authorization") or _has_omitted_header(custom_headers, "Authorization"):
return

if self._api_key_explicitly_provided:
return

raise TypeError(
'"Could not resolve authentication method. Expected either api_key or admin_api_key to be set. Or for one of the `Authorization` or `Authorization` headers to be explicitly omitted"'
)
Expand Down