Skip to content

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

fix(formatter): derive audio format from media type (#2301)

ump45noseb2d60ccab81b
diff --git a/src/agentscope/formatter/_openai_formatter.py b/src/agentscope/formatter/_openai_formatter.py
index a94d8ae56..7d7397fde 100644
--- a/src/agentscope/formatter/_openai_formatter.py
+++ b/src/agentscope/formatter/_openai_formatter.py
@@ -4,7 +4,6 @@ import base64
from abc import ABC
from fnmatch import fnmatch
from typing import Any
-from urllib.parse import urlparse
import requests
from pydantic import Field
@@ -137,25 +136,25 @@ class _OpenAIFormatterBase(FormatterBase, ABC):
`dict[str, Any]`:
A dictionary with ``"type": "input_audio"`` in OpenAI format.
"""
+ media_type_to_format = {
+ "audio/wav": "wav",
+ "audio/mp3": "mp3",
+ "audio/mpeg": "mp3",
+ }
+ media_type = source.media_type
+ if media_type not in media_type_to_format:
+ raise TypeError(
+ f"Unsupported audio media type: {media_type}, "
+ "only WAV and MP3 audio are supported.",
+ )
+ audio_format = media_type_to_format[media_type]
+
if isinstance(source, Base64Source):
- media_type = source.media_type
- _AUDIO_FORMAT_MAP = {
- "audio/wav": "wav",
- "audio/mp3": "mp3",
- "audio/mpeg": "mp3",
- }
- fmt = _AUDIO_FORMAT_MAP.get(media_type)
- if fmt is None:
- raise TypeError(
- f"Unsupported audio media type: {media_type}, "
- "only audio/wav, audio/mp3 and audio/mpeg"
- " are supported.",
- )
return {
"type": "input_audio",
"input_audio": {
"data": source.data,
- "format": fmt,
+ "format": audio_format,
},
}
@@ -164,23 +163,10 @@ class _OpenAIFormatterBase(FormatterBase, ABC):
if url_str.startswith("file://"):
# Local file
local_path = url_str.removeprefix("file://")
- extension = local_path.rsplit(".", 1)[-1].lower()
- if extension not in ["wav", "mp3"]:
- raise TypeError(
- f"Unsupported audio file extension: {extension}, "
- "wav and mp3 are supported.",
- )
with open(local_path, "rb") as f:
data = base64.b64encode(f.read()).decode("utf-8")
else:
# Remote URL — download and encode
- parsed = urlparse(url_str)
- extension = parsed.path.rsplit(".", 1)[-1].lower()
- if extension not in ["wav", "mp3"]:
- raise TypeError(
- f"Unsupported audio file extension: {extension}, "
- "wav and mp3 are supported.",
- )
response = requests.get(url_str, timeout=30)
response.raise_for_status()
data = base64.b64encode(response.content).decode("utf-8")
@@ -189,7 +175,7 @@ class _OpenAIFormatterBase(FormatterBase, ABC):
"type": "input_audio",
"input_audio": {
"data": data,
- "format": extension,
+ "format": audio_format,
},
}
diff --git a/tests/formatter_openai_chat_test.py b/tests/formatter_openai_chat_test.py
index 10999e7bc..87d6a8147 100644
--- a/tests/formatter_openai_chat_test.py
+++ b/tests/formatter_openai_chat_test.py
@@ -4,7 +4,7 @@ OpenAIMultiAgentFormatter, following the reference test style with exact
ground-truth comparisons.
"""
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import patch
+from unittest.mock import Mock, patch
from agentscope.formatter import (
OpenAIChatFormatter,
@@ -389,6 +389,68 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
res,
)
+ async def test_chat_formatter_base64_mpeg_audio(self) -> None:
+ """The standard MPEG media type maps to OpenAI's mp3 format."""
+ fmt = OpenAIChatFormatter()
+
+ res = await fmt.format(
+ [
+ UserMsg(
+ name="user",
+ content=[
+ DataBlock(
+ source=Base64Source(
+ data="bXAzIGRhdGE=",
+ media_type="audio/mpeg",
+ ),
+ ),
+ ],
+ ),
+ ],
+ )
+
+ self.assertEqual(
+ res[0]["content"][0],
+ {
+ "type": "input_audio",
+ "input_audio": {
+ "data": "bXAzIGRhdGE=",
+ "format": "mp3",
+ },
+ },
+ )
+
+ @patch("agentscope.formatter._openai_formatter.requests.get")
+ async def test_chat_formatter_extensionless_url_audio(
+ self,
+ mock_get: Mock,
+ ) -> None:
+ """Declared media type controls extensionless URL audio format."""
+ mock_get.return_value.content = b"wav data"
+ fmt = OpenAIChatFormatter()
+
+ res = await fmt.format(
+ [
+ UserMsg(
+ name="user",
+ content=[
+ DataBlock(
+ source=URLSource(
+ url="https://example.com/download?token=abc",
+ media_type="audio/wav",
+ ),
+ ),
+ ],
+ ),
+ ],
+ )
+
+ self.assertEqual(res[0]["content"][0]["input_audio"]["format"], "wav")
+ mock_get.assert_called_once_with(
+ "https://example.com/download?token=abc",
+ timeout=30,
+ )
+
async def test_chat_formatter_thinking_dropped(self) -> None:
"""ThinkingBlock is silently dropped by OpenAI formatter."""
fmt = OpenAIChatFormatter()