@@ -136,10 +136,11 @@ def __init__(
136
136
"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"
137
137
)
138
138
if callable (api_key ):
139
- self .bearer_token_provider = api_key
140
139
self .api_key = ""
140
+ self .bearer_token_provider = api_key
141
141
else :
142
142
self .api_key = api_key or ""
143
+ self .bearer_token_provider = None
143
144
144
145
if organization is None :
145
146
organization = os .environ .get ("OPENAI_ORG_ID" )
@@ -296,7 +297,10 @@ def qs(self) -> Querystring:
296
297
return Querystring (array_format = "brackets" )
297
298
298
299
def refresh_auth_headers (self ) -> None :
299
- secret = self .bearer_token_provider () if self .bearer_token_provider else self .api_key
300
+ if self .bearer_token_provider :
301
+ secret = self .bearer_token_provider ()
302
+ else :
303
+ secret = self .api_key
300
304
if not secret :
301
305
# if the api key is an empty string, encoding the header will fail
302
306
# so we set it to an empty dict
@@ -367,7 +371,7 @@ def copy(
367
371
368
372
http_client = http_client or self ._client
369
373
return self .__class__ (
370
- api_key = api_key or self .api_key or self .bearer_token_provider ,
374
+ api_key = api_key or self .bearer_token_provider or self .api_key ,
371
375
organization = organization or self .organization ,
372
376
project = project or self .project ,
373
377
webhook_secret = webhook_secret or self .webhook_secret ,
@@ -478,11 +482,11 @@ def __init__(
478
482
"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"
479
483
)
480
484
if callable (api_key ):
481
- self .bearer_token_provider = api_key
482
485
self .api_key = ""
486
+ self .bearer_token_provider = api_key
483
487
else :
484
- self .bearer_token_provider = None
485
488
self .api_key = api_key or ""
489
+ self .bearer_token_provider = None
486
490
487
491
if organization is None :
488
492
organization = os .environ .get ("OPENAI_ORG_ID" )
@@ -713,7 +717,7 @@ def copy(
713
717
714
718
http_client = http_client or self ._client
715
719
return self .__class__ (
716
- api_key = api_key or self .api_key or self .bearer_token_provider ,
720
+ api_key = api_key or self .bearer_token_provider or self .api_key ,
717
721
organization = organization or self .organization ,
718
722
project = project or self .project ,
719
723
webhook_secret = webhook_secret or self .webhook_secret ,
0 commit comments