Files
9router/tests/unit/param-support.test.js
Anantachoke 7c2b1fe3e1 fix(translator): drop replayed reasoning fields for Groq/Mistral/Cerebras (#4220)
Strict OpenAI-compatible validators reject unknown assistant-message
fields: Groq 400 ("property 'reasoning_content' is unsupported"),
Mistral 422 ("extra_forbidden"), Cerebras 400 ("wrong_api_format").

Clients driving reasoning models (Hermes Agent, and anything following
the DeepSeek/Kimi convention) echo the previous turn's reasoning_content
on every assistant message, so from the second turn on every request to
these providers fails and a fallback combo silently skips them.

Add a dropMessageFields rule to paramSupport.js that strips
reasoning_content / reasoning / reasoning_details from assistant turns
for groq, mistral, and cerebras.
2026-09-21 20:02:21 +07:00

100 lines
3.1 KiB
JavaScript

import { describe, it, expect } from "vitest";
import { stripUnsupportedParams } from "../../open-sse/translator/concerns/paramSupport.js";
describe("stripUnsupportedParams", () => {
it("flattens Cloudflare AI OpenAI content-part arrays", () => {
const body = {
messages: [
{
role: "user",
content: [
{ type: "text", text: "hello " },
{ type: "image_url", image_url: { url: "data:image/png;base64,xx" } },
{ type: "text", text: "world" },
],
},
],
};
expect(() => stripUnsupportedParams("cloudflare-ai", "@cf/meta/llama-3.1-8b-instruct", body)).not.toThrow();
expect(body.messages[0].content).toBe("hello world");
});
it("still drops unsupported GitHub model params", () => {
const body = { temperature: 0.7, top_p: 1 };
stripUnsupportedParams("github", "gpt-5.4", body);
expect(body).toEqual({ top_p: 1 });
});
it("clamps VolcEngine Ark GLM max token fields to the model output ceiling", () => {
const body = {
max_tokens: 131072,
max_completion_tokens: 131072,
max_output_tokens: 131072,
};
stripUnsupportedParams("volcengine-ark", "GLM-5.2", body);
expect(body).toEqual({
max_tokens: 128000,
max_completion_tokens: 128000,
max_output_tokens: 128000,
});
});
it("keeps VolcEngine Ark GLM max tokens when already under the ceiling", () => {
const body = { max_tokens: 64000 };
stripUnsupportedParams("volcengine-ark", "GLM-5.2", body);
expect(body.max_tokens).toBe(64000);
});
it("drops replayed reasoning fields from assistant messages for strict providers", () => {
const makeBody = () => ({
messages: [
{ role: "user", content: "hi" },
{
role: "assistant",
content: "hello",
reasoning_content: "thinking...",
reasoning: "thinking...",
reasoning_details: [{ text: "thinking..." }],
tool_calls: [{ id: "c1", type: "function", function: { name: "f", arguments: "{}" } }],
},
{ role: "user", content: "again", reasoning_content: "user-side field stays" },
],
});
for (const [provider, model] of [
["groq", "openai/gpt-oss-120b"],
["mistral", "codestral-latest"],
["cerebras", "gpt-oss-120b"],
]) {
const body = makeBody();
stripUnsupportedParams(provider, model, body);
expect(body.messages[1]).toEqual({
role: "assistant",
content: "hello",
tool_calls: [{ id: "c1", type: "function", function: { name: "f", arguments: "{}" } }],
});
// only assistant turns are touched
expect(body.messages[2].reasoning_content).toBe("user-side field stays");
}
});
it("leaves reasoning fields alone for providers that accept or require them", () => {
const body = {
messages: [{ role: "assistant", content: "hello", reasoning_content: "thinking..." }],
};
stripUnsupportedParams("deepseek", "deepseek-reasoner", body);
stripUnsupportedParams("openrouter", "nvidia/nemotron-3-ultra-550b-a55b:free", body);
expect(body.messages[0].reasoning_content).toBe("thinking...");
});
});