Files
OmniRoute/tests/unit/azure-param-rules.test.ts
Diego Rodrigues de Sa e Souza 5926d35758 cherry-pick(pr-9787): fix(sse): apply Azure param rules on azure-ai and clamp gpt-4o-mini output tokens (#9855)
* fix(sse): apply Azure request-param rules on the azure-ai wire path

Azure rejects several stock Chat Completions params on its newer deployments
and returns HTTP 400 rather than ignoring them:

  max_tokens       -> 'max_tokens' is not supported with this model.
                      Use 'max_completion_tokens' instead.
  reasoning_effort -> Function tools with reasoning_effort are not supported.

Those rules lived inline in AzureOpenAIExecutor, so they only covered the
azure-openai provider. azure-ai (Azure AI Foundry) had no executor entry and
fell through to the bare DefaultExecutor, so the SAME Azure deployment
succeeded on one connection and 400'd on the other. Every agentic client sends
tools on every turn, so azure-ai failed on the first request.

Extract the rules to open-sse/executors/azureParamRules.ts, add an
AzureAiExecutor that inherits DefaultExecutor's azure-ai URL/header/apiType
handling unchanged and applies the shared rules, and register it for azure-ai.

Also widen the deployment pattern to cover gpt-chat-latest: it is a moving
alias that resolves to a GPT-5-era model and rejects max_tokens, but carries no
version number for the token-boundary pattern to key on. Verified against the
base regex - gpt-chat-latest did not match, which is exactly the observed 400.

Regression guard: tests/unit/azure-param-rules.test.ts, including an assertion
that getExecutor("azure-ai") no longer resolves to a bare DefaultExecutor.

* fix(sse): clamp Azure gpt-4o-mini completion tokens to its 16384 ceiling

Azure gpt-4o-mini deployments accept at most 16384 completion tokens and 400 on
anything larger:

  max_tokens is too large: 32000. This model supports at most 16384 completion
  tokens, whereas you provided 32000.

The 32000 is OmniRoute's own doing: adjustMaxTokens raises any smaller
max_tokens to DEFAULT_MIN_TOKENS (32000) whenever tools are present, to avoid
truncated tool arguments. That floor has no upper bound, so an agentic client
asking for far less still trips the model ceiling on its first turn.

Add scoped maxOutputCap rules in paramSupport.ts for both Azure wire paths.
PROVIDER_MAX_TOKENS is the wrong lever here - it is provider-wide, and the same
Azure resource also serves GPT-5 deployments with a much higher ceiling.

Regression guard: tests/unit/azure-max-output-clamp.test.ts, which also pins
that the clamp does not leak to gpt-5.1 or to gpt-4o-mini on other providers.

---------

Co-authored-by: Mihaly Bodo <michael@proton-quantum.com>
2026-08-09 09:52:16 -03:00

97 lines
3.4 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
applyAzureParamRules,
AZURE_COMPLETION_TOKEN_DEPLOYMENT,
} from "../../open-sse/executors/azureParamRules.ts";
import { getExecutor, AzureAiExecutor } from "../../open-sse/executors/index.ts";
/**
* Regression guards for two Azure 400s observed against a live Azure AI Foundry
* resource:
*
* azure-ai/gpt-chat-latest
* -> 400 "Unsupported parameter: 'max_tokens' is not supported with this
* model. Use 'max_completion_tokens' instead."
* azure-ai/<any gpt-5 deployment> with tools
* -> 400 "Function tools with reasoning_effort are not supported ...
* Please use /v1/responses instead."
*
* Both rules already existed inline in AzureOpenAIExecutor, so the identical
* deployment succeeded on the `azure-openai` connection and failed on
* `azure-ai`, which routed through the bare DefaultExecutor.
*/
test("gpt-chat-latest converts max_tokens to max_completion_tokens", () => {
const out = applyAzureParamRules(
"gpt-chat-latest",
{ max_tokens: 4096 },
{ max_tokens: 4096, messages: [] }
) as Record<string, unknown>;
assert.equal(out.max_tokens, undefined);
assert.equal(out.max_completion_tokens, 4096);
});
test("gpt-5 family converts max_tokens too", () => {
for (const model of ["gpt-5.1", "gpt-5.4-nano", "my-gpt-5-prod", "o3", "o4-mini"]) {
const out = applyAzureParamRules(model, { max_tokens: 100 }, { max_tokens: 100 }) as Record<
string,
unknown
>;
assert.equal(out.max_tokens, undefined, `${model} should drop max_tokens`);
assert.equal(out.max_completion_tokens, 100, `${model} should set max_completion_tokens`);
}
});
test("reasoning_effort is dropped when tools are present", () => {
const out = applyAzureParamRules(
"gpt-5.1",
{},
{ reasoning_effort: "high", tools: [{ name: "read_file" }] }
) as Record<string, unknown>;
assert.equal(out.reasoning_effort, undefined);
assert.equal((out.tools as unknown[]).length, 1);
});
test("reasoning_effort survives when there are no tools", () => {
const out = applyAzureParamRules("gpt-5.1", {}, { reasoning_effort: "high" }) as Record<
string,
unknown
>;
assert.equal(out.reasoning_effort, "high");
});
test("non-default temperature is dropped, temperature=1 kept", () => {
const dropped = applyAzureParamRules("gpt-5.1", {}, { temperature: 0.7 }) as Record<
string,
unknown
>;
assert.equal(dropped.temperature, undefined);
const kept = applyAzureParamRules("gpt-5.1", {}, { temperature: 1 }) as Record<string, unknown>;
assert.equal(kept.temperature, 1);
});
test("unaffected deployments pass through untouched", () => {
const body = { max_tokens: 500, temperature: 0.2, reasoning_effort: "low" };
const out = applyAzureParamRules("Phi-4", {}, body);
assert.deepEqual(out, body);
});
test("the regex does not match unrelated names by accident", () => {
assert.equal(AZURE_COMPLETION_TOKEN_DEPLOYMENT.test("gpt-4o-mini"), false);
assert.equal(AZURE_COMPLETION_TOKEN_DEPLOYMENT.test("DeepSeek-V4-Flash"), false);
assert.equal(AZURE_COMPLETION_TOKEN_DEPLOYMENT.test("Kimi-K2.7-Code"), false);
});
test("azure-ai resolves to AzureAiExecutor, not the bare DefaultExecutor", () => {
const executor = getExecutor("azure-ai");
assert.ok(
executor instanceof AzureAiExecutor,
"azure-ai must have its own executor so it inherits the Azure param rules"
);
});