Skip to content

Python: [Bug]: Anthropic connector silently drops ImageContent in user messages and crashes on image-only requests #14531

Open

Description

@chauvuusvn

Describe the bug

In semantic-kernel Python SDK, _format_user_message in semantic_kernel/connectors/ai/anthropic/services/utils.py currently hardcodes:

def _format_user_message(message: ChatMessageContent) -> dict[str, Any]:
    return {
        "role": "user",
        "content": message.content,
    }

This causes two major issues when interacting with multimodal Anthropic Claude models (such as Claude 3.5 Sonnet, Claude 3.7 Sonnet, Claude Opus 4.8, Claude Fable 5.1 / Mythos 5.1):

  1. Silent Image Data Loss: If a user message contains ImageContent along with text in message.items, the image items are completely discarded and only the text content is sent to Anthropic.
  2. API Crash on Image-Only Messages: If a user message contains only ImageContent without text, message.content is empty/None, causing Anthropic API to return a 400 Bad Request: messages: at least one content block is required error.

Other connectors (Google AI, Vertex AI, AWS Bedrock) iterate over message.items to format ImageContent. Anthropic connector should support this as well.

Steps to Reproduce

from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.image_content import ImageContent
from semantic_kernel.contents.text_content import TextContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.connectors.ai.anthropic.services.utils import _format_user_message

msg = ChatMessageContent(
    role=AuthorRole.USER,
    items=[
        TextContent(text="Analyze this image"),
        ImageContent(data_uri="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="),
    ],
)
formatted = _format_user_message(msg)
# formatted['content'] is "Analyze this image" -- image was completely discarded!

Expected Behavior

_format_user_message should inspect message.items and format ImageContent into Anthropic's native {"type": "image", "source": {"type": "base64", ...}} content blocks.

Activity

  1. added
    .NETIssue or Pull requests regarding .NET code
    pythonPull requests for the Python Semantic Kernel
    on Oct 3, 2026
  2. changed the title [-][Bug]: Anthropic connector silently drops ImageContent in user messages and crashes on image-only requests[/-] [+].Net: [Bug]: Anthropic connector silently drops ImageContent in user messages and crashes on image-only requests[/+] on Oct 3, 2026
  3. changed the title [-].Net: [Bug]: Anthropic connector silently drops ImageContent in user messages and crashes on image-only requests[/-] [+]Python: [Bug]: Anthropic connector silently drops ImageContent in user messages and crashes on image-only requests[/+] on Oct 3, 2026
  4. ducanhnguyen223 commented on Oct 3, 2026

    @ducanhnguyen223

    Update: I found PR #14532 linked to this issue after posting; it already addresses data-backed image content, so I will not submit a duplicate implementation PR. I left a focused comment on #14532 about the URI-only ImageContent case because the type permits URI-only instances and Anthropic supports URL image sources. I will wait for feedback on whether that case is in scope before preparing any follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue or Pull requests regarding .NET codepythonPull requests for the Python Semantic Kerneltriage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions