agentscope-ai/agentscope · error · ValueError
Unsupported image source type: {type(source)}
Error message
Unsupported image source type: {type(source)} What it means
The Moonshot formatter only accepts URLSource (remote URLs are converted to base64 data URLs by downloading) and Base64Source for images; any other source type (e.g. LocalFileSource, raw bytes) hits this ValueError in _moonshot_format_image_source.
Source
Thrown at src/agentscope/formatter/_moonshot_formatter.py:51
"""
if isinstance(source, Base64Source):
url = f"data:{source.media_type};base64,{source.data}"
elif isinstance(source, URLSource):
url_str = str(source.url)
if url_str.startswith("file://"):
local_path = url_str.removeprefix("file://")
with open(local_path, "rb") as f:
encoded = base64.b64encode(f.read()).decode("utf-8")
url = f"data:{source.media_type};base64,{encoded}"
else:
response = requests.get(url_str, timeout=30)
response.raise_for_status()
encoded = base64.b64encode(response.content).decode("utf-8")
url = f"data:{source.media_type};base64,{encoded}"
else:
raise ValueError(f"Unsupported image source type: {type(source)}")
return {
"type": "image_url",
"image_url": {"url": url},
}
class MoonshotChatFormatter(_OpenAIFormatterBase):
"""The Moonshot AI formatter for chatbot scenario.
Moonshot's API is OpenAI-compatible, but thinking models (``kimi-k2.6``,
``kimi-k2-thinking``) return a ``reasoning_content`` field alongside
``content`` in assistant messages. This formatter preserves that field
when re-sending assistant messages back to the API so that the
*Preserved Thinking* feature works correctly in multi-turn conversations.
"""
input_types: list[str] = Field(View on GitHub (pinned to e90f1c7592)
Solutions
- Convert the local image to a Base64Source before formatting
- Host the image and use URLSource with the public URL
- Pre-check source types and provide a text fallback for unsupported blocks
Example fix
# before
ImageBlock(source=LocalFileSource(path="cat.jpg"), alt="cat")
# after
import base64
ImageBlock(source=Base64Source(data=base64.b64encode(open("cat.jpg","rb").read()).decode(), media_type="image/jpeg"), alt="cat") Defensive patterns
Strategy: type-guard
Validate before calling
from agentscope.message import URLSource, Base64Source assert isinstance(block.source, (URLSource, Base64Source))
Type guard
def is_moonshot_image_source(src) -> bool:
return isinstance(src, (URLSource, Base64Source)) Try / catch
try:
formatter.format(msgs)
except ValueError as e:
if "Unsupported image source type" in str(e):
block.source = local_path_to_base64_source(path) # then retry
else:
raise Prevention
- Convert local images to Base64Source up front
- Remember the Moonshot formatter downloads remote URLs — ensure they're reachable to avoid requests errors
When it happens
Trigger: Passing an ImageBlock with a LocalFileSource or non-URL/base64 source to the Moonshot formatter; using a source class the Moonshot path doesn't isinstance-match.
Common situations: Local image paths fed into Moonshot pipelines; sharing message-building code across providers where some accept local files; stale source objects after library upgrades that renamed source classes.
Related errors
- Unsupported source type: {type(source)}
- Unsupported image source type: {type(source)}
- Unsupported audio source type: {type(source)}
- Unsupported source type: {type(source)}
- Unsupported audio source type: {type(source)}.
AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28).
Data as JSON: /api/errors/35e975a0cb854a78.
Report an issue: GitHub.