agentscope-ai/agentscope · error · ValueError

Unsupported source type: {type(source)}

Error message

Unsupported source type: {type(source)}

What it means

The Ollama formatter's _format_image_source accepts Base64Source and URLSource (downloading remote URLs and converting to base64, since Ollama expects base64 image data); any other source type (LocalFileSource, plain strings/bytes) triggers this ValueError.

Source

Thrown at src/agentscope/formatter/_ollama_formatter.py:99

        """
        if isinstance(source, Base64Source):
            return source.data
        elif isinstance(source, URLSource):
            url = str(source.url)
            if url.startswith("file://"):
                # Local file - read and convert to base64
                file_path = url.removeprefix("file://")
                with open(file_path, "rb") as f:
                    data = base64.b64encode(f.read()).decode("utf-8")
                return data
            else:
                # Remote URL - download and convert to base64
                response = requests.get(url, timeout=30)
                response.raise_for_status()
                data = base64.b64encode(response.content).decode("utf-8")
                return data
        else:
            raise ValueError(f"Unsupported source type: {type(source)}")


class OllamaChatFormatter(_OllamaFormatterBase):
    """The Ollama formatter class for chatbot scenario, where only a user
    and an agent are involved. We use the `role` field to identify different
    participants in the conversation.
    """

    input_types: list[str] = Field(
        default_factory=lambda: ["text/plain", "image/*"],
        description=(
            "The supported input types. "
            'Defaults to ``["text/plain", "image/*"]``.'
        ),
    )

    async def format(
        self,

View on GitHub (pinned to e90f1c7592)

Solutions

  1. Read the local file and wrap it in a Base64Source
  2. Or use a URLSource if the image is remotely hosted (the formatter downloads it)
  3. Normalize message blocks to Base64Source at construction time when targeting Ollama

Example fix

# before
ImageBlock(source=LocalFileSource(path="dog.png"), alt="dog")

# after
import base64
ImageBlock(source=Base64Source(data=base64.b64encode(open("dog.png","rb").read()).decode(), media_type="image/png"), alt="dog")
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Formatting a Msg with an ImageBlock whose source is a LocalFileSource or another unsupported source class via OllamaChatFormatter.

Common situations: Assuming Ollama, being local, can read local file paths directly; porting multimodal messages from other providers; constructing sources from raw paths without the typed wrapper classes.

Related errors


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