Skip to content

Commit 8b70ba6

Browse files
Glucksbergsteipete
authored andcommitted
fix(#8127): auto-skip tiny/empty audio files in whisper transcription
Add a minimum file size guard (MIN_AUDIO_FILE_BYTES = 1024) before sending audio to transcription APIs. Files below this threshold are almost certainly empty or corrupt and would cause unhelpful errors from Whisper/Deepgram/Groq providers. Changes: - Add 'tooSmall' skip reason to MediaUnderstandingSkipError - Add MIN_AUDIO_FILE_BYTES constant (1024 bytes) to defaults - Guard both provider and CLI audio paths in runner.ts - Add comprehensive tests for tiny, empty, and valid audio files - Update existing test fixtures to use audio files above threshold
1 parent 036bd18 commit 8b70ba6

4 files changed

Lines changed: 214 additions & 1 deletion

File tree

‎src/media-understanding/defaults.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ export const DEFAULT_IMAGE_MODELS: Record<string, string> = {
5858
};
5959
export const CLI_OUTPUT_MAX_BUFFER = 5 * MB;
6060
export const DEFAULT_MEDIA_CONCURRENCY = 2;
61+
62+
/**
63+
* Minimum audio file size in bytes below which transcription is skipped.
64+
* Files smaller than this threshold are almost certainly empty or corrupt
65+
* and would cause unhelpful API errors from Whisper/transcription providers.
66+
*/
67+
export const MIN_AUDIO_FILE_BYTES = 1024;

‎src/media-understanding/errors.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export type MediaUnderstandingSkipReason = "maxBytes" | "timeout" | "unsupported" | "empty";
1+
export type MediaUnderstandingSkipReason =
2+
| "maxBytes"
3+
| "timeout"
4+
| "unsupported"
5+
| "empty"
6+
| "tooSmall";
27

38
export class MediaUnderstandingSkipError extends Error {
49
readonly reason: MediaUnderstandingSkipReason;

‎src/media-understanding/runner.entries.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
CLI_OUTPUT_MAX_BUFFER,
2222
DEFAULT_AUDIO_MODELS,
2323
DEFAULT_TIMEOUT_SECONDS,
24+
MIN_AUDIO_FILE_BYTES,
2425
} from "./defaults.js";
2526
import { MediaUnderstandingSkipError } from "./errors.js";
2627
import { fileExists } from "./fs.js";
@@ -446,6 +447,12 @@ export async function runProviderEntry(params: {
446447
maxBytes,
447448
timeoutMs,
448449
});
450+
if (media.size < MIN_AUDIO_FILE_BYTES) {
451+
throw new MediaUnderstandingSkipError(
452+
"tooSmall",
453+
`Audio attachment ${params.attachmentIndex + 1} is too small (${media.size} bytes, minimum ${MIN_AUDIO_FILE_BYTES})`,
454+
);
455+
}
449456
const { apiKeys, baseUrl, headers } = await resolveProviderExecutionContext({
450457
providerId,
451458
cfg,
@@ -563,6 +570,15 @@ export async function runCliEntry(params: {
563570
maxBytes,
564571
timeoutMs,
565572
});
573+
if (capability === "audio") {
574+
const stat = await fs.stat(pathResult.path);
575+
if (stat.size < MIN_AUDIO_FILE_BYTES) {
576+
throw new MediaUnderstandingSkipError(
577+
"tooSmall",
578+
`Audio attachment ${params.attachmentIndex + 1} is too small (${stat.size} bytes, minimum ${MIN_AUDIO_FILE_BYTES})`,
579+
);
580+
}
581+
}
566582
const outputDir = await fs.mkdtemp(
567583
path.join(resolvePreferredOpenClawTmpDir(), "openclaw-media-cli-"),
568584
);
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import fs from "node:fs/promises";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { describe, expect, it } from "vitest";
5+
import type { MsgContext } from "../auto-reply/templating.js";
6+
import type { OpenClawConfig } from "../config/config.js";
7+
import { MIN_AUDIO_FILE_BYTES } from "./defaults.js";
8+
import {
9+
buildProviderRegistry,
10+
createMediaAttachmentCache,
11+
normalizeMediaAttachments,
12+
runCapability,
13+
} from "./runner.js";
14+
15+
describe("runCapability skips tiny audio files", () => {
16+
it("skips audio transcription when file is smaller than MIN_AUDIO_FILE_BYTES", async () => {
17+
const originalPath = process.env.PATH;
18+
process.env.PATH = "/usr/bin:/bin";
19+
20+
// Create a tiny audio file (well below the 1KB threshold)
21+
const tmpPath = path.join(os.tmpdir(), `openclaw-tiny-audio-${Date.now()}.wav`);
22+
const tinyBuffer = Buffer.alloc(100); // 100 bytes, way below 1024
23+
await fs.writeFile(tmpPath, tinyBuffer);
24+
25+
const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" };
26+
const media = normalizeMediaAttachments(ctx);
27+
const cache = createMediaAttachmentCache(media);
28+
29+
let transcribeCalled = false;
30+
const providerRegistry = buildProviderRegistry({
31+
openai: {
32+
id: "openai",
33+
capabilities: ["audio"],
34+
transcribeAudio: async (req) => {
35+
transcribeCalled = true;
36+
return { text: "should not happen", model: req.model };
37+
},
38+
},
39+
});
40+
41+
const cfg = {
42+
models: {
43+
providers: {
44+
openai: {
45+
apiKey: "test-key",
46+
models: [],
47+
},
48+
},
49+
},
50+
} as unknown as OpenClawConfig;
51+
52+
try {
53+
const result = await runCapability({
54+
capability: "audio",
55+
cfg,
56+
ctx,
57+
attachments: cache,
58+
media,
59+
providerRegistry,
60+
});
61+
62+
// The provider should never be called
63+
expect(transcribeCalled).toBe(false);
64+
65+
// The result should indicate the attachment was skipped
66+
expect(result.outputs).toHaveLength(0);
67+
expect(result.decision.outcome).toBe("skipped");
68+
expect(result.decision.attachments[0]?.attempts[0]?.outcome).toBe("skipped");
69+
expect(result.decision.attachments[0]?.attempts[0]?.reason).toContain("tooSmall");
70+
} finally {
71+
process.env.PATH = originalPath;
72+
await cache.cleanup();
73+
await fs.unlink(tmpPath).catch(() => {});
74+
}
75+
});
76+
77+
it("skips audio transcription for empty (0-byte) files", async () => {
78+
const originalPath = process.env.PATH;
79+
process.env.PATH = "/usr/bin:/bin";
80+
81+
const tmpPath = path.join(os.tmpdir(), `openclaw-empty-audio-${Date.now()}.ogg`);
82+
await fs.writeFile(tmpPath, Buffer.alloc(0));
83+
84+
const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/ogg" };
85+
const media = normalizeMediaAttachments(ctx);
86+
const cache = createMediaAttachmentCache(media);
87+
88+
let transcribeCalled = false;
89+
const providerRegistry = buildProviderRegistry({
90+
openai: {
91+
id: "openai",
92+
capabilities: ["audio"],
93+
transcribeAudio: async () => {
94+
transcribeCalled = true;
95+
return { text: "nope", model: "whisper-1" };
96+
},
97+
},
98+
});
99+
100+
const cfg = {
101+
models: {
102+
providers: {
103+
openai: {
104+
apiKey: "test-key",
105+
models: [],
106+
},
107+
},
108+
},
109+
} as unknown as OpenClawConfig;
110+
111+
try {
112+
const result = await runCapability({
113+
capability: "audio",
114+
cfg,
115+
ctx,
116+
attachments: cache,
117+
media,
118+
providerRegistry,
119+
});
120+
121+
expect(transcribeCalled).toBe(false);
122+
expect(result.outputs).toHaveLength(0);
123+
} finally {
124+
process.env.PATH = originalPath;
125+
await cache.cleanup();
126+
await fs.unlink(tmpPath).catch(() => {});
127+
}
128+
});
129+
130+
it("proceeds with transcription when file meets minimum size", async () => {
131+
const originalPath = process.env.PATH;
132+
process.env.PATH = "/usr/bin:/bin";
133+
134+
const tmpPath = path.join(os.tmpdir(), `openclaw-ok-audio-${Date.now()}.wav`);
135+
const okBuffer = Buffer.alloc(MIN_AUDIO_FILE_BYTES + 100);
136+
await fs.writeFile(tmpPath, okBuffer);
137+
138+
const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" };
139+
const media = normalizeMediaAttachments(ctx);
140+
const cache = createMediaAttachmentCache(media);
141+
142+
let transcribeCalled = false;
143+
const providerRegistry = buildProviderRegistry({
144+
openai: {
145+
id: "openai",
146+
capabilities: ["audio"],
147+
transcribeAudio: async (req) => {
148+
transcribeCalled = true;
149+
return { text: "hello world", model: req.model };
150+
},
151+
},
152+
});
153+
154+
const cfg = {
155+
models: {
156+
providers: {
157+
openai: {
158+
apiKey: "test-key",
159+
models: [],
160+
},
161+
},
162+
},
163+
} as unknown as OpenClawConfig;
164+
165+
try {
166+
const result = await runCapability({
167+
capability: "audio",
168+
cfg,
169+
ctx,
170+
attachments: cache,
171+
media,
172+
providerRegistry,
173+
});
174+
175+
expect(transcribeCalled).toBe(true);
176+
expect(result.outputs).toHaveLength(1);
177+
expect(result.outputs[0]?.text).toBe("hello world");
178+
expect(result.decision.outcome).toBe("success");
179+
} finally {
180+
process.env.PATH = originalPath;
181+
await cache.cleanup();
182+
await fs.unlink(tmpPath).catch(() => {});
183+
}
184+
});
185+
});

0 commit comments

Comments
 (0)