Home / Function/ _format_content_block_xml() — langchain Function Reference

_format_content_block_xml() — langchain Function Reference

Architecture documentation for the _format_content_block_xml() function in utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  8c272a75_04d0_f5f2_a5cd_cd4a21d4dfdd["_format_content_block_xml()"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6["utils.py"]
  8c272a75_04d0_f5f2_a5cd_cd4a21d4dfdd -->|defined in| 0b528c80_0ce7_1c74_8932_bc433bcb03c6
  cbb21a1a_871c_8af4_2881_e51d99e3c0a1["get_buffer_string()"]
  cbb21a1a_871c_8af4_2881_e51d99e3c0a1 -->|calls| 8c272a75_04d0_f5f2_a5cd_cd4a21d4dfdd
  0cbee999_e800_48f7_a4ea_c05ff0851910["_has_base64_data()"]
  8c272a75_04d0_f5f2_a5cd_cd4a21d4dfdd -->|calls| 0cbee999_e800_48f7_a4ea_c05ff0851910
  80987f62_c6a0_4904_aacc_d630b164fa6a["_truncate()"]
  8c272a75_04d0_f5f2_a5cd_cd4a21d4dfdd -->|calls| 80987f62_c6a0_4904_aacc_d630b164fa6a
  style 8c272a75_04d0_f5f2_a5cd_cd4a21d4dfdd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/utils.py lines 142–244

def _format_content_block_xml(block: dict) -> str | None:
    """Format a content block as XML.

    Args:
        block: A LangChain content block.

    Returns:
        XML string representation of the block, or `None` if the block should be
            skipped.

    Note:
        Plain text document content, server tool call arguments, and server tool
        result outputs are truncated to 500 characters.
    """
    block_type = block.get("type", "")

    # Skip blocks with base64 encoded data
    if _has_base64_data(block):
        return None

    # Text blocks
    if block_type == "text":
        text = block.get("text", "")
        return escape(text) if text else None

    # Reasoning blocks
    if block_type == "reasoning":
        reasoning = block.get("reasoning", "")
        if reasoning:
            return f"<reasoning>{escape(reasoning)}</reasoning>"
        return None

    # Image blocks (URL only, base64 already filtered)
    if block_type == "image":
        url = block.get("url")
        file_id = block.get("file_id")
        if url:
            return f"<image url={quoteattr(url)} />"
        if file_id:
            return f"<image file_id={quoteattr(file_id)} />"
        return None

    # OpenAI-style image_url blocks
    if block_type == "image_url":
        image_url = block.get("image_url", {})
        if isinstance(image_url, dict):
            url = image_url.get("url", "")
            if url and not url.startswith("data:"):
                return f"<image url={quoteattr(url)} />"
        return None

    # Audio blocks (URL only)
    if block_type == "audio":
        url = block.get("url")
        file_id = block.get("file_id")
        if url:
            return f"<audio url={quoteattr(url)} />"
        if file_id:
            return f"<audio file_id={quoteattr(file_id)} />"
        return None

    # Video blocks (URL only)
    if block_type == "video":
        url = block.get("url")
        file_id = block.get("file_id")
        if url:
            return f"<video url={quoteattr(url)} />"
        if file_id:
            return f"<video file_id={quoteattr(file_id)} />"
        return None

    # Plain text document blocks
    if block_type == "text-plain":
        text = block.get("text", "")
        return escape(_truncate(text)) if text else None

    # Server tool call blocks (from AI messages)
    if block_type == "server_tool_call":
        tc_id = quoteattr(str(block.get("id") or ""))
        tc_name = quoteattr(str(block.get("name") or ""))
        tc_args_json = json.dumps(block.get("args", {}), ensure_ascii=False)

Subdomains

Frequently Asked Questions

What does _format_content_block_xml() do?
_format_content_block_xml() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/utils.py.
Where is _format_content_block_xml() defined?
_format_content_block_xml() is defined in libs/core/langchain_core/messages/utils.py at line 142.
What does _format_content_block_xml() call?
_format_content_block_xml() calls 2 function(s): _has_base64_data, _truncate.
What calls _format_content_block_xml()?
_format_content_block_xml() is called by 1 function(s): get_buffer_string.

Analyze Your Own Codebase

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

Try Supermodel Free