From c0ce6f9f1ffedb6fbfe5a75093fdf021529ce9f1 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 3 Feb 2026 13:08:01 +0100 Subject: [PATCH] Fix GPT-5 max_completion_tokens parameter detection GPT-5 and newer models require max_completion_tokens instead of max_tokens. Added usesNewTokenParam() to detect GPT-5+ models separately from reasoning model restrictions (temperature, json_object, system messages). Co-Authored-By: Claude Opus 4.5 --- src/lib/openai.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib/openai.ts b/src/lib/openai.ts index 85126c8..a4f7a0c 100644 --- a/src/lib/openai.ts +++ b/src/lib/openai.ts @@ -19,8 +19,15 @@ const globalForOpenAI = globalThis as unknown as { */ const REASONING_MODEL_PREFIXES = ['o1', 'o3', 'o4'] +/** + * Models that use max_completion_tokens instead of max_tokens. + * This includes reasoning models AND newer GPT models (GPT-5+). + */ +const NEW_TOKEN_PARAM_PREFIXES = ['o1', 'o3', 'o4', 'gpt-5', 'gpt-6', 'gpt-7'] + /** * Check if a model is a reasoning model (o1, o3, o4 series) + * These models have additional restrictions (no temperature, no json_object, etc.) */ export function isReasoningModel(model: string): boolean { const modelLower = model.toLowerCase() @@ -31,6 +38,19 @@ export function isReasoningModel(model: string): boolean { ) } +/** + * Check if a model requires max_completion_tokens instead of max_tokens. + * This includes reasoning models AND newer GPT models (GPT-5+). + */ +export function usesNewTokenParam(model: string): boolean { + const modelLower = model.toLowerCase() + return NEW_TOKEN_PARAM_PREFIXES.some(prefix => + modelLower.startsWith(prefix) || + modelLower.includes(`/${prefix}`) || + modelLower.includes(`-${prefix}`) + ) +} + // ─── Chat Completion Parameter Builder ─────────────────────────────────────── type MessageRole = 'system' | 'user' | 'assistant' | 'developer' @@ -84,8 +104,9 @@ export function buildCompletionParams( } // Token limit parameter differs between model types + // Newer models (GPT-5+, o-series) use max_completion_tokens if (options.maxTokens) { - if (isReasoning) { + if (usesNewTokenParam(model)) { params.max_completion_tokens = options.maxTokens } else { params.max_tokens = options.maxTokens