Skip to content

Commit d724a6a

Browse files
committed
fix: narrow FunASR STT exception handling
1 parent e05f50b commit d724a6a

3 files changed

Lines changed: 74 additions & 4 deletions

File tree

plugins/funasr/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ raw-options = { root = "..", search_parent_directories = true, fallback_version
2828
packages = ["vision_agents"]
2929

3030
[tool.hatch.build.targets.sdist]
31-
include = ["/vision_agents"]
31+
include = ["vision_agents"]
3232

3333
[tool.uv.sources]
3434
vision-agents = { workspace = true }

plugins/funasr/tests/test_funasr_stt.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
1+
import asyncio
2+
3+
import pytest
4+
15
from vision_agents.plugins import funasr
26

37

8+
class _FakeAutoModel:
9+
pass
10+
11+
12+
class _FakeSamples:
13+
def __init__(self, size=0):
14+
self.size = size
15+
16+
17+
class _FakePcmData:
18+
def __init__(
19+
self,
20+
*,
21+
samples=None,
22+
sample_rate=16000,
23+
channels=1,
24+
format=None,
25+
duration_ms=0,
26+
):
27+
self.samples = samples if samples is not None else _FakeSamples()
28+
self.sample_rate = sample_rate
29+
self.channels = channels
30+
self.format = format
31+
self.duration_ms = duration_ms
32+
33+
def resample(self, sample_rate):
34+
self.sample_rate = sample_rate
35+
return self
36+
37+
def to_float32(self):
38+
return self
39+
40+
def append(self, audio_data):
41+
return audio_data
42+
43+
44+
class _ExplodingPcmData:
45+
samples = _FakeSamples(size=1)
46+
47+
def resample(self, sample_rate):
48+
raise AssertionError("unexpected audio bug")
49+
50+
451
class TestSTT:
552
"""Unit tests for the FunASR STT plugin (no model loaded)."""
653

@@ -24,3 +71,24 @@ def test_custom_params(self):
2471
assert stt.language == "zh"
2572
assert stt.device == "cuda"
2673
assert stt.use_itn is False
74+
75+
def test_process_audio_raises_unexpected_buffer_errors(self):
76+
stt = funasr.STT(client=_FakeAutoModel())
77+
78+
with pytest.raises(AssertionError, match="unexpected audio bug"):
79+
asyncio.run(stt.process_audio(_ExplodingPcmData(), participant=object()))
80+
81+
def test_process_buffer_raises_unexpected_transcription_errors(self):
82+
stt = funasr.STT(client=_FakeAutoModel())
83+
stt._audio_buffer = _FakePcmData(
84+
samples=_FakeSamples(size=16000),
85+
duration_ms=1000,
86+
)
87+
88+
async def raise_unexpected_error(*, audio_array):
89+
raise AssertionError("unexpected transcription bug")
90+
91+
stt._transcribe = raise_unexpected_error
92+
93+
with pytest.raises(AssertionError, match="unexpected transcription bug"):
94+
asyncio.run(stt._process_buffer(participant=object()))

plugins/funasr/vision_agents/plugins/funasr/stt.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ async def process_audio(
136136
if should_process:
137137
await self._process_buffer(participant)
138138

139-
except Exception:
140-
logger.exception("Error buffering audio for FunASR")
139+
except ValueError:
140+
logger.exception("Invalid PCM audio while buffering for FunASR")
141+
except RuntimeError:
142+
logger.exception("Audio buffering/runtime error for FunASR")
141143

142144
async def _process_buffer(self, participant: Participant):
143145
"""Process the current audio buffer through FunASR."""
@@ -158,7 +160,7 @@ async def _process_buffer(self, participant: Participant):
158160

159161
try:
160162
text = await self._transcribe(audio_array=audio_array)
161-
except Exception:
163+
except (RuntimeError, ValueError, KeyError, IndexError, TypeError):
162164
logger.exception("Error processing audio buffer with FunASR")
163165
return
164166

0 commit comments

Comments
 (0)