Skip to content

Commit 3f1b4cb

Browse files
refactor: simplify O3 family model check and update max_tokens handling
1 parent 2e59347 commit 3f1b4cb

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/api/providers/openai.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
9292
const deepseekReasoner = modelId.includes("deepseek-reasoner") || enabledR1Format
9393
const ark = modelUrl.includes(".volces.com")
9494

95-
if (modelId.includes("o1") || modelId.includes("o3") || modelId.includes("o4")) {
95+
if (this.isO3FamilyModel(modelId)) {
9696
yield* this.handleO3FamilyMessage(modelId, systemPrompt, messages)
9797
return
9898
}
@@ -365,6 +365,10 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
365365
}
366366
}
367367

368+
private isO3FamilyModel(modelId: string): boolean {
369+
return modelId.includes("o1") || modelId.includes("o3") || modelId.includes("o4")
370+
}
371+
368372
private async *handleStreamResponse(stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>): ApiStream {
369373
for await (const chunk of stream) {
370374
const delta = chunk.choices[0]?.delta
@@ -404,21 +408,29 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
404408
}
405409

406410
/**
407-
* Adds max_completion_tokens to the request body if needed based on provider configuration
411+
* Adds max_tokens or max_completion_tokens to the request body if needed based on provider configuration
408412
* Note: max_tokens is deprecated in favor of max_completion_tokens as per OpenAI documentation
409-
* O3 family models handle max_tokens separately in handleO3FamilyMessage
413+
* O3 family models only support max_completion_tokens, while others use both for compatibility
410414
*/
411415
private addMaxTokensIfNeeded(
412416
requestOptions:
413417
| OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming
414418
| OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming,
415419
modelInfo: ModelInfo,
416420
): void {
417-
// Only add max_completion_tokens if includeMaxTokens is true
421+
// Only add max tokens if includeMaxTokens is true
418422
if (this.options.includeMaxTokens === true) {
419423
// Use user-configured modelMaxTokens if available, otherwise fall back to model's default maxTokens
420-
// Using max_completion_tokens as max_tokens is deprecated
421-
requestOptions.max_completion_tokens = this.options.modelMaxTokens || modelInfo.maxTokens
424+
const maxTokens = this.options.modelMaxTokens || modelInfo.maxTokens
425+
426+
if (this.isO3FamilyModel(this.options.openAiModelId ?? "")) {
427+
// O3 family models only support max_completion_tokens
428+
requestOptions.max_completion_tokens = maxTokens
429+
} else {
430+
// Other models use both max_tokens and max_completion_tokens for compatibility
431+
requestOptions.max_tokens = maxTokens
432+
requestOptions.max_completion_tokens = maxTokens
433+
}
422434
}
423435
}
424436
}

0 commit comments

Comments
 (0)