Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(reasoning): stop Mistral cleanup requests failing with 422

Alcahest287e62dea10f
diff --git a/src/services/ai/apiErrorMessage.ts b/src/services/ai/apiErrorMessage.ts
new file mode 100644
index 000000000..2eb6f9bdc
--- /dev/null
+++ b/src/services/ai/apiErrorMessage.ts
@@ -0,0 +1,66 @@
+/**
+ * Reads a human-readable message out of an OpenAI-compatible error body. Kept free
+ * of runtime imports so the shape table stays unit-testable on its own.
+ */
+const MAX_STRINGIFIED_LENGTH = 500;
+
+function asNonEmptyString(value: unknown): string | null {
+ return typeof value === "string" && value.trim() ? value.trim() : null;
+}
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return typeof value === "object" && value !== null && !Array.isArray(value);
+}
+
+/** FastAPI/Pydantic validation errors: {detail: [{loc, msg}]}. */
+function fromDetailList(message: unknown): string | null {
+ if (!isRecord(message) || !Array.isArray(message.detail)) return null;
+
+ const parts: string[] = [];
+ for (const entry of message.detail) {
+ if (!isRecord(entry)) continue;
+ const msg = asNonEmptyString(entry.msg);
+ if (!msg) continue;
+ const loc = Array.isArray(entry.loc) ? entry.loc : [];
+ const field = asNonEmptyString(loc.length ? loc[loc.length - 1] : null);
+ parts.push(field ? `${field}: ${msg}` : msg);
+ }
+
+ return parts.length ? parts.join("; ") : null;
+}
+
+function stringify(value: unknown): string | null {
+ try {
+ const json = JSON.stringify(value);
+ if (!json) return null;
+ return json.length > MAX_STRINGIFIED_LENGTH
+ ? `${json.slice(0, MAX_STRINGIFIED_LENGTH)}…`
+ : json;
+ } catch {
+ return null;
+ }
+}
+
+export function extractApiErrorMessage(errorData: unknown, fallback: string): string {
+ const safeFallback = asNonEmptyString(fallback) || "Unknown API error";
+ if (!isRecord(errorData)) return safeFallback;
+
+ const nested = isRecord(errorData.error) ? asNonEmptyString(errorData.error.message) : null;
+ if (nested) return nested;
+
+ const flat = asNonEmptyString(errorData.message);
+ if (flat) return flat;
+
+ const detail = fromDetailList(errorData.message);
+ if (detail) return detail;
+
+ const errorString = asNonEmptyString(errorData.error);
+ if (errorString) return errorString;
+
+ if (isRecord(errorData.message) || Array.isArray(errorData.message)) {
+ const stringified = stringify(errorData.message);
+ if (stringified) return stringified;
+ }
+
+ return safeFallback;
+}
diff --git a/src/services/ai/inferenceProviders/openai.ts b/src/services/ai/inferenceProviders/openai.ts
index b769e5ec9..aacf52470 100644
--- a/src/services/ai/inferenceProviders/openai.ts
+++ b/src/services/ai/inferenceProviders/openai.ts
@@ -6,6 +6,8 @@ import { withRetry, createApiRetryStrategy } from "../../../utils/retry";
import logger from "../../../utils/logger";
import { getConfiguredOpenAIBase } from "../openaiBase";
import { applyThinkingSuppression } from "../thinkingSuppression";
+import { detectEndpointDialect } from "../thinkingSuppressionDialects";
+import { extractApiErrorMessage } from "../apiErrorMessage";
import { wrapCleanupTranscript } from "../../../config/prompts";
const OPENAI_ENDPOINT_PREF_STORAGE_KEY = "openAiEndpointPreference";
@@ -161,6 +163,7 @@ export const openaiProvider: InferenceProvider = {
endpointCandidates = getEndpointCandidates(openAiBase);
}
const isCustomEndpoint = openAiBase !== API_ENDPOINTS.OPENAI_BASE;
+ const dialect = detectEndpointDialect(openAiBase);
logger.logReasoning("OPENAI_ENDPOINTS", {
base: openAiBase,
@@ -198,7 +201,8 @@ export const openaiProvider: InferenceProvider = {
)
);
- const apiConfig = getOpenAiApiConfig(model, resolvedProvider);
+ // A known endpoint host knows its own request shape better than the model id does.
+ const apiConfig = dialect ?? getOpenAiApiConfig(model, resolvedProvider);
const requestBody: Record<string, unknown> = { model };
if (type === "responses") {
@@ -211,7 +215,7 @@ export const openaiProvider: InferenceProvider = {
if (!config.systemPrompt && model.includes("gpt-oss")) {
requestBody.reasoning_effort = "low";
}
- applyThinkingSuppression(requestBody, model, resolvedProvider, config);
+ applyThinkingSuppression(requestBody, model, resolvedProvider, config, openAiBase);
}
if (apiConfig.supportsTemperature) {
@@ -230,8 +234,10 @@ export const openaiProvider: InferenceProvider = {
if (!res.ok) {
const errorData = await res.json().catch(() => ({ error: res.statusText }));
- const errorMessage =
- errorData.error?.message || errorData.message || `OpenAI API error: ${res.status}`;
+ const errorMessage = extractApiErrorMessage(
+ errorData,
+ `OpenAI API error: ${res.status}`
+ );
const isUnsupportedEndpoint =
(res.status === 404 || res.status === 405) && type === "responses";
diff --git a/src/services/ai/thinkingSuppression.ts b/src/services/ai/thinkingSuppression.ts
index c375e384b..d65076ec3 100644
--- a/src/services/ai/thinkingSuppression.ts
+++ b/src/services/ai/thinkingSuppression.ts
@@ -1,14 +1,16 @@
import type { ReasoningConfig } from "../BaseReasoningService";
import { getCloudModel, getLocalModel } from "../../models/ModelRegistry";
-import { suppressThinking } from "./thinkingSuppressionDialects";
+import { detectEndpointDialect, suppressThinking } from "./thinkingSuppressionDialects";
export function applyThinkingSuppression(
requestBody: Record<string, unknown>,
model: string,
provider: string,
- config: ReasoningConfig
+ config: ReasoningConfig,
+ baseUrl?: string
): void {
- const providerKey = provider.toLowerCase();
+ // A known endpoint host wins over the generic provider dialect.
+ const providerKey = detectEndpointDialect(baseUrl)?.key ?? provider.toLowerCase();
const cloudModel = getCloudModel(model);
if (cloudModel?.disableThinking && providerKey === "groq") {
diff --git a/src/services/ai/thinkingSuppressionDialects.ts b/src/services/ai/thinkingSuppressionDialects.ts
index b9b980155..7e6ae588f 100644
--- a/src/services/ai/thinkingSuppressionDialects.ts
+++ b/src/services/ai/thinkingSuppressionDialects.ts
@@ -2,6 +2,31 @@
* Per-provider dialects for turning a model's thinking off. Kept free of runtime
* imports so the dialect table stays unit-testable on its own.
*/
+export interface EndpointDialect {
+ key: "mistral";
+ tokenParam: "max_tokens";
+ supportsTemperature: true;
+}
+
+/** Custom endpoints that need their own request shape, recognised by host. */
+export function detectEndpointDialect(baseUrl: string | null | undefined): EndpointDialect | null {
+ if (!baseUrl) return null;
+
+ let host: string;
+ try {
+ const normalized = baseUrl.includes("://") ? baseUrl : `https://${baseUrl}`;
+ host = new URL(normalized).hostname.toLowerCase();
+ } catch {
+ return null;
+ }
+
+ if (host === "mistral.ai" || host.endsWith(".mistral.ai")) {
+ return { key: "mistral", tokenParam: "max_tokens", supportsTemperature: true };
+ }
+
+ return null;
+}
+
export function suppressThinking(
requestBody: Record<string, unknown>,
providerKey: string,
@@ -33,6 +58,14 @@ export function suppressThinking(
return;
}
+ // Mistral rejects unknown fields with a 422; reasoning_effort is its native switch.
+ if (providerKey === "mistral") {
+ // Legacy magistral models reason natively and may reject reasoning_effort.
+ if ((model || "").toLowerCase().includes("magistral")) return;
+ requestBody.reasoning_effort = "none";
+ return;
+ }
+
if (providerKey === "local") {
requestBody.think = false;
} else if (providerKey === "lan") {
diff --git a/test/helpers/apiErrorMessage.test.js b/test/helpers/apiErrorMessage.test.js
new file mode 100644
index 000000000..d34525a20
--- /dev/null
+++ b/test/helpers/apiErrorMessage.test.js
@@ -0,0 +1,125 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+// Requires Node's native TypeScript type-stripping (Node >= 22.6 with
+// --experimental-strip-types, on by default in Node 23.6+/24). CI runs Node 24.
+
+const load = () => import("../../src/services/ai/apiErrorMessage.ts");
+
+test("a mistral 422 validation body names every rejected field instead of [object Object]", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ const body = {
+ object: "error",
+ message: {
+ detail: [
+ {
+ type: "extra_forbidden",
+ loc: ["body", "max_completion_tokens"],
+ msg: "Extra inputs are not permitted",
+ input: 4096,
+ },
+ {
+ type: "extra_forbidden",
+ loc: ["body", "chat_template_kwargs"],
+ msg: "Extra inputs are not permitted",
+ input: { enable_thinking: false },
+ },
+ ],
+ },
+ type: "invalid_request_error",
+ };
+
+ const message = extractApiErrorMessage(body, "fallback");
+
+ assert.equal(
+ message,
+ "max_completion_tokens: Extra inputs are not permitted; chat_template_kwargs: Extra inputs are not permitted"
+ );
+ assert.ok(!message.includes("[object Object]"));
+});
+
+test("the openai error shape returns its nested message", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ assert.equal(
+ extractApiErrorMessage({ error: { message: "Invalid API key" } }, "fallback"),
+ "Invalid API key"
+ );
+});
+
+test("a plain string message is returned as is", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ assert.equal(extractApiErrorMessage({ message: "Rate limited" }, "fallback"), "Rate limited");
+});
+
+test("an object message without a detail list is stringified rather than coerced", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ const message = extractApiErrorMessage({ message: { weird: "object" } }, "fallback");
+
+ assert.equal(message, '{"weird":"object"}');
+ assert.ok(!message.includes("[object Object]"));
+});
+
+test("a long stringified message is capped", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ const message = extractApiErrorMessage({ message: { blob: "x".repeat(2000) } }, "fallback");
+
+ assert.ok(message.length <= 501, `expected a capped message, got ${message.length} chars`);
+});
+
+test("detail entries with missing or non-string msg and loc degrade gracefully", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ const body = {
+ message: {
+ detail: [
+ { loc: ["body", "field_a"], msg: 42 },
+ { loc: [], msg: "Field is required" },
+ { loc: ["body", { nested: true }], msg: "Bad nesting" },
+ { msg: "No loc at all" },
+ "not an object",
+ null,
+ ],
+ },
+ };
+
+ assert.equal(
+ extractApiErrorMessage(body, "fallback"),
+ "Field is required; Bad nesting; No loc at all"
+ );
+});
+
+test("a detail list with nothing usable still surfaces the raw body over [object Object]", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ assert.equal(extractApiErrorMessage({ message: { detail: [] } }, "fallback"), '{"detail":[]}');
+});
+
+test("a bare error string is used when there is no message", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ assert.equal(
+ extractApiErrorMessage({ error: "Service Unavailable" }, "fallback"),
+ "Service Unavailable"
+ );
+});
+
+test("non-object bodies fall back to the caller's message", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ assert.equal(extractApiErrorMessage(null, "fallback"), "fallback");
+ assert.equal(extractApiErrorMessage("just a string", "fallback"), "fallback");
+ assert.equal(extractApiErrorMessage([], "fallback"), "fallback");
+ assert.equal(extractApiErrorMessage({}, "fallback"), "fallback");
+ assert.equal(extractApiErrorMessage(undefined, "fallback"), "fallback");
+});
+
+test("an empty fallback still yields a non-empty message", async () => {
+ const { extractApiErrorMessage } = await load();
+
+ assert.equal(extractApiErrorMessage({}, ""), "Unknown API error");
+});
diff --git a/test/helpers/thinkingSuppressionDialects.test.js b/test/helpers/thinkingSuppressionDialects.test.js
index 3a86610de..2be120613 100644
--- a/test/helpers/thinkingSuppressionDialects.test.js
+++ b/test/helpers/thinkingSuppressionDialects.test.js
@@ -106,3 +106,68 @@ test("unlisted providers keep the legacy reasoning_effort none plus chat_templat
chat_template_kwargs: { enable_thinking: false },
});
});
+
+test("mistral gets reasoning_effort none and never chat_template_kwargs", async () => {
+ const { suppressThinking } = await load();
+
+ const body = {};
+ suppressThinking(body, "mistral", "mistral-small-latest");
+
+ assert.deepEqual(body, { reasoning_effort: "none" });
+ assert.ok(!("chat_template_kwargs" in body), "Mistral rejects chat_template_kwargs with a 422");
+});
+
+test("mistral magistral models are left untouched because they reason natively", async () => {
+ const { suppressThinking } = await load();
+
+ const body = { model: "magistral-medium-latest", messages: [] };
+ suppressThinking(body, "mistral", "Magistral-Medium-Latest");
+
+ assert.deepEqual(body, { model: "magistral-medium-latest", messages: [] });
+});
+
+test("mistral tolerates a missing model without throwing", async () => {
+ const { suppressThinking } = await load();
+
+ const body = {};
+ suppressThinking(body, "mistral", undefined);
+
+ assert.deepEqual(body, { reasoning_effort: "none" });
+});
+
+test("detectEndpointDialect maps the mistral api base to max_tokens and temperature", async () => {
+ const { detectEndpointDialect } = await load();
+
+ assert.deepEqual(detectEndpointDialect("https://api.mistral.ai/v1"), {
+ key: "mistral",
+ tokenParam: "max_tokens",
+ supportsTemperature: true,
+ });
+});
+
+test("detectEndpointDialect matches mistral hosts regardless of scheme, case, port or path", async () => {
+ const { detectEndpointDialect } = await load();
+
+ assert.equal(detectEndpointDialect("api.mistral.ai/v1")?.key, "mistral");
+ assert.equal(detectEndpointDialect("https://mistral.ai")?.key, "mistral");
+ assert.equal(detectEndpointDialect("https://API.Mistral.AI/v1/")?.key, "mistral");
+ assert.equal(detectEndpointDialect("https://api.mistral.ai:443/v1")?.key, "mistral");
+ assert.equal(detectEndpointDialect("https://[email protected]/v1")?.key, "mistral");
+});
+
+test("detectEndpointDialect rejects lookalike hosts", async () => {
+ const { detectEndpointDialect } = await load();
+
+ assert.equal(detectEndpointDialect("https://api.openai.com/v1"), null);
+ assert.equal(detectEndpointDialect("https://notmistral.ai"), null);
+ assert.equal(detectEndpointDialect("https://mistral.ai.evil.com"), null);
+});
+
+test("detectEndpointDialect returns null for unparseable or missing input", async () => {
+ const { detectEndpointDialect } = await load();
+
+ assert.equal(detectEndpointDialect("::::"), null);
+ assert.equal(detectEndpointDialect(""), null);
+ assert.equal(detectEndpointDialect(undefined), null);
+ assert.equal(detectEndpointDialect(null), null);
+});