mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
fix(ai): retry chat requests with reasoning_effort none when a server refuses tools with reasoning
Azure AI Foundry reasoning deployments behind an "Own server" connection reject function tools on /v1/chat/completions at their default reasoning effort and answer HTTP 400 naming reasoning_effort. The Responses API switch from #2792 only covers direct OpenAI keys, so the assistant could not run on these deployments at all. The chat client still never sends reasoning_effort, so servers that do not know the field (Ollama, LM Studio, vLLM, OpenRouter) see unchanged requests. When a request with tools is refused over reasoning_effort, it goes out once more with reasoning_effort "none", and the rest of the turn keeps sending it. The refused field is now carried on AiProviderError instead of only being logged. Fixes #3043
This commit is contained in:
@@ -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,42 +139,7 @@ export class OpenAiChatClient implements AiChatClient {
|
||||
}
|
||||
|
||||
async *stream(request: AiChatRequest, signal: AbortSignal): AsyncGenerator<AiChatEvent> {
|
||||
const body = {
|
||||
model: this.model,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
messages: toChatMessages(request, this.model),
|
||||
...(request.tools.length > 0
|
||||
? {
|
||||
tools: request.tools.map((tool) => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: toProviderSchema(tool.parameters, this.schemaKind),
|
||||
},
|
||||
})),
|
||||
tool_choice: request.toolChoice ?? 'auto',
|
||||
}
|
||||
: {}),
|
||||
...(this.kind === 'openai' && request.cacheKey ? { prompt_cache_key: request.cacheKey } : {}),
|
||||
...(request.webSearch && this.webSearch ? { plugins: [{ id: 'web', max_results: 5 }] } : {}),
|
||||
};
|
||||
const events = await openProviderStream(
|
||||
this.fetcher,
|
||||
this.kind,
|
||||
`${this.baseUrl}/chat/completions`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
accept: 'text/event-stream',
|
||||
...(this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
signal,
|
||||
);
|
||||
const events = await this.open(request, signal);
|
||||
|
||||
const turn = new ChatTurn();
|
||||
for await (const sse of readSseEvents(events)) {
|
||||
@@ -192,6 +168,61 @@ export class OpenAiChatClient implements AiChatClient {
|
||||
}
|
||||
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,
|
||||
stream_options: { include_usage: true },
|
||||
messages: toChatMessages(request, this.model),
|
||||
...(request.tools.length > 0
|
||||
? {
|
||||
tools: request.tools.map((tool) => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: toProviderSchema(tool.parameters, this.schemaKind),
|
||||
},
|
||||
})),
|
||||
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 }] } : {}),
|
||||
};
|
||||
return openProviderStream(
|
||||
this.fetcher,
|
||||
this.kind,
|
||||
`${this.baseUrl}/chat/completions`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
accept: 'text/event-stream',
|
||||
...(this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** A tool call while it streams in: its id, name and arguments so far, and Gemini's signature. */
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user