Home / Function/ _format_data_content_block() — langchain Function Reference

_format_data_content_block() — langchain Function Reference

Architecture documentation for the _format_data_content_block() function in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  7042d659_4e83_4ce8_9e4c_bfd32dddc9aa["_format_data_content_block()"]
  a85819c7_917d_4c71_2864_a19e68947340["chat_models.py"]
  7042d659_4e83_4ce8_9e4c_bfd32dddc9aa -->|defined in| a85819c7_917d_4c71_2864_a19e68947340
  a1122c23_c68d_f096_a90e_42f0e4d39d61["_format_messages()"]
  a1122c23_c68d_f096_a90e_42f0e4d39d61 -->|calls| 7042d659_4e83_4ce8_9e4c_bfd32dddc9aa
  4db283f3_f14d_8856_90b5_c06620f07118["_format_image()"]
  7042d659_4e83_4ce8_9e4c_bfd32dddc9aa -->|calls| 4db283f3_f14d_8856_90b5_c06620f07118
  style 7042d659_4e83_4ce8_9e4c_bfd32dddc9aa fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/chat_models.py lines 284–407

def _format_data_content_block(block: dict) -> dict:
    """Format standard data content block to format expected by Anthropic."""
    if block["type"] == "image":
        if "url" in block:
            if block["url"].startswith("data:"):
                # Data URI
                formatted_block = {
                    "type": "image",
                    "source": _format_image(block["url"]),
                }
            else:
                formatted_block = {
                    "type": "image",
                    "source": {"type": "url", "url": block["url"]},
                }
        elif "base64" in block or block.get("source_type") == "base64":
            formatted_block = {
                "type": "image",
                "source": {
                    "type": "base64",
                    "media_type": block["mime_type"],
                    "data": block.get("base64") or block.get("data", ""),
                },
            }
        elif "file_id" in block:
            formatted_block = {
                "type": "image",
                "source": {
                    "type": "file",
                    "file_id": block["file_id"],
                },
            }
        elif block.get("source_type") == "id":
            formatted_block = {
                "type": "image",
                "source": {
                    "type": "file",
                    "file_id": block["id"],
                },
            }
        else:
            msg = (
                "Anthropic only supports 'url', 'base64', or 'id' keys for image "
                "content blocks."
            )
            raise ValueError(
                msg,
            )

    elif block["type"] == "file":
        if "url" in block:
            formatted_block = {
                "type": "document",
                "source": {
                    "type": "url",
                    "url": block["url"],
                },
            }
        elif "base64" in block or block.get("source_type") == "base64":
            formatted_block = {
                "type": "document",
                "source": {
                    "type": "base64",
                    "media_type": block.get("mime_type") or "application/pdf",
                    "data": block.get("base64") or block.get("data", ""),
                },
            }
        elif block.get("source_type") == "text":
            formatted_block = {
                "type": "document",
                "source": {
                    "type": "text",
                    "media_type": block.get("mime_type") or "text/plain",
                    "data": block["text"],
                },
            }
        elif "file_id" in block:
            formatted_block = {
                "type": "document",
                "source": {
                    "type": "file",

Domain

Subdomains

Called By

Frequently Asked Questions

What does _format_data_content_block() do?
_format_data_content_block() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/chat_models.py.
Where is _format_data_content_block() defined?
_format_data_content_block() is defined in libs/partners/anthropic/langchain_anthropic/chat_models.py at line 284.
What does _format_data_content_block() call?
_format_data_content_block() calls 1 function(s): _format_image.
What calls _format_data_content_block()?
_format_data_content_block() is called by 1 function(s): _format_messages.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free