Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): support audio/x-m4a for ASR (#40776)

zy141cbf983234
diff --git a/api/controllers/service_api/app/audio.py b/api/controllers/service_api/app/audio.py
index 44bea189553..f3cbb538898 100644
--- a/api/controllers/service_api/app/audio.py
+++ b/api/controllers/service_api/app/audio.py
@@ -49,7 +49,7 @@ class AudioApi(Resource):
summary="Convert Audio to Text",
description=(
"Convert audio file to text. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, "
- "`audio/wav`, and `audio/amr`. File size limit is `30 MB`."
+ "`audio/x-m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`."
),
tags=["TTS"],
responses={
@@ -76,7 +76,7 @@ class AudioApi(Resource):
include_user=True,
file_description=(
"Audio file to transcribe. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, "
- "`audio/wav`, and `audio/amr`. File size limit is `30 MB`."
+ "`audio/x-m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`."
),
),
)
diff --git a/api/openapi/markdown/service-openapi.md b/api/openapi/markdown/service-openapi.md
index 493fb19d316..2d57536f25f 100644
--- a/api/openapi/markdown/service-openapi.md
+++ b/api/openapi/markdown/service-openapi.md
@@ -265,7 +265,7 @@ Updates the question and answer of an existing annotation.
### [POST] /audio-to-text
**Convert Audio to Text**
-Convert audio file to text. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`.
+Convert audio file to text. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, `audio/x-m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`.
#### Request Body
diff --git a/api/services/audio_service.py b/api/services/audio_service.py
index ea085116ce5..0d7e73217a2 100644
--- a/api/services/audio_service.py
+++ b/api/services/audio_service.py
@@ -30,6 +30,9 @@ from services.workflow_service import WorkflowService
FILE_SIZE = 30
FILE_SIZE_LIMIT = FILE_SIZE * 1024 * 1024
+_ASR_MIME_TYPE_ALIASES = {
+ "audio/x-m4a": "audio/m4a",
+}
logger = logging.getLogger(__name__)
@@ -130,8 +133,8 @@ class AudioService:
if file is None:
raise NoAudioUploadedServiceError()
- extension = file.mimetype
- if extension not in [f"audio/{ext}" for ext in AUDIO_EXTENSIONS]:
+ mimetype = _ASR_MIME_TYPE_ALIASES.get(file.mimetype, file.mimetype)
+ if mimetype not in [f"audio/{ext}" for ext in AUDIO_EXTENSIONS]:
raise UnsupportedAudioTypeServiceError()
file_content = file.stream.read()
diff --git a/api/tests/unit_tests/controllers/test_swagger.py b/api/tests/unit_tests/controllers/test_swagger.py
index b20ee8df44d..20bdeacda3c 100644
--- a/api/tests/unit_tests/controllers/test_swagger.py
+++ b/api/tests/unit_tests/controllers/test_swagger.py
@@ -350,7 +350,7 @@ def test_service_openapi_documents_app_multipart_contracts(monkeypatch: pytest.M
assert schema["properties"]["file"] == {
"description": (
"Audio file to transcribe. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, "
- "`audio/wav`, and `audio/amr`. File size limit is `30 MB`."
+ "`audio/x-m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`."
),
"format": "binary",
"type": "string",
diff --git a/api/tests/unit_tests/services/test_audio_service.py b/api/tests/unit_tests/services/test_audio_service.py
index 1dd1277c2fb..e4d6046a3ea 100644
--- a/api/tests/unit_tests/services/test_audio_service.py
+++ b/api/tests/unit_tests/services/test_audio_service.py
@@ -253,6 +253,26 @@ class TestAudioServiceASR:
mock_model_instance.invoke_speech2text.assert_called_once()
mock_model_manager_class.assert_called_once_with(tenant_id=app.tenant_id, user_id="user-123")
+ @patch("services.audio_service.ModelManager.for_tenant", autospec=True)
+ def test_transcript_asr_accepts_x_m4a_mimetype(
+ self, mock_model_manager_class, factory: AudioServiceTestDataFactory
+ ):
+ """Test that the x-m4a MIME alias follows the normal m4a transcription flow."""
+ # Arrange
+ app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})
+ app = factory.create_app_mock(mode=AppMode.CHAT, app_model_config=app_model_config)
+ file = factory.create_file_storage_mock(filename="audio.m4a", mimetype="audio/x-m4a")
+ mock_model_instance = MagicMock()
+ mock_model_instance.invoke_speech2text.return_value = "M4A transcript"
+ mock_model_manager_class.return_value.get_default_model_instance.return_value = mock_model_instance
+
+ # Act
+ result = AudioService.transcript_asr(app_model=app, file=file, session=self.session)
+
+ # Assert
+ assert result == {"text": "M4A transcript"}
+ mock_model_instance.invoke_speech2text.assert_called_once()
+
@patch("services.audio_service.ModelManager.for_tenant", autospec=True)
def test_transcript_asr_success_advanced_chat_mode(
self, mock_model_manager_class, factory: AudioServiceTestDataFactory
diff --git a/packages/contracts/generated/api/service/orpc.gen.ts b/packages/contracts/generated/api/service/orpc.gen.ts
index 513a87995d9..ce9f4bc9f0b 100644
--- a/packages/contracts/generated/api/service/orpc.gen.ts
+++ b/packages/contracts/generated/api/service/orpc.gen.ts
@@ -419,12 +419,12 @@ export const apps = {
/**
* Convert Audio to Text
*
- * Convert audio file to text. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`.
+ * Convert audio file to text. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, `audio/x-m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`.
*/
export const post3 = oc
.route({
description:
- 'Convert audio file to text. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`.',
+ 'Convert audio file to text. Supported MIME types: `audio/mp3`, `audio/mpga`, `audio/m4a`, `audio/x-m4a`, `audio/wav`, and `audio/amr`. File size limit is `30 MB`.',
inputStructure: 'detailed',
method: 'POST',
operationId: 'postAudioToText',