agentscope-ai/agentscope · error · ValueError

Invalid input: {item!r}. Expected str or DataBlock.

Error message

Invalid input: {item!r}. Expected str or DataBlock.

What it means

The DashScope multimodal embedding endpoint accepts only str and DataBlock items; _call_multimodal rejects any other element with a ValueError echoing the offending value.

Source

Thrown at src/agentscope/embedding/_dashscope/_model.py:426

        """Call the DashScope multimodal embedding API for a single batch.

        Args:
            inputs (`list[str | DataBlock]`):
                ``str`` for text, ``DataBlock`` for images / videos.
            **kwargs:
                Forwarded to the API.

        Returns:
            `EmbeddingResponse`: Embedding vectors and usage info.
        """
        formatted: list[dict[str, str]] = []
        for item in inputs:
            if isinstance(item, str):
                formatted.append({"text": item})
            elif isinstance(item, DataBlock):
                formatted.append(self._format_data_block(item))
            else:
                raise ValueError(
                    f"Invalid input: {item!r}. Expected str or DataBlock.",
                )

        api_kwargs: dict[str, Any] = {
            "input": formatted,
            "model": self.model,
            "api_key": self.api_key,
            **kwargs,
        }

        # Exclude api_key from cache identifier to avoid persisting secrets
        # and to keep cache valid across key rotations.
        cache_identifier = {
            k: v for k, v in api_kwargs.items() if k != "api_key"
        }

        if self.embedding_cache:
            cached = await self.embedding_cache.retrieve(

View on GitHub (pinned to e90f1c7592)

Solutions

  1. Wrap binary data in DataBlock(source=Base64Source(...)) and text in plain str
  2. Coerce or drop non-conforming items before the call
  3. Add a preprocessing step that maps your pipeline's types to str/DataBlock

Example fix

# before
await model([open(img, "rb").read()])
# after
from agentscope.message import DataBlock, Base64Source
import base64
await model([DataBlock(source=Base64Source(media_type="image/png", data=base64.b64encode(raw).decode()))])
Defensive patterns

Strategy: type-guard

Validate before calling

from agentscope.message import DataBlock
def ok(i): return isinstance(i, (str, DataBlock))
inputs = [i for i in inputs if ok(i)]

Type guard

def is_multimodal_input(item) -> bool:\n    from agentscope.message import DataBlock\n    return isinstance(item, (str, DataBlock))

Try / catch

try:\n    await model(items)\nexcept ValueError as e:\n    if \"Expected str or DataBlock\" in str(e): items = coerce(items); await model(items)\n    else: raise

Prevention

When it happens

Trigger: Calling the multimodal embedding model with list items that are neither str nor DataBlock — e.g. bytes, dicts, tuples, or raw file paths as strings-as-path objects.

Common situations: Feeding raw base64 strings or bytes instead of wrapping them in DataBlock with Base64Source; passing already-formatted API dicts; heterogeneous data from crawlers containing None.

Related errors


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