The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
fix(ai): retry chat requests with reasoning_effort none when a server refuses tools with reasoning
Mauricebac968c0bf26
diff --git a/server/src/nest/ai-connections/providers/openai-chat.client.ts b/server/src/nest/ai-connections/providers/openai-chat.client.tsindex 1c3a988b4..d7625c6a5 100644--- a/server/src/nest/ai-connections/providers/openai-chat.client.ts+++ b/server/src/nest/ai-connections/providers/openai-chat.client.ts@@ -1,3 +1,4 @@+import { logWarn } from '../../audit/audit-log.logger';import type {AiChatClient,AiChatEvent,@@ -8,7 +9,7 @@ import type {AiSource,AiToolCall,} from '../ai-chat-client.interface';-import { AiProviderError, errorCodeOf, providerFailure } from './provider-errors';+import { AiProviderError, errorCodeOf, isReasoningEffortRefusal, providerFailure } from './provider-errors';import {asCount,asRecord,@@ -107,6 +108,13 @@ const FINISH_REASONS: Record<string, AiFinishReason> = { tool_calls: 'tool-calls* schemas, also when an admin enters its endpoint as their own server, and* OpenAI the prompt cache key, which the others do not know. Gemini's thought* signatures come back as the turn's provider state.+ *+ * No request names a reasoning effort, since a server that does not reason may+ * refuse the field. A reasoning model that takes function tools only without+ * reasoning (Azure AI Foundry's deployments, #3043) refuses the default effort+ * by naming `reasoning_effort`; the request then goes out once more with+ * `reasoning_effort: "none"`, and so does every later request with tools on+ * this client, which lives for one turn.*/export class OpenAiChatClient implements AiChatClient {/** OpenRouter searches the web for any model through its web plugin; the others have no search here. */@@ -117,6 +125,9 @@ export class OpenAiChatClient implements AiChatClient {/** The kind whose tool schemas the endpoint takes. */private readonly schemaKind: AiConnectionKind;+ /** Whether the server refused function tools with the model's reasoning, so they go out without it. */+ private toolsWithoutReasoning = false;+constructor(readonly kind: AiConnectionKind,readonly model: string,@@ -128,6 +139,52 @@ export class OpenAiChatClient implements AiChatClient {}async *stream(request: AiChatRequest, signal: AbortSignal): AsyncGenerator<AiChatEvent> {+ const events = await this.open(request, signal);++ const turn = new ChatTurn();+ for await (const sse of readSseEvents(events)) {+ if (sse.data.trim() === DONE) {+ turn.done = true;+ break;+ }+ const chunk = parseEventData(sse.data);+ if (!chunk) continue;+ if (chunk.error) throw providerFailure(this.kind, 0, errorCodeOf(chunk));+ const delta = turn.apply(chunk);+ if (delta) yield { type: 'text-delta', delta };+ for (const source of turn.newSources()) yield { type: 'source', source };+ }+ if (!turn.done && !turn.reason) throw new AiProviderError('provider_unavailable', 0, true, undefined, 'stream_cut');+ for (const call of turn.calls()) yield { type: 'tool-call', call };+ yield {+ type: 'usage',+ inputTokens: turn.inputTokens,+ cachedInputTokens: turn.cachedInputTokens,+ outputTokens: turn.outputTokens,+ };+ const signatures = turn.signatures();+ if (Object.keys(signatures).length > 0) {+ yield { type: 'provider-state', state: { adapter: 'openai-chat', model: this.model, signatures } };+ }+ yield { type: 'finish', reason: turn.finishReason() };+ }++ /**+ * Opens the request's stream. A refusal of the model's reasoning effort for+ * a request with tools is answered once by asking again without reasoning.+ */+ private async open(request: AiChatRequest, signal: AbortSignal): Promise<ReadableStream<Uint8Array>> {+ try {+ return await this.send(request, signal);+ } catch (err) {+ if (this.toolsWithoutReasoning || request.tools.length === 0 || !isReasoningEffortRefusal(err)) throw err;+ this.toolsWithoutReasoning = true;+ logWarn(`[ai] ${this.kind} takes function tools only without reasoning: asking again with reasoning_effort none`);+ return this.send(request, signal);+ }+ }++ private send(request: AiChatRequest, signal: AbortSignal): Promise<ReadableStream<Uint8Array>> {const body = {model: this.model,stream: true,@@ -146,10 +203,11 @@ export class OpenAiChatClient implements AiChatClient {tool_choice: request.toolChoice ?? 'auto',}: {}),+ ...(this.toolsWithoutReasoning && request.tools.length > 0 ? { reasoning_effort: 'none' } : {}),...(this.kind === 'openai' && request.cacheKey ? { prompt_cache_key: request.cacheKey } : {}),...(request.webSearch && this.webSearch ? { plugins: [{ id: 'web', max_results: 5 }] } : {}),};- const events = await openProviderStream(+ return openProviderStream(this.fetcher,this.kind,`${this.baseUrl}/chat/completions`,@@ -164,33 +222,6 @@ export class OpenAiChatClient implements AiChatClient {},signal,);-- const turn = new ChatTurn();- for await (const sse of readSseEvents(events)) {- if (sse.data.trim() === DONE) {- turn.done = true;- break;- }- const chunk = parseEventData(sse.data);- if (!chunk) continue;- if (chunk.error) throw providerFailure(this.kind, 0, errorCodeOf(chunk));- const delta = turn.apply(chunk);- if (delta) yield { type: 'text-delta', delta };- for (const source of turn.newSources()) yield { type: 'source', source };- }- if (!turn.done && !turn.reason) throw new AiProviderError('provider_unavailable', 0, true, undefined, 'stream_cut');- for (const call of turn.calls()) yield { type: 'tool-call', call };- yield {- type: 'usage',- inputTokens: turn.inputTokens,- cachedInputTokens: turn.cachedInputTokens,- outputTokens: turn.outputTokens,- };- const signatures = turn.signatures();- if (Object.keys(signatures).length > 0) {- yield { type: 'provider-state', state: { adapter: 'openai-chat', model: this.model, signatures } };- }- yield { type: 'finish', reason: turn.finishReason() };}}diff --git a/server/src/nest/ai-connections/providers/provider-errors.ts b/server/src/nest/ai-connections/providers/provider-errors.tsindex 12e89f630..aa9948944 100644--- a/server/src/nest/ai-connections/providers/provider-errors.ts+++ b/server/src/nest/ai-connections/providers/provider-errors.ts@@ -10,6 +10,12 @@ export const CHATGPT_USAGE_URL = 'https://chatgpt.com/settings/usage';* request again later can help.*/export class AiProviderError extends Error {+ /**+ * The request field the provider named as refused (OpenAI's `param`), when+ * it named one as a bare path. Set by `refusalOf` for an HTTP refusal.+ */+ refusedParam?: string;+constructor(readonly code: AssistantErrorCode,readonly status: number,@@ -120,3 +126,12 @@ export function namesImageInput(status: number, body: unknown): boolean {export function isImageRefusal(err: unknown): boolean {return err instanceof AiProviderError && err.providerCode === IMAGE_INPUT_REFUSED;}++/**+ * Whether a provider error is the refusal of the request's reasoning effort:+ * a reasoning model behind Chat Completions that takes function tools only+ * with `reasoning_effort: "none"`, as Azure AI Foundry's deployments do (#3043).+ */+export function isReasoningEffortRefusal(err: unknown): boolean {+ return err instanceof AiProviderError && err.status === 400 && err.refusedParam === 'reasoning_effort';+}diff --git a/server/src/nest/ai-connections/providers/provider-http.ts b/server/src/nest/ai-connections/providers/provider-http.tsindex b1542772a..188e8dfc3 100644--- a/server/src/nest/ai-connections/providers/provider-http.ts+++ b/server/src/nest/ai-connections/providers/provider-http.ts@@ -65,7 +65,9 @@ export async function refusalOf(kind: AiConnectionKind, res: Response): Promise<logWarn(`[ai] ${kind} refused the request: HTTP ${res.status}${code ? ` (${code})` : ''}${param ? ` at ${param}` : ''}`,);- return providerFailure(kind, res.status, code);+ const failure = providerFailure(kind, res.status, code);+ failure.refusedParam = param;+ return failure;}/** The field an error body names as refused (OpenAI's `param`, such as `input[12]`), when it is a bare path. */diff --git a/server/tests/unit/nest/ai-connections/provider-adapters.test.ts b/server/tests/unit/nest/ai-connections/provider-adapters.test.tsindex 8a36d1c53..f9ea47354 100644--- a/server/tests/unit/nest/ai-connections/provider-adapters.test.ts+++ b/server/tests/unit/nest/ai-connections/provider-adapters.test.ts@@ -676,6 +676,74 @@ describe('OpenAiChatClient', () => {expect(await parameters('http://ollama:11434/v1')).toEqual(toProviderSchema(TOOL.parameters, 'custom'));expect(await parameters('not a url')).toEqual(toProviderSchema(TOOL.parameters, 'custom'));});++ // Azure AI Foundry's answer for a reasoning deployment asked for function tools on Chat Completions (#3043).+ const REASONING_REFUSAL = {+ error: {+ message:+ "Function tools with reasoning_effort are not supported for gpt-6-luna in /v1/chat/completions. To use function tools, use /v1/responses or set reasoning_effort to 'none'.",+ type: 'invalid_request_error',+ param: 'reasoning_effort',+ code: null,+ },+ };+ const AZURE = 'https://res.services.ai.azure.com/openai/v1';++ it('OACH-010: a server that takes tools only without reasoning is asked again with reasoning_effort none (#3043)', async () => {+ logWarn.mockClear();+ const http = fetchAnswering(+ jsonResponse(400, REASONING_REFUSAL),+ streamResponse(STREAM),+ streamResponse(STREAM),+ streamResponse([{ choices: [{ delta: { content: 'hi' }, finish_reason: 'stop' }] }]),+ );+ const client = new OpenAiChatClient('custom', 'gpt-6-luna', AZURE, 'k', http.fetcher);++ const events = await run(client.stream(REQUEST, signal()));+ // Nothing of the refused request reaches the turn: it answers as if the first request had been taken.+ expect(events.filter((event) => event.type === 'tool-call')).toHaveLength(1);+ expect(events.at(-1)).toEqual({ type: 'finish', reason: 'tool-calls' });+ expect(http.body(0)).not.toHaveProperty('reasoning_effort');+ expect(http.body(1)).toMatchObject({ reasoning_effort: 'none', tool_choice: 'auto' });+ expect(logWarn).toHaveBeenCalledWith(+ '[ai] custom takes function tools only without reasoning: asking again with reasoning_effort none',+ );++ // The turn's next step goes out without reasoning right away; a step without tools may reason again.+ await run(client.stream(REQUEST, signal()));+ expect(http.body(2)).toMatchObject({ reasoning_effort: 'none' });+ await run(client.stream({ ...REQUEST, tools: [] }, signal()));+ expect(http.body(3)).not.toHaveProperty('reasoning_effort');+ expect(http.calls).toHaveLength(4);+ });++ it('OACH-011: any other server, refusal or request keeps the body as it was and is not asked again (#3043)', async () => {+ // Ollama, LM Studio, vLLM and OpenRouter never see the parameter.+ for (const kind of ['custom', 'openrouter', 'openai', 'gemini'] as const) {+ const plain = fetchAnswering(streamResponse(STREAM));+ await run(+ new OpenAiChatClient(kind, 'm', 'http://ollama:11434/v1', null, plain.fetcher).stream(REQUEST, signal()),+ );+ expect(plain.body()).not.toHaveProperty('reasoning_effort');+ }++ const once = async (request: AiChatRequest, ...responses: Response[]) => {+ const http = fetchAnswering(...responses);+ const err = await failure(+ new OpenAiChatClient('custom', 'm', AZURE, 'k', http.fetcher).stream(request, signal()),+ );+ return { err, calls: http.calls.length };+ };+ // A refusal of another field, of the same field with another status, or of a request without tools.+ const other = { error: { ...REASONING_REFUSAL.error, param: 'tools[0]' } };+ expect(await once(REQUEST, jsonResponse(400, other))).toMatchObject({ err: { code: 'generic' }, calls: 1 });+ expect(await once(REQUEST, jsonResponse(422, REASONING_REFUSAL))).toMatchObject({ calls: 1 });+ expect(await once({ ...REQUEST, tools: [] }, jsonResponse(400, REASONING_REFUSAL))).toMatchObject({ calls: 1 });+ // A server that refuses `none` as well is asked only once more, and its refusal is the turn's error.+ expect(+ await once(REQUEST, jsonResponse(400, REASONING_REFUSAL), jsonResponse(400, REASONING_REFUSAL)),+ ).toMatchObject({ err: { code: 'generic', status: 400 }, calls: 2 });+ });});describe('OpenAiResponsesClient', () => {