fix: use max_completion_tokens instead of max_tokens for OpenAI-compatible APIs

Azure OpenAI and newer OpenAI models (o1, o3, o4...) reject `max_tokens`
with a 400 error and require `max_completion_tokens` instead.

Maps `params.max_tokens` → `max_completion_tokens` in the request body,
which is the current standard across OpenAI-compatible providers.
This commit is contained in:
nusquama
2026-04-02 08:36:01 +08:00
parent 5fae22a8f2
commit 537ac24a9c

View File

@@ -636,9 +636,13 @@ class OpenAIShimMessages {
const body: Record<string, unknown> = { const body: Record<string, unknown> = {
model: request.resolvedModel, model: request.resolvedModel,
messages: openaiMessages, messages: openaiMessages,
max_tokens: params.max_tokens,
stream: params.stream ?? false, stream: params.stream ?? false,
} }
if (params.max_tokens !== undefined) {
body.max_completion_tokens = params.max_tokens
} else if ((params as Record<string, unknown>).max_completion_tokens !== undefined) {
body.max_completion_tokens = (params as Record<string, unknown>).max_completion_tokens
}
if (params.stream) { if (params.stream) {
body.stream_options = { include_usage: true } body.stream_options = { include_usage: true }