agentscope-ai/agentscope · error · TypeError

Unsupported audio source type: {type(source)}.

Error message

Unsupported audio source type: {type(source)}.

What it means

After validating the media type, _format_audio_source handles Base64Source and URLSource; a source of any other type falls through to this TypeError. It mirrors the image/file source checks: OpenAI audio must be provided as base64 data or a URL.

Source

Thrown at src/agentscope/formatter/_openai_formatter.py:182

                # Local file
                local_path = url_str.removeprefix("file://")
                with open(local_path, "rb") as f:
                    data = base64.b64encode(f.read()).decode("utf-8")
            else:
                # Remote URL — download and encode
                response = requests.get(url_str, timeout=30)
                response.raise_for_status()
                data = base64.b64encode(response.content).decode("utf-8")

            return {
                "type": "input_audio",
                "input_audio": {
                    "data": data,
                    "format": audio_format,
                },
            }

        raise TypeError(f"Unsupported audio source type: {type(source)}.")

    @staticmethod
    def _format_file_source(
        source: URLSource | Base64Source,
        name: str | None,
    ) -> dict[str, Any]:
        """Convert a PDF source to the OpenAI ``file`` content part.

        Files are always inlined as base64 data URIs: local ``file://`` URLs
        are read from disk and remote URLs are downloaded, since the Chat
        Completions API has no URL form for files.

        Args:
            source (`URLSource | Base64Source`):
                The file source to convert.
            name (`str | None`):
                The file name presented to the API, falls back to
                ``"document.pdf"``.

View on GitHub (pinned to e90f1c7592)

Solutions

  1. Read the file, base64-encode it, and use Base64Source with media_type 'audio/wav' or 'audio/mpeg'
  2. Or use URLSource with a hosted audio URL
  3. Pre-validate audio sources before format() and fall back to text if unsupported

Example fix

# before
AudioBlock(source=LocalFileSource(path="note.wav"), alt="note")

# after
import base64
AudioBlock(source=Base64Source(data=base64.b64encode(open("note.wav","rb").read()).decode(), media_type="audio/wav"), alt="note")
Defensive patterns

Strategy: type-guard

Validate before calling

from agentscope.message import URLSource, Base64Source
assert isinstance(source, (URLSource, Base64Source))

Type guard

def is_openai_audio_source(src) -> bool:
    return isinstance(src, (URLSource, Base64Source))

Try / catch

try:
    formatter.format(msgs)
except TypeError as e:
    if "Unsupported audio source type" in str(e):
        block.source = to_base64_source(path, "audio/wav")
        formatted = formatter.format(msgs)
    else:
        raise

Prevention

When it happens

Trigger: Passing an AudioBlock with a LocalFileSource or non-URL/base64 source (raw bytes, file object) to the OpenAI formatter.

Common situations: Recording audio locally and passing file paths; reusing audio blocks across providers; constructing sources manually instead of via the typed classes.

Related errors


AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28). Data as JSON: /api/errors/38f6bb921e857826. Report an issue: GitHub.