@@ -59,9 +59,9 @@ class OpenAI(SyncAPIClient):
5959 def __init__ (
6060 self ,
6161 * ,
62+ api_key : str | None = None ,
6263 organization : str | None = None ,
6364 base_url : Optional [str ] = None ,
64- api_key : Optional [str ] = None ,
6565 timeout : Union [float , Timeout , None , NotGiven ] = NOT_GIVEN ,
6666 max_retries : int = DEFAULT_MAX_RETRIES ,
6767 default_headers : Mapping [str , str ] | None = None ,
@@ -84,15 +84,17 @@ def __init__(
8484 - `api_key` from `OPENAI_API_KEY`
8585 - `organization` from `OPENAI_ORG_ID`
8686 """
87- api_key = api_key or os .environ .get ("OPENAI_API_KEY" , None )
88- if not api_key :
87+ if api_key is None :
88+ api_key = os .environ .get ("OPENAI_API_KEY" )
89+ if api_key is None :
8990 raise OpenAIError (
9091 "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"
9192 )
9293 self .api_key = api_key
9394
94- organization_envvar = os .environ .get ("OPENAI_ORG_ID" , None )
95- self .organization = organization or organization_envvar or None
95+ if organization is None :
96+ organization = os .environ .get ("OPENAI_ORG_ID" ) or None
97+ self .organization = organization
9698
9799 if base_url is None :
98100 base_url = f"https://api.openai.com/v1"
@@ -142,8 +144,8 @@ def default_headers(self) -> dict[str, str | Omit]:
142144 def copy (
143145 self ,
144146 * ,
145- organization : str | None = None ,
146147 api_key : str | None = None ,
148+ organization : str | None = None ,
147149 base_url : str | None = None ,
148150 timeout : float | Timeout | None | NotGiven = NOT_GIVEN ,
149151 http_client : httpx .Client | None = None ,
@@ -179,9 +181,9 @@ def copy(
179181
180182 http_client = http_client or self ._client
181183 return self .__class__ (
184+ api_key = api_key or self .api_key ,
182185 organization = organization or self .organization ,
183186 base_url = base_url or str (self .base_url ),
184- api_key = api_key or self .api_key ,
185187 timeout = self .timeout if isinstance (timeout , NotGiven ) else timeout ,
186188 http_client = http_client ,
187189 max_retries = max_retries if is_given (max_retries ) else self .max_retries ,
@@ -256,9 +258,9 @@ class AsyncOpenAI(AsyncAPIClient):
256258 def __init__ (
257259 self ,
258260 * ,
261+ api_key : str | None = None ,
259262 organization : str | None = None ,
260263 base_url : Optional [str ] = None ,
261- api_key : Optional [str ] = None ,
262264 timeout : Union [float , Timeout , None , NotGiven ] = NOT_GIVEN ,
263265 max_retries : int = DEFAULT_MAX_RETRIES ,
264266 default_headers : Mapping [str , str ] | None = None ,
@@ -281,15 +283,17 @@ def __init__(
281283 - `api_key` from `OPENAI_API_KEY`
282284 - `organization` from `OPENAI_ORG_ID`
283285 """
284- api_key = api_key or os .environ .get ("OPENAI_API_KEY" , None )
285- if not api_key :
286+ if api_key is None :
287+ api_key = os .environ .get ("OPENAI_API_KEY" )
288+ if api_key is None :
286289 raise OpenAIError (
287290 "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"
288291 )
289292 self .api_key = api_key
290293
291- organization_envvar = os .environ .get ("OPENAI_ORG_ID" , None )
292- self .organization = organization or organization_envvar or None
294+ if organization is None :
295+ organization = os .environ .get ("OPENAI_ORG_ID" ) or None
296+ self .organization = organization
293297
294298 if base_url is None :
295299 base_url = f"https://api.openai.com/v1"
@@ -339,8 +343,8 @@ def default_headers(self) -> dict[str, str | Omit]:
339343 def copy (
340344 self ,
341345 * ,
342- organization : str | None = None ,
343346 api_key : str | None = None ,
347+ organization : str | None = None ,
344348 base_url : str | None = None ,
345349 timeout : float | Timeout | None | NotGiven = NOT_GIVEN ,
346350 http_client : httpx .AsyncClient | None = None ,
@@ -376,9 +380,9 @@ def copy(
376380
377381 http_client = http_client or self ._client
378382 return self .__class__ (
383+ api_key = api_key or self .api_key ,
379384 organization = organization or self .organization ,
380385 base_url = base_url or str (self .base_url ),
381- api_key = api_key or self .api_key ,
382386 timeout = self .timeout if isinstance (timeout , NotGiven ) else timeout ,
383387 http_client = http_client ,
384388 max_retries = max_retries if is_given (max_retries ) else self .max_retries ,
0 commit comments