Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/models/__tests__/openai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('OpenAIModel', () => {
vi.stubEnv('OPENAI_API_KEY', '')
}
expect(() => new OpenAIModel({ modelId: 'gpt-4o' })).toThrow(
"OpenAI API key is required. Provide it via the 'apiKey' option or set the OPENAI_API_KEY environment variable."
"OpenAI API key is required. Provide it via the 'apiKey' option (string or function) or set the OPENAI_API_KEY environment variable."
)
})

Expand Down Expand Up @@ -144,6 +144,37 @@ describe('OpenAIModel', () => {
const mockClient = {} as OpenAI
expect(() => new OpenAIModel({ modelId: 'gpt-4o', client: mockClient })).not.toThrow()
})

it('accepts function-based API key', () => {
const apiKeyFn = vi.fn(async () => 'sk-dynamic')
new OpenAIModel({
modelId: 'gpt-4o',
apiKey: apiKeyFn,
})
expect(OpenAI).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: apiKeyFn,
})
)
})

it('accepts async function-based API key', () => {
const apiKeyFn = async (): Promise<string> => {
await new Promise((resolve) => globalThis.setTimeout(resolve, 10))
return 'sk-async-key'
}

new OpenAIModel({
modelId: 'gpt-4o',
apiKey: apiKeyFn,
})

expect(OpenAI).toHaveBeenCalledWith(
expect.objectContaining({
apiKey: apiKeyFn,
})
)
})
})

describe('updateConfig', () => {
Expand Down
15 changes: 13 additions & 2 deletions src/models/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import OpenAI, { type ClientOptions } from 'openai'
import type { ApiKeySetter } from 'openai/client'
import { Model } from '../models/model.js'
import type { BaseModelConfig, StreamOptions } from '../models/model.js'
import type { Message } from '../types/messages.js'
Expand Down Expand Up @@ -169,8 +170,12 @@ export interface OpenAIModelConfig extends BaseModelConfig {
export interface OpenAIModelOptions extends OpenAIModelConfig {
/**
* OpenAI API key (falls back to OPENAI_API_KEY environment variable).
*
* Accepts either a static string or an async function that resolves to a string.
* When a function is provided, it is invoked before each request, allowing for
* dynamic API key rotation or runtime credential refresh.
*/
apiKey?: string
apiKey?: string | ApiKeySetter

/**
* Pre-configured OpenAI client instance.
Expand Down Expand Up @@ -241,6 +246,12 @@ export class OpenAIModel extends Model<OpenAIModelConfig> {
* modelId: 'gpt-3.5-turbo'
* })
*
* // Using function-based API key for dynamic key retrieval
* const provider = new OpenAIModel({
* modelId: 'gpt-4o',
* apiKey: async () => await getRotatingApiKey()
* })
*
* // Using a pre-configured client instance
* const client = new OpenAI({ apiKey: 'sk-...', timeout: 60000 })
* const provider = new OpenAIModel({
Expand All @@ -267,7 +278,7 @@ export class OpenAIModel extends Model<OpenAIModelConfig> {
typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.OPENAI_API_KEY
if (!apiKey && !hasEnvKey) {
throw new Error(
"OpenAI API key is required. Provide it via the 'apiKey' option or set the OPENAI_API_KEY environment variable."
"OpenAI API key is required. Provide it via the 'apiKey' option (string or function) or set the OPENAI_API_KEY environment variable."
)
}

Expand Down
Loading