Skip to content

Build and run agents you can see, understand and trust.

fix(model): declare real WAV sizes in the final streamed audio block (#3242)

哈基米e941f946c09e
diff --git a/src/agentscope/_utils/_audio.py b/src/agentscope/_utils/_audio.py
index c232006c7..5019623c8 100644
--- a/src/agentscope/_utils/_audio.py
+++ b/src/agentscope/_utils/_audio.py
@@ -92,3 +92,38 @@ def _build_wav_header(
channels,
bits_per_sample,
)
+
+
+def _finalize_streaming_wav(wav_bytes: bytes) -> bytes:
+ """Declare real chunk sizes in a complete WAV built for streaming.
+
+ A buffer assembled from streaming deltas starts with the placeholder
+ header from :func:`_build_streaming_wav_header`. Once the buffer is
+ complete, the RIFF and ``data`` chunk sizes are rewritten with the real
+ values, so the result is a well-formed WAV just like
+ :func:`_build_wav_header` produces. Only the canonical 44-byte layout
+ emitted by :func:`_build_streaming_wav_header` is rewritten; other RIFF
+ layouts are returned unchanged.
+
+ Args:
+ wav_bytes (`bytes`):
+ The complete buffer assembled from the stream deltas.
+
+ Returns:
+ `bytes`:
+ The buffer with real chunk sizes in its WAV header.
+ """
+ placeholder_size = struct.pack("<I", 0xFFFFFFFF)
+ if (
+ len(wav_bytes) >= 44
+ and wav_bytes[:4] == b"RIFF"
+ and wav_bytes[4:8] == placeholder_size
+ and wav_bytes[8:20] == b"WAVEfmt " + struct.pack("<I", 16)
+ and wav_bytes[36:44] == b"data" + placeholder_size
+ ):
+ data_len = len(wav_bytes) - 44
+ finalized = bytearray(wav_bytes)
+ struct.pack_into("<I", finalized, 4, 36 + data_len)
+ struct.pack_into("<I", finalized, 40, data_len)
+ return bytes(finalized)
+ return wav_bytes
diff --git a/src/agentscope/model/_utils.py b/src/agentscope/model/_utils.py
index 10cf74a77..de7b003ef 100644
--- a/src/agentscope/model/_utils.py
+++ b/src/agentscope/model/_utils.py
@@ -8,6 +8,7 @@ from pydantic import Field
from ._model_response import ChatResponse, FinishedReason
from ._model_usage import ChatUsage
from .._logging import logger
+from .._utils._audio import _finalize_streaming_wav
from ..message import (
Base64Source,
DataBlock,
@@ -125,8 +126,13 @@ class _AccBase64Source(Base64Source):
def build(self) -> Base64Source:
"""Join the raw bytes and base64-encode them once."""
+ data = b"".join(self.data)
+ if self.media_type == "audio/wav":
+ # The stream began with the placeholder WAV header; now that
+ # the buffer is complete, declare its real chunk sizes.
+ data = _finalize_streaming_wav(data)
return Base64Source(
- data=base64.b64encode(b"".join(self.data)).decode("ascii"),
+ data=base64.b64encode(data).decode("ascii"),
media_type=self.media_type,
)
diff --git a/tests/model_base_test.py b/tests/model_base_test.py
index a79c3069d..83b712c94 100644
--- a/tests/model_base_test.py
+++ b/tests/model_base_test.py
@@ -3,6 +3,7 @@
retry / accumulation / interrupt wrapper around ``_call_api``."""
import asyncio
import base64
+import struct
from typing import Any
from unittest.async_case import IsolatedAsyncioTestCase
@@ -873,6 +874,74 @@ class ChatModelBaseCallTest(IsolatedAsyncioTestCase):
),
)
+ async def test_stream_extended_wav_header_is_preserved(self) -> None:
+ """A non-canonical WAV chunk layout must not be rewritten."""
+ pcm = bytes([1, 2, 3, 4])
+ extended_wav = (
+ b"RIFF"
+ + b"\xff" * 4
+ + b"WAVE"
+ + b"fmt "
+ + struct.pack(
+ "<IHHIIHHH",
+ 18,
+ 1,
+ 1,
+ 24000,
+ 48000,
+ 2,
+ 16,
+ 0,
+ )
+ + b"data"
+ + b"\xff" * 4
+ + pcm
+ )
+ deltas = [
+ ChatResponse(
+ content=[
+ DataBlock(
+ id="audio-1",
+ source=Base64Source(
+ data=base64.b64encode(extended_wav).decode(
+ "ascii",
+ ),
+ media_type="audio/wav",
+ ),
+ ),
+ ],
+ is_last=False,
+ ),
+ ]
+ self.model.set_responses([deltas])
+
+ gen = await self.model(messages=self.messages)
+ last_chunk = None
+ async for chunk in gen:
+ last_chunk = chunk
+
+ self.assertDictEqual(
+ _dump(last_chunk),
+ _expected(
+ content=[
+ {
+ "type": "data",
+ "id": "audio-1",
+ "source": {
+ "type": "base64",
+ "data": base64.b64encode(extended_wav).decode(
+ "ascii",
+ ),
+ "media_type": "audio/wav",
+ },
+ "name": None,
+ },
+ ],
+ is_last=True,
+ finished_reason=FinishedReason.COMPLETED,
+ ),
+ )
+
# ------------------------------------------------------------------
# 11) stream non-audio data block — the latest delta wins
# ------------------------------------------------------------------
diff --git a/tests/model_dashscope_test.py b/tests/model_dashscope_test.py
index 89c31de24..18d560a87 100644
--- a/tests/model_dashscope_test.py
+++ b/tests/model_dashscope_test.py
@@ -6,6 +6,7 @@ Tests cover both non-streaming and streaming modes.
"""
import base64
import io
+import struct
import wave
from typing import Any
import unittest
@@ -574,10 +575,19 @@ class TestDashScopeStream(IsolatedAsyncioTestCase):
b for b in final.content if isinstance(b, DataBlock)
)
wav_bytes = base64.b64decode(final_audio.source.data)
+ self.assertEqual(
+ struct.unpack("<I", wav_bytes[4:8])[0],
+ 36 + len(pcm_full),
+ )
+ self.assertEqual(
+ struct.unpack("<I", wav_bytes[40:44])[0],
+ len(pcm_full),
+ )
with wave.open(io.BytesIO(wav_bytes), "rb") as wav:
self.assertEqual(wav.getnchannels(), 1)
self.assertEqual(wav.getsampwidth(), 2)
self.assertEqual(wav.getframerate(), 24000)
+ self.assertEqual(wav.getnframes(), len(pcm_full) // 2)
frames = wav.readframes(wav.getnframes())
self.assertEqual(frames, pcm_full)
diff --git a/tests/model_openai_chat_test.py b/tests/model_openai_chat_test.py
index 004f7282a..2a4804b37 100644
--- a/tests/model_openai_chat_test.py
+++ b/tests/model_openai_chat_test.py
@@ -795,10 +795,19 @@ class TestOpenAIChatStream(IsolatedAsyncioTestCase):
b for b in final.content if isinstance(b, DataBlock)
)
wav_bytes = base64.b64decode(final_audio.source.data)
+ self.assertEqual(
+ struct.unpack("<I", wav_bytes[4:8])[0],
+ 36 + len(pcm_full),
+ )
+ self.assertEqual(
+ struct.unpack("<I", wav_bytes[40:44])[0],
+ len(pcm_full),
+ )
with wave.open(io.BytesIO(wav_bytes), "rb") as wav:
self.assertEqual(wav.getnchannels(), 1)
self.assertEqual(wav.getsampwidth(), 2)
self.assertEqual(wav.getframerate(), 24000)
+ self.assertEqual(wav.getnframes(), len(pcm_full) // 2)
frames = wav.readframes(wav.getnframes())
self.assertEqual(frames, pcm_full)