Skip to content

Commit 2cb8c3b

Browse files
committed
Keep the SDK error for dtypes NumPy cannot parse
np.dtype() reports an unparseable dtype as either TypeError or ValueError, and the handler only caught TypeError. A malformed structured dtype such as {"names": ["x"], "formats": []} therefore escaped as a NumPy ValueError instead of the UserError the consumer gets for every other unsupported dtype. Catch both, and cover the unresolvable case in the existing rejection test alongside a dtype that resolves but is not supported.
1 parent cf79d1d commit 2cb8c3b

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎src/agents/voice/result.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ def _transform_audio_buffer(
107107
np_array = np.frombuffer(combined_buffer, dtype=np.int16)
108108

109109
# Resolve the configured dtype the way NumPy does so that every spelling of a supported
110-
# dtype is accepted, including the strings that dictionary-based settings carry.
110+
# dtype is accepted, including the strings that dictionary-based settings carry. NumPy
111+
# reports an unparseable dtype as either TypeError or ValueError, and both keep the
112+
# SDK-owned error rather than surfacing a NumPy parse failure to the consumer.
111113
try:
112114
resolved_dtype = np.dtype(output_dtype)
113-
except TypeError:
115+
except (TypeError, ValueError):
114116
raise UserError("Invalid output dtype") from None
115117

116118
if resolved_dtype == np.int16:

‎tests/voice/test_pipeline.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,15 +1366,26 @@ async def test_voicepipeline_accepts_numpy_dtype_spellings(
13661366

13671367

13681368
@pytest.mark.asyncio
1369-
async def test_voicepipeline_rejects_unsupported_output_dtype() -> None:
1369+
@pytest.mark.parametrize(
1370+
"dtype_spelling",
1371+
[
1372+
"int32",
1373+
{"names": ["x"], "formats": []},
1374+
],
1375+
ids=["resolvable-but-unsupported", "unresolvable-structured-dtype"],
1376+
)
1377+
async def test_voicepipeline_rejects_unsupported_output_dtype(
1378+
dtype_spelling: npt.DTypeLike,
1379+
) -> None:
1380+
"""An unsupported dtype keeps the SDK error, whether or not NumPy can parse it."""
13701381
fake_stt = QueuedSTTModel(["first"])
13711382
workflow = QueuedVoiceWorkflow([["out_1"]])
13721383
fake_tts = ZeroPcmTTSModel()
13731384
pipeline = VoicePipeline(
13741385
workflow=workflow,
13751386
stt_model=fake_stt,
13761387
tts_model=fake_tts,
1377-
config={"tts_settings": {"buffer_size": 1, "dtype": "int32"}},
1388+
config={"tts_settings": {"buffer_size": 1, "dtype": dtype_spelling}},
13781389
)
13791390
result = await pipeline.run(AudioInput(buffer=np.zeros(2, dtype=np.int16)))
13801391

0 commit comments

Comments
 (0)