microsoft/autogen · error · ValueError

Unknown content type: {part}

Error message

Unknown content type: {part}

What it means

When converting a UserMessage with multi-part content into Anthropic blocks, the client handles str parts and Image parts explicitly. Any other object in the content list (a dict, a custom class, bytes, None) hits the else branch and raises ValueError('Unknown content type: {part}'). This keeps malformed message content from reaching the Anthropic API.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py:225

    else:
        blocks: List[Union[TextBlockParam, ImageBlockParam]] = []

        for part in message.content:
            if isinstance(part, str):
                blocks.append(TextBlockParam(type="text", text=__empty_content_to_whitespace(part)))
            elif isinstance(part, Image):
                blocks.append(
                    ImageBlockParam(
                        type="image",
                        source=Base64ImageSourceParam(
                            type="base64",
                            media_type=get_mime_type_from_image(part),
                            data=part.to_base64(),
                        ),
                    )
                )
            else:
                raise ValueError(f"Unknown content type: {part}")

        return {
            "role": "user",
            "content": blocks,
        }


def system_message_to_anthropic(message: SystemMessage) -> str:
    return __empty_content_to_whitespace(message.content)


def assistant_message_to_anthropic(message: AssistantMessage) -> MessageParam:
    assert_valid_name(message.source)

    if isinstance(message.content, list):
        # Tool calls
        tool_use_blocks: List[ToolUseBlock] = []

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Build multi-part content from plain str and autogen_core.Image instances only.
  2. Unwrap API-style dicts to their text/image equivalents before constructing UserMessage.
  3. Filter the list: [p for p in parts if isinstance(p, (str, Image))] to drop None/unsupported entries.

Example fix

# before
msg = UserMessage(content=[{'type': 'text', 'text': 'analyze this'}, img], source='user')  # ValueError

# after
msg = UserMessage(content=['analyze this', img], source='user')
Defensive patterns

Strategy: type-guard

Validate before calling

from autogen_core import Image

def clean_parts(parts):
    out = []
    for p in parts:
        if isinstance(p, str):
            out.append(p)
        elif isinstance(p, Image):
            out.append(p)
        elif isinstance(p, dict) and p.get('type') == 'text':
            out.append(p['text'])
        # else dropped
    return out

Type guard

def is_supported_part(p) -> bool:
    return isinstance(p, (str, Image))

Try / catch

try:
    res = await client.create([UserMessage(content=parts, source='user')])
except ValueError as e:
    if 'Unknown content type' in str(e):
        parts = [p for p in parts if isinstance(p, (str, Image))]
        res = await client.create([UserMessage(content=parts, source='user')])
    else:
        raise

Prevention

When it happens

Trigger: UserMessage(content=[{'type': 'text', 'text': 'hi'}], ...) using Anthropic-native dicts instead of autogen types; a list mixing in None or FunctionCall objects; a custom content class not derived from str/Image.

Common situations: Translating raw Anthropic/OpenAI message payloads into autogen messages and forgetting to unwrap dicts to plain strings; spreading optional parts into the list (None placeholders); version drift where a new content type exists in core but not in this client.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/6af9736d737c0bcb. Report an issue: GitHub.