fix(assistant): send OpenAI reasoning models on an API key to the Responses API

gpt-5.6-sol, gpt-6.1-sol and the other reasoning models refuse function tools
on Chat Completions, so every assistant message on such a model came back as
HTTP 400. An OpenAI connection whose model is from the o-series or GPT-5 and
later now goes through the Responses API, the client a ChatGPT plan already
uses, with the key as its bearer token and no token refresh on a 401. Older
models and the -chat variants stay on Chat Completions.

Closes #2792
This commit is contained in:
Maurice
2026-10-25 16:55:39 +01:00
parent c1c2bbf146
commit f6976d5cec
4 changed files with 117 additions and 9 deletions
@@ -23,10 +23,21 @@ const cloudFetch: AiFetch = (url, init) => safeFetch(url, { ...init, signal: ini
*/
const adminServerFetch: AiFetch = (url, init) => safeFetchLlm(url, { ...init, signal: init.signal }, 5, null);
/**
* Whether an OpenAI model behind an API key goes through the Responses API:
* the reasoning models (the o-series, GPT-5 and later) refuse function tools
* on Chat Completions with a 400 (#2792). Their `-chat` variants do not reason
* and stay on Chat Completions, like the older GPT-4 line.
*/
export function openAiWantsResponses(model: string): boolean {
return /^(o\d|gpt-([5-9]|\d{2,}))/.test(model) && !/-chat/.test(model);
}
/**
* The chat client for a resolved connection: Anthropic's Messages API, the
* Responses API for a ChatGPT plan, and Chat Completions for everything
* OpenAI-compatible (OpenAI, Gemini, OpenRouter, an admin's own server).
* Responses API for a ChatGPT plan and OpenAI's reasoning models, and Chat
* Completions for everything else OpenAI-compatible (older OpenAI models,
* Gemini, OpenRouter, an admin's own server).
*/
@Injectable()
export class AiChatClientService {
@@ -45,6 +56,10 @@ export class AiChatClientService {
cloudFetch,
);
}
if (kind === 'openai' && openAiWantsResponses(model)) {
const apiKey = credential.apiKey ?? '';
return new OpenAiResponsesClient(model, () => Promise.resolve(apiKey), cloudFetch, 'openai');
}
switch (kind) {
case 'anthropic':
return new AnthropicMessagesClient(model, credential.apiKey ?? '', cloudFetch);
@@ -130,20 +130,22 @@ function citedSources(item: Record<string, unknown>): AiSource[] {
}
/**
* OpenAI's Responses API on a ChatGPT plan (Sign in with ChatGPT). Within the
* preview's limits: nothing stored, streamed, no output cap or temperature,
* the instructions as `instructions`, the tools in one namespace that carries
* a description. A 401 before the stream starts is answered once with a
* refreshed token; a second one means the sign-in has to be renewed.
* OpenAI's Responses API, on a ChatGPT plan (Sign in with ChatGPT) or, for the
* reasoning models, on an OpenAI API key, which take their function tools only
* here (#2792). Within the preview's limits: nothing stored, streamed, no
* output cap or temperature, the instructions as `instructions`, the tools in
* one namespace that carries a description. On a plan, a 401 before the stream
* starts is answered once with a refreshed token; a second one means the
* sign-in has to be renewed. A key has nothing to refresh.
*/
export class OpenAiResponsesClient implements AiChatClient {
readonly kind = 'chatgpt' as const;
readonly webSearch = true;
constructor(
readonly model: string,
private readonly token: ChatgptTokenSource,
private readonly fetcher: AiFetch,
readonly kind: 'chatgpt' | 'openai' = 'chatgpt',
) {}
async *stream(request: AiChatRequest, signal: AbortSignal): AsyncGenerator<AiChatEvent> {
@@ -227,7 +229,7 @@ export class OpenAiResponsesClient implements AiChatClient {
try {
return await send(accessToken);
} catch (err) {
if (!(err instanceof AiProviderError) || err.status !== 401) throw err;
if (this.kind !== 'chatgpt' || !(err instanceof AiProviderError) || err.status !== 401) throw err;
return send(await this.token(accessToken));
}
}