Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: send audio MIME for Gemini audio sniffed as video container (#19960)

YuTengjing16e67d0d5a4a
diff --git a/packages/model-runtime/src/core/contextBuilders/google.test.ts b/packages/model-runtime/src/core/contextBuilders/google.test.ts
index 5b12a0850e8..a7573d4830b 100644
--- a/packages/model-runtime/src/core/contextBuilders/google.test.ts
+++ b/packages/model-runtime/src/core/contextBuilders/google.test.ts
@@ -507,6 +507,31 @@ describe('google contextBuilders', () => {
});
});
+ it('should send audio MIME when an inlined audio URL is sniffed as a video container', async () => {
+ const audioUrl = 'https://example.com/voice-message.m4a';
+
+ vi.mocked(parseDataUri).mockReturnValueOnce({
+ base64: null,
+ mimeType: null,
+ type: 'url',
+ });
+ vi.mocked(isPublicExternalUrl).mockReturnValueOnce(false);
+ vi.spyOn(imageToBase64Module, 'imageUrlToBase64').mockResolvedValueOnce({
+ base64: 'm4aBase64Data',
+ mimeType: 'video/mp4',
+ });
+
+ const content: UserMessageContentPart = {
+ audio_url: { url: audioUrl },
+ type: 'audio_url',
+ };
+
+ await expect(buildGooglePart(content, { model: 'gemini-3.5-flash-lite' })).resolves.toEqual({
+ inlineData: { data: 'm4aBase64Data', mimeType: 'audio/mp4' },
+ thoughtSignature: GEMINI_MAGIC_THOUGHT_SIGNATURE,
+ });
+ });
+
it('should return undefined for unsupported SVG image (base64)', async () => {
const svgBase64 =
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==';
diff --git a/packages/model-runtime/src/core/contextBuilders/google.ts b/packages/model-runtime/src/core/contextBuilders/google.ts
index 3fd347a2f24..c1447d49af4 100644
--- a/packages/model-runtime/src/core/contextBuilders/google.ts
+++ b/packages/model-runtime/src/core/contextBuilders/google.ts
@@ -75,6 +75,22 @@ const supportsFunctionCallId = (model?: string) => {
return version.major > 3 || (version.major === 3 && version.minor >= 5);
};
+/**
+ * Magic-byte sniffing cannot tell audio-only containers from video ones, so an
+ * `.m4a` voice note (`ftypmp42`) is detected as `video/mp4`. Vertex AI
+ * intermittently rejects such audio-only payloads with 400 INVALID_ARGUMENT,
+ * while `audio/mp4` succeeds consistently.
+ * @see https://linear.app/lobehub/issue/LOBE-13855
+ */
+const AUDIO_CONTAINER_MIME_TYPES: Record<string, string> = {
+ 'video/mp4': 'audio/mp4',
+ 'video/ogg': 'audio/ogg',
+ 'video/webm': 'audio/webm',
+};
+
+const toAudioContainerMimeType = (mimeType: string) =>
+ AUDIO_CONTAINER_MIME_TYPES[mimeType] ?? mimeType;
+
const buildExternalUrlFileDataPart = async (
url: string,
options?: GoogleMessageBuildOptions,
@@ -233,7 +249,7 @@ export const buildGooglePart = async (
: recordedMimeType || urlMimeType || 'audio/mp3';
return {
- inlineData: { data: urlBase64, mimeType: resolvedMimeType },
+ inlineData: { data: urlBase64, mimeType: toAudioContainerMimeType(resolvedMimeType) },
thoughtSignature: GEMINI_MAGIC_THOUGHT_SIGNATURE,
};
}