Home / Function/ content_blocks() — langchain Function Reference

content_blocks() — langchain Function Reference

Architecture documentation for the content_blocks() function in ai.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  c00149ad_6593_d143_66cc_6d95af19fd90["content_blocks()"]
  de5a7878_b3fe_95d7_2575_7f534546dc1e["AIMessage"]
  c00149ad_6593_d143_66cc_6d95af19fd90 -->|defined in| de5a7878_b3fe_95d7_2575_7f534546dc1e
  3153d67d_4a59_c444_07e0_3c7cd1bcbc86["content_blocks()"]
  3153d67d_4a59_c444_07e0_3c7cd1bcbc86 -->|calls| c00149ad_6593_d143_66cc_6d95af19fd90
  3153d67d_4a59_c444_07e0_3c7cd1bcbc86["content_blocks()"]
  c00149ad_6593_d143_66cc_6d95af19fd90 -->|calls| 3153d67d_4a59_c444_07e0_3c7cd1bcbc86
  style c00149ad_6593_d143_66cc_6d95af19fd90 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/ai.py lines 243–299

    def content_blocks(self) -> list[types.ContentBlock]:
        """Return standard, typed `ContentBlock` dicts from the message.

        If the message has a known model provider, use the provider-specific translator
        first before falling back to best-effort parsing. For details, see the property
        on `BaseMessage`.
        """
        if self.response_metadata.get("output_version") == "v1":
            return cast("list[types.ContentBlock]", self.content)

        model_provider = self.response_metadata.get("model_provider")
        if model_provider:
            from langchain_core.messages.block_translators import (  # noqa: PLC0415
                get_translator,
            )

            translator = get_translator(model_provider)
            if translator:
                try:
                    return translator["translate_content"](self)
                except NotImplementedError:
                    pass

        # Otherwise, use best-effort parsing
        blocks = super().content_blocks

        if self.tool_calls:
            # Add from tool_calls if missing from content
            content_tool_call_ids = {
                block.get("id")
                for block in self.content
                if isinstance(block, dict) and block.get("type") == "tool_call"
            }
            for tool_call in self.tool_calls:
                if (id_ := tool_call.get("id")) and id_ not in content_tool_call_ids:
                    tool_call_block: types.ToolCall = {
                        "type": "tool_call",
                        "id": id_,
                        "name": tool_call["name"],
                        "args": tool_call["args"],
                    }
                    if "index" in tool_call:
                        tool_call_block["index"] = tool_call["index"]  # type: ignore[typeddict-item]
                    if "extras" in tool_call:
                        tool_call_block["extras"] = tool_call["extras"]  # type: ignore[typeddict-item]
                    blocks.append(tool_call_block)

        # Best-effort reasoning extraction from additional_kwargs
        # Only add reasoning if not already present
        # Insert before all other blocks to keep reasoning at the start
        has_reasoning = any(block.get("type") == "reasoning" for block in blocks)
        if not has_reasoning and (
            reasoning_block := _extract_reasoning_from_additional_kwargs(self)
        ):
            blocks.insert(0, reasoning_block)

        return blocks

Domain

Subdomains

Called By

Frequently Asked Questions

What does content_blocks() do?
content_blocks() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/ai.py.
Where is content_blocks() defined?
content_blocks() is defined in libs/core/langchain_core/messages/ai.py at line 243.
What does content_blocks() call?
content_blocks() calls 1 function(s): content_blocks.
What calls content_blocks()?
content_blocks() is called by 1 function(s): content_blocks.

Analyze Your Own Codebase

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

Try Supermodel Free