Skip to content

Commit 4978db3

Browse files
committed
Change the implementation to work in any initialization processes
1 parent 152e774 commit 4978db3

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

packages/agents-openai/src/openaiProvider.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type OpenAIProviderOptions = {
2525
* The provider of OpenAI's models (or Chat Completions compatible ones)
2626
*/
2727
export class OpenAIProvider implements ModelProvider {
28-
#client: OpenAI;
28+
#client?: OpenAI;
2929
#useResponses?: boolean;
3030
#options: OpenAIProviderOptions;
3131

@@ -38,19 +38,8 @@ export class OpenAIProvider implements ModelProvider {
3838
if (this.#options.baseURL) {
3939
throw new Error('Cannot provide both baseURL and openAIClient');
4040
}
41+
this.#client = this.#options.openAIClient;
4142
}
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-
});
5443
this.#useResponses = this.#options.useResponses;
5544
}
5645

@@ -59,6 +48,19 @@ export class OpenAIProvider implements ModelProvider {
5948
* never actually use the client.
6049
*/
6150
#getClient(): OpenAI {
51+
// If the constructor does not accept the OpenAI client,
52+
if (!this.#client) {
53+
this.#client =
54+
// this provider checks if there is the default client first,
55+
getDefaultOpenAIClient() ??
56+
// and then manually creates a new one.
57+
new OpenAI({
58+
apiKey: this.#options.apiKey ?? getDefaultOpenAIKey(),
59+
baseURL: this.#options.baseURL,
60+
organization: this.#options.organization,
61+
project: this.#options.project,
62+
});
63+
}
6264
return this.#client;
6365
}
6466

0 commit comments

Comments
 (0)