fix(providers): clamp Cohere reasoning_effort to high and learn its 422 wording (#20363)

This commit is contained in:
diegosouzapw
2026-11-19 04:56:29 -03:00
parent cc59ad70b2
commit 9396d4e3c7
4 changed files with 80 additions and 1 deletions

View File

@@ -0,0 +1 @@
- fix(providers): clamp Cohere reasoning_effort to `high` and learn its "Use 'high' instead" 422 so auto routing no longer burns fallbacks (#20363)

View File

@@ -468,6 +468,9 @@ export function sanitizeReasoningEffortForProvider(
return stripEffortValue(working, c);
}
// Cohere's compat endpoint only accepts "high" (422 otherwise, #20363).
if (provider === "cohere" && effortStr !== "high") return writeEffortValue(working, "high", c);
// ── GLM-5.3 and GLM-5.3-FLASH specific rules ──────────────────────────────
// Supported options: max (default & recommended), high, low.
// none/minimal/low → low; medium/high → high; xhigh/max → max.

View File

@@ -281,7 +281,8 @@ export function recordLearnedProbeReasoningEffort(
// Matches prose shapes: OVH's "@ai-sdk/openai-compatible" deserializer
// ("expected one of `a`, `b`"), generic ("Supported types are a, b, and c"),
// and "please use a, b, or c".
const LIST_INTRO = /(?:expected one of|supported (?:types|values) are|please use)[:\s]*([^.]+)/i;
const LIST_INTRO =
/(?:expected one of|supported (?:types|values) are|please use|use (?=['"`]))[:\s]*([^.]+?)(?:\s+instead\b|\.|$)/i;
/**
* Extract the upstream-advertised accepted reasoning_effort values from a 4xx

View File

@@ -0,0 +1,74 @@
import test from "node:test";
import assert from "node:assert/strict";
import {
parseReasoningEffortEnum,
__test_resetLearnedReasoningEffortCaps,
} from "../../open-sse/services/learnedReasoningEffortCaps.ts";
import { applyReasoningEffortRecovery } from "../../open-sse/executors/base/reasoningEffortRecovery.ts";
import { sanitizeReasoningEffortForProvider } from "../../open-sse/executors/base/reasoningEffort.ts";
const COHERE_422 = {
error: {
message:
"unprocessable entity: reasoning_effort 'medium' and 'low' are not supported. Use 'high' instead",
type: "upstream_error",
code: 422,
},
};
test.beforeEach(() => {
delete process.env.OMNIROUTE_REASONING_EFFORT_PROBE_PROVIDERS;
__test_resetLearnedReasoningEffortCaps();
});
test("#20363 parser reads Cohere's \"Use 'high' instead\" wording", () => {
assert.deepEqual(parseReasoningEffortEnum(JSON.stringify(COHERE_422)), ["high"]);
});
test("#20363 parser keeps existing wordings", () => {
assert.deepEqual(parseReasoningEffortEnum("Supported values are: low, medium, high."), [
"low",
"medium",
"high",
]);
});
test("#20363 reactive recovery retries a Cohere 422 with reasoning_effort high", async () => {
const sent: Array<{ reasoning_effort?: string }> = [];
const body = { model: "command-a-plus-05-2026", reasoning_effort: "medium", messages: [] };
const res = await applyReasoningEffortRecovery({
response: new Response(JSON.stringify(COHERE_422), { status: 422 }),
url: "https://api.cohere.com/compatibility/v1/chat/completions",
provider: "cohere",
model: "command-a-plus-05-2026",
body,
fetchOptions: { method: "POST", body: JSON.stringify(body) },
fetchFn: async (_u: unknown, o: { body?: unknown }) => {
sent.push(JSON.parse(String(o.body)));
return new Response("{}", { status: 200 });
},
} as unknown as Parameters<typeof applyReasoningEffortRecovery>[0]);
assert.equal(res.retried, true);
assert.equal(sent[0]?.reasoning_effort, "high");
});
test("#20363 proactive sanitize clamps every tier to high for cohere", () => {
for (const eff of ["none", "minimal", "low", "medium", "high", "xhigh", "max"]) {
const out = sanitizeReasoningEffortForProvider(
{ model: "command-a-plus-05-2026", reasoning_effort: eff },
"cohere",
"command-a-plus-05-2026"
) as { reasoning_effort?: string };
assert.equal(out.reasoning_effort, "high", `effort ${eff}`);
}
});
test("#20363 other providers are untouched", () => {
const out = sanitizeReasoningEffortForProvider(
{ model: "gpt-5", reasoning_effort: "low" },
"openai",
"gpt-5"
) as { reasoning_effort?: string };
assert.equal(out.reasoning_effort, "low");
});