@@ -42,7 +42,6 @@ import {
4242 type OpenAIApiKey ,
4343} from "../types.js" ;
4444import { type OpenAIEndpointConfig , getEndpoint } from "../utils/azure.js" ;
45- import { resolveOpenAIApiKey } from "../utils/client.js" ;
4645import {
4746 type FunctionDef ,
4847 formatFunctionDefinitions ,
@@ -463,17 +462,12 @@ export abstract class BaseChatOpenAI<
463462 if ( this . disableStreaming ) this . streamUsage = false ;
464463
465464 const clientConfig : ClientOptions = {
465+ apiKey : this . apiKey ,
466466 dangerouslyAllowBrowser : true ,
467467 ...fields ?. configuration ,
468468 organization : this . organization ,
469469 } ;
470470
471- if ( typeof this . apiKey === "string" ) {
472- clientConfig . apiKey = this . apiKey ;
473- } else {
474- clientConfig . apiKey = undefined ;
475- }
476-
477471 this . clientConfig = clientConfig ;
478472
479473 // If `supportsStrictToolCalling` is explicitly set, use that value.
@@ -555,7 +549,7 @@ export abstract class BaseChatOpenAI<
555549 protected async _getClientOptions (
556550 options : OpenAICoreRequestOptions | undefined
557551 ) : Promise < OpenAICoreRequestOptions > {
558- const resolvedApiKey = await resolveOpenAIApiKey ( this . apiKey ) ;
552+ const currentApiKey = this . apiKey ?? this . clientConfig . apiKey ;
559553
560554 if ( ! this . client ) {
561555 const openAIEndpointConfig : OpenAIEndpointConfig = {
@@ -571,19 +565,21 @@ export abstract class BaseChatOpenAI<
571565 maxRetries : 0 ,
572566 } ;
573567
574- if ( resolvedApiKey !== undefined ) {
575- params . apiKey = resolvedApiKey ;
576- } else if ( typeof this . clientConfig . apiKey === "string" ) {
577- params . apiKey = this . clientConfig . apiKey ;
568+ if ( currentApiKey !== undefined ) {
569+ params . apiKey = currentApiKey ;
578570 }
579571
580572 if ( ! params . baseURL ) {
581573 delete params . baseURL ;
582574 }
583575
584576 this . client = new OpenAIClient ( params ) ;
585- } else if ( resolvedApiKey !== undefined ) {
586- this . client . apiKey = resolvedApiKey ;
577+ } else if ( currentApiKey !== undefined ) {
578+ if ( typeof currentApiKey === "string" ) {
579+ this . client . apiKey = currentApiKey ;
580+ } else {
581+ this . client = this . client . withOptions ( { apiKey : currentApiKey } ) ;
582+ }
587583 }
588584
589585 const requestOptions = {
0 commit comments