From 50689f15f4acbd9f3b36fe28df34e0a2a0cc896e Mon Sep 17 00:00:00 2001 From: Ting Sun Date: Sat, 4 Jul 2026 00:16:02 +0800 Subject: [PATCH] Accept scalar audio formats in Voxtral transcription requests --- .../models/voxtral/processing_voxtral.py | 10 ++++++++++ tests/models/voxtral/test_modeling_voxtral.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/transformers/models/voxtral/processing_voxtral.py b/src/transformers/models/voxtral/processing_voxtral.py index 5757a490692a..4bd9e04e04db 100644 --- a/src/transformers/models/voxtral/processing_voxtral.py +++ b/src/transformers/models/voxtral/processing_voxtral.py @@ -329,10 +329,20 @@ def apply_transcription_request( ] else: audio = make_list_of_audio(audio) + if format is None: + raise ValueError("`format` must be provided when passing audio arrays to VoxtralProcessor.") + + if isinstance(format, str): + format = [format] * len(audio) + if len(audio) != len(format): raise ValueError( f"When passed as a list of audio, the length ({len(audio)}) must match the number of format ({len(format)})" ) + + if not is_soundfile_available(): + raise ImportError("Please install `soundfile` to encode audio arrays with VoxtralProcessor.") + audio_buffers = [] for array, f in zip(audio, format): # Create new BytesIO object and write audio data to it diff --git a/tests/models/voxtral/test_modeling_voxtral.py b/tests/models/voxtral/test_modeling_voxtral.py index deeed284bc5d..fb03e3983cb3 100644 --- a/tests/models/voxtral/test_modeling_voxtral.py +++ b/tests/models/voxtral/test_modeling_voxtral.py @@ -15,6 +15,8 @@ import unittest +import numpy as np + from transformers import ( AutoProcessor, LlamaConfig, @@ -27,10 +29,12 @@ from transformers.testing_utils import ( Expectations, cleanup, + require_mistral_common, require_torch, slow, torch_device, ) +from transformers.utils import is_soundfile_available from ...alm_tester import ALMModelTest, ALMModelTester @@ -380,6 +384,20 @@ def test_mini_multi_turn_text_and_audio(self): ] self.assertEqual(decoded_outputs, EXPECTED_OUTPUT) + @slow + @require_mistral_common + @unittest.skipUnless(is_soundfile_available(), "test requires soundfile") + def test_transcription_request_accepts_single_format_for_array_audio(self): + processor = AutoProcessor.from_pretrained(self.checkpoint_name) + audio = np.zeros(16000, dtype=np.float32) + + inputs = processor.apply_transcription_request( + audio=audio, model_id=self.checkpoint_name, sampling_rate=16000, format="wav" + ) + + self.assertIn("input_features", inputs) + self.assertEqual(inputs["input_features"].shape[0], 1) + @slow def test_transcribe_mode_audio_input(self): """