Skip to content

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

fix(transcription): cap BYOK gpt-transcribe keywords at 900 and send the rest as prompt (#2224) (#2314)

Joshua15de623503ca
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index a154b5438..68e8a2d89 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -116,7 +116,11 @@ import {
payloadSendsDictionaryBias,
} from "../utils/dictionaryEchoFilter.js";
import { dictionaryPromptLimit, trimDictionaryPrompt } from "../utils/dictionaryPromptCap.js";
-import { dictionaryKeywords, usesTranscriptionKeywords } from "../utils/dictionaryKeywords.js";
+import {
+ dictionaryKeywordOverflow,
+ dictionaryKeywords,
+ usesTranscriptionKeywords,
+} from "../utils/dictionaryKeywords.js";
import { getDictionaryHintWords } from "../utils/snippets";
import { normalizeAgentSelectionContext } from "../utils/agentSelectionContext";
import { getAgentName } from "../utils/agentName";
@@ -3717,7 +3721,8 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
const endpoint = this.getTranscriptionEndpoint(route);
// gpt-transcribe takes the dictionary on its own keywords[] channel (see
- // dictionaryKeywords), so its prompt carries only the Chinese script bias.
+ // dictionaryKeywords), so its prompt carries only the Chinese script bias and
+ // the terms past the keyword cap.
const usesKeywords = usesTranscriptionKeywords(model);
const dictionary = this.getCustomDictionaryPrompt();
@@ -3728,7 +3733,10 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
// Whisper decoders read the tail of whatever they are given.
const MAX_PROMPT_CHARS = dictionaryPromptLimit({ provider, endpoint, model });
const trimmedPrompt = trimDictionaryPrompt(
- this.getWhisperPrompt(apiSettings, usesKeywords ? null : dictionary),
+ this.getWhisperPrompt(
+ apiSettings,
+ usesKeywords ? dictionaryKeywordOverflow(dictionary) : dictionary
+ ),
MAX_PROMPT_CHARS
);
const dictionaryPrompt = trimmedPrompt.prompt;
diff --git a/src/utils/dictionaryKeywords.js b/src/utils/dictionaryKeywords.js
index 4d923b774..7d9a04454 100644
--- a/src/utils/dictionaryKeywords.js
+++ b/src/utils/dictionaryKeywords.js
@@ -1,15 +1,26 @@
// gpt-transcribe takes the custom dictionary as one `keywords[]` multipart field
// per term instead of the Whisper-era free-text prompt. OpenAI rejects the whole
-// request when a keyword carries `<`, `>` or a line break. Same rules as the
-// server-side adapter in openwhispr-api (lib/providers/openai.ts).
+// request when a keyword carries `<`, `>` or a line break, or when the form has
+// more than ~1,000 parts ("Could not parse multipart form", #2224). So the first
+// 900 terms go out as keywords and the rest ride in `prompt` as context. Same
+// rules as the server-side adapter in openwhispr-api (lib/providers/openai.ts).
+const MAX_TRANSCRIPTION_KEYWORDS = 900;
export function usesTranscriptionKeywords(model) {
return typeof model === "string" && model.trim() === "gpt-transcribe";
}
-export function dictionaryKeywords(dictionaryPrompt) {
+function dictionaryTerms(dictionaryPrompt) {
return String(dictionaryPrompt ?? "")
.split(/[,\r\n]+/)
.map((term) => term.replace(/[<>]/g, "").trim())
.filter(Boolean);
}
+
+export function dictionaryKeywords(dictionaryPrompt) {
+ return dictionaryTerms(dictionaryPrompt).slice(0, MAX_TRANSCRIPTION_KEYWORDS);
+}
+
+export function dictionaryKeywordOverflow(dictionaryPrompt) {
+ return dictionaryTerms(dictionaryPrompt).slice(MAX_TRANSCRIPTION_KEYWORDS).join(", ") || null;
+}
diff --git a/src/utils/dictionaryPromptCap.js b/src/utils/dictionaryPromptCap.js
index 564df1de0..e6f0cfc94 100644
--- a/src/utils/dictionaryPromptCap.js
+++ b/src/utils/dictionaryPromptCap.js
@@ -1,3 +1,5 @@
+import { usesTranscriptionKeywords } from "./dictionaryKeywords.js";
+
// Per-provider budgets for the custom-dictionary STT prompt on the direct-API
// path, plus the decoder window the dictionary UI warns against. One place, so
// the request bound and the warning cannot drift apart.
@@ -20,6 +22,11 @@ export const WHISPER_PROMPT_CHARS = 900;
// budget is tokens, and no character count is right for every language.
export const WHISPER_DECODER_PROMPT_CHARS = 550;
+// gpt-transcribe's prompt carries only the dictionary terms past its keyword cap
+// (see dictionaryKeywords). OpenAI accepts exactly 65,536 characters there
+// (65,537 is a 400), and neither channel adds measurable latency.
+export const TRANSCRIPTION_OVERFLOW_PROMPT_CHARS = 65536;
+
// gpt-4o-transcribe / gpt-4o-mini-transcribe are LLMs, not Whisper decoders: no
// 223-token prompt window, and verified live to 40k chars. The only hard limit
// is the 16k-token context shared with the audio, so this is a guard against an
@@ -27,13 +34,15 @@ export const WHISPER_DECODER_PROMPT_CHARS = 550;
export const TRANSCRIBE_PROMPT_CHARS = 8000;
// Only the 4o transcribe family is known to read past a Whisper decoder's
-// prompt window, so it alone earns the generous budget. Everything else falls
-// back to the Whisper budget on purpose: custom and self-hosted endpoints take
-// whatever model name the user typed, and most of those servers are
-// Whisper-family under a name that never says "whisper".
+// prompt window, so it earns the generous budget, as does gpt-transcribe's
+// keyword overflow. Everything else falls back to the Whisper budget on
+// purpose: custom and self-hosted endpoints take whatever model name the user
+// typed, and most of those servers are Whisper-family under a name that never
+// says "whisper".
export function dictionaryPromptLimit({ provider = "", endpoint = "", model = "" } = {}) {
if (provider === "groq" || endpoint.includes("api.groq.com")) return GROQ_PROMPT_CHARS;
if (model.toLowerCase().startsWith("gpt-4o")) return TRANSCRIBE_PROMPT_CHARS;
+ if (usesTranscriptionKeywords(model)) return TRANSCRIPTION_OVERFLOW_PROMPT_CHARS;
return WHISPER_PROMPT_CHARS;
}
diff --git a/test/helpers/audioManagerTranscriptionKeywords.test.js b/test/helpers/audioManagerTranscriptionKeywords.test.js
index 5e767249a..3e40ba0e7 100644
--- a/test/helpers/audioManagerTranscriptionKeywords.test.js
+++ b/test/helpers/audioManagerTranscriptionKeywords.test.js
@@ -83,6 +83,85 @@ test("the custom dictionary rides gpt-transcribe's keywords[] channel, legacy mo
assert.deepEqual(requests, [{ prompt: DICTIONARY, keywords: [], stream: "true" }]);
});
+ // Serializes the real request, so part counts include every field it carries.
+ function captureSerialized(st) {
+ const originalFetch = globalThis.fetch;
+ st.after(() => {
+ globalThis.fetch = originalFetch;
+ });
+ const sent = {};
+ globalThis.fetch = async (endpoint, init) => {
+ const request = new Request(endpoint, init);
+ const boundary = request.headers.get("content-type").split("boundary=")[1];
+ const body = await request.text();
+ Object.assign(sent, {
+ parts: body.split(`--${boundary}\r\n`).length - 1,
+ keywords: init.body.getAll("keywords[]"),
+ language: init.body.get("language"),
+ prompt: init.body.get("prompt"),
+ });
+ return {
+ ok: true,
+ status: 200,
+ headers: { get: () => "application/json" },
+ text: async () => JSON.stringify({ text: "transcribed text" }),
+ };
+ };
+ return sent;
+ }
+
+ // #2224: OpenAI rejects a form of more than ~1,000 parts with "Could not parse
+ // multipart form", so a 1,481-term dictionary failed every dictation. zh-CN adds
+ // the language and script-bias prompt parts, the most this request carries.
+ for (const language of ["auto", "zh-CN"]) {
+ await t.test(
+ `a 1,481-term dictionary sends 900 keywords and the rest as prompt (${language})`,
+ async (st) => {
+ const terms = Array.from({ length: 1481 }, (_, i) => `Term${i}`);
+ const sent = captureSerialized(st);
+
+ const result = await manager("gpt-transcribe", {
+ getEffectiveSttLanguage: () => language,
+ getCustomDictionaryPrompt: () => terms.join(", "),
+ }).processWithOpenAIAPI(audioBlob, {});
+
+ assert.equal(result.success, true);
+ assert.ok(sent.parts < 1000, `${sent.parts} multipart parts exceeds OpenAI's form limit`);
+ assert.deepEqual(sent.keywords, terms.slice(0, 900));
+ const overflow = terms.slice(900).join(", ");
+ if (language === "zh-CN") {
+ assert.ok(sent.language, "zh-CN must send the language part");
+ assert.match(sent.prompt, /^以下是简体中文。/, "the script bias must lead the prompt");
+ assert.ok(
+ sent.prompt.endsWith(` ${overflow}`),
+ "the overflow must follow the bias whole"
+ );
+ } else {
+ assert.equal(sent.prompt, overflow);
+ }
+ }
+ );
+ }
+
+ await t.test(
+ "an overflow past OpenAI's 65,536-char prompt limit is cut between terms",
+ async (st) => {
+ // 900 keywords plus 7,000 twelve-character terms: ~98k chars of overflow.
+ const terms = Array.from({ length: 7900 }, (_, i) => `Term${String(i).padStart(8, "0")}`);
+ const sent = captureSerialized(st);
+
+ await manager("gpt-transcribe", {
+ getCustomDictionaryPrompt: () => terms.join(", "),
+ }).processWithOpenAIAPI(audioBlob, {});
+
+ assert.equal(sent.keywords.length, 900);
+ assert.ok(sent.prompt.length <= 65_536, `prompt of ${sent.prompt.length} chars is rejected`);
+ assert.ok(sent.prompt.length > 65_000, "the cut must use the budget, not a smaller one");
+ const promptTerms = sent.prompt.split(", ");
+ assert.deepEqual(promptTerms, terms.slice(900, 900 + promptTerms.length));
+ }
+ );
+
await t.test("whisper-1 keeps the prompt and never streams", async () => {
const requests = captureRequests(t);
await manager("whisper-1").processWithOpenAIAPI(audioBlob, {});
diff --git a/test/helpers/dictionaryKeywords.test.js b/test/helpers/dictionaryKeywords.test.js
index adbee0038..6b65ecef3 100644
--- a/test/helpers/dictionaryKeywords.test.js
+++ b/test/helpers/dictionaryKeywords.test.js
@@ -29,3 +29,21 @@ test("keywords drop the characters OpenAI rejects and any entry left empty", asy
assert.deepEqual(dictionaryKeywords(null), []);
assert.deepEqual(dictionaryKeywords(""), []);
});
+
+test("keywords stop at 900, counted after empty entries are dropped (#2224)", async () => {
+ const { dictionaryKeywords } = await load();
+ const terms = Array.from({ length: 1481 }, (_, i) => `Term${i}`);
+ assert.deepEqual(dictionaryKeywords(", , <>, " + terms.join(", ")), terms.slice(0, 900));
+ assert.deepEqual(dictionaryKeywords(terms.slice(0, 900).join(", ")), terms.slice(0, 900));
+});
+
+test("terms past the keyword cap are comma-joined for the prompt, never dropped (#2224)", async () => {
+ const { dictionaryKeywordOverflow } = await load();
+ const terms = Array.from({ length: 1481 }, (_, i) => `Term${i}`);
+ assert.equal(
+ dictionaryKeywordOverflow(", , <>, " + terms.join(", ")),
+ terms.slice(900).join(", ")
+ );
+ assert.equal(dictionaryKeywordOverflow(terms.slice(0, 900).join(", ")), null);
+ assert.equal(dictionaryKeywordOverflow(null), null);
+});