@@ -2,6 +2,7 @@ import { Model, ModelProvider } from '@openai/agents-core';
22import OpenAI from 'openai' ;
33import {
44 DEFAULT_OPENAI_MODEL ,
5+ getDefaultOpenAIClient ,
56 getDefaultOpenAIKey ,
67 shouldUseResponsesByDefault ,
78} from './defaults' ;
@@ -24,7 +25,7 @@ export type OpenAIProviderOptions = {
2425 * The provider of OpenAI's models (or Chat Completions compatible ones)
2526 */
2627export class OpenAIProvider implements ModelProvider {
27- #client? : OpenAI ;
28+ #client: OpenAI ;
2829 #useResponses?: boolean ;
2930 #options: OpenAIProviderOptions ;
3031
@@ -37,9 +38,19 @@ export class OpenAIProvider implements ModelProvider {
3738 if ( this . #options. baseURL ) {
3839 throw new Error ( 'Cannot provide both baseURL and openAIClient' ) ;
3940 }
40- this . #client = this . #options. openAIClient ;
4141 }
42-
42+ this . #client =
43+ // If the constructor does not accept the OpenAI client,
44+ this . #options. openAIClient ??
45+ // this provider checks if there is the default client first,
46+ getDefaultOpenAIClient ( ) ??
47+ // and then manually creates a new one.
48+ new OpenAI ( {
49+ apiKey : this . #options. apiKey ?? getDefaultOpenAIKey ( ) ,
50+ baseURL : this . #options. baseURL ,
51+ organization : this . #options. organization ,
52+ project : this . #options. project ,
53+ } ) ;
4354 this . #useResponses = this . #options. useResponses ;
4455 }
4556
@@ -48,15 +59,6 @@ export class OpenAIProvider implements ModelProvider {
4859 * never actually use the client.
4960 */
5061 #getClient( ) : OpenAI {
51- if ( ! this . #client) {
52- this . #client = new OpenAI ( {
53- apiKey : this . #options. apiKey ?? getDefaultOpenAIKey ( ) ,
54- baseURL : this . #options. baseURL ,
55- organization : this . #options. organization ,
56- project : this . #options. project ,
57- } ) ;
58- }
59-
6062 return this . #client;
6163 }
6264
0 commit comments