langchain-ai/langchain · error · ValueError

Unrecognized content block at messages[{i}].content[{j}] has

Error message

Unrecognized content block at messages[{i}].content[{j}] has 'type': 'image' but 'source' is missing expected key(s) {missing}. Full content block:

{block}

What it means

Raised by `convert_to_openai_messages` when converting an Anthropic-style image block (`{'type': 'image', 'source': {...}}`): the `source` dict must contain 'media_type', 'type', and 'data' so the converter can assemble a `data:<media_type>;<type>,<data>` URL. Missing any of those keys triggers this error with the full block printed.

Source

Thrown at libs/core/langchain_core/messages/utils.py:1738

                        and "filename" not in formatted_block["file"]
                    ):
                        logger.info("Generating a fallback filename.")
                        formatted_block["file"]["filename"] = "LC_AUTOGENERATED"
                    content.append(formatted_block)
                # Anthropic and Bedrock converse format
                elif (block.get("type") == "image") or "image" in block:
                    # Anthropic
                    if source := block.get("source"):
                        if missing := [
                            k for k in ("media_type", "type", "data") if k not in source
                        ]:
                            err = (
                                f"Unrecognized content block at "
                                f"messages[{i}].content[{j}] has 'type': 'image' "
                                f"but 'source' is missing expected key(s) "
                                f"{missing}. Full content block:\n\n{block}"
                            )
                            raise ValueError(err)
                        content.append(
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": (
                                        f"data:{source['media_type']};"
                                        f"{source['type']},{source['data']}"
                                    )
                                },
                            }
                        )
                    # Bedrock converse
                    elif image := block.get("image"):
                        if missing := [
                            k for k in ("source", "format") if k not in image
                        ]:
                            err = (
                                f"Unrecognized content block at "

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Provide all three source keys: `{'type': 'base64', 'media_type': 'image/png', 'data': '<base64>'}`
  2. If you only have a URL, use the OpenAI shape `{'type': 'image_url', 'image_url': {'url': ...}}` instead
  3. Validate source dict keys before assembling Anthropic blocks

Example fix

# before
{'type': 'image', 'source': {'type': 'base64', 'data': b64}}

# after
{'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/png', 'data': b64}}
Defensive patterns

Strategy: validation

Validate before calling

def valid_anthropic_source(s) -> bool:
    return (isinstance(s, dict)
            and all(k in s for k in ('media_type', 'type', 'data'))
            and isinstance(s.get('data'), str))

Type guard

def is_wellformed_anthropic_image(b: object) -> bool:
    return (isinstance(b, dict) and b.get('type') == 'image'
            and isinstance(b.get('source'), dict)
            and all(k in b['source'] for k in ('media_type', 'type', 'data')))

Prevention

When it happens

Trigger: An Anthropic image block whose source is incomplete, e.g. `{'type': 'image', 'source': {'type': 'base64', 'data': '...'}}` without 'media_type', or a source that is a URL string instead of the expected dict shape.

Common situations: Relaying raw Anthropic API payloads into langchain message content; stripping fields during JSON transport; hand-building blocks from memory of the Anthropic schema.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/979f988729ff6c65. Report an issue: GitHub.