Home / Function/ _format_message_content() — langchain Function Reference

_format_message_content() — langchain Function Reference

Architecture documentation for the _format_message_content() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  1f060587_17a0_0b26_d72a_02b23bd5460f["_format_message_content()"]
  2b046911_ea21_8e2e_ba0d_9d03da8d7bda["base.py"]
  1f060587_17a0_0b26_d72a_02b23bd5460f -->|defined in| 2b046911_ea21_8e2e_ba0d_9d03da8d7bda
  fd643003_13df_3a67_6bdc_07576981e414["_convert_message_to_dict()"]
  fd643003_13df_3a67_6bdc_07576981e414 -->|calls| 1f060587_17a0_0b26_d72a_02b23bd5460f
  style 1f060587_17a0_0b26_d72a_02b23bd5460f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/langchain_openai/chat_models/base.py lines 228–286

def _format_message_content(
    content: Any,
    api: Literal["chat/completions", "responses"] = "chat/completions",
    role: str | None = None,
) -> Any:
    """Format message content."""
    if content and isinstance(content, list):
        formatted_content = []
        for block in content:
            # Remove unexpected block types
            if (
                isinstance(block, dict)
                and "type" in block
                and (
                    block["type"] in ("tool_use", "thinking", "reasoning_content")
                    or (
                        block["type"] in ("function_call", "code_interpreter_call")
                        and api == "chat/completions"
                    )
                )
            ):
                continue
            if (
                isinstance(block, dict)
                and is_data_content_block(block)
                # Responses API messages handled separately in _compat (parsed into
                # image generation calls)
                and not (api == "responses" and str(role).lower().startswith("ai"))
            ):
                formatted_content.append(convert_to_openai_data_block(block, api=api))
            # Anthropic image blocks
            elif (
                isinstance(block, dict)
                and block.get("type") == "image"
                and (source := block.get("source"))
                and isinstance(source, dict)
            ):
                if source.get("type") == "base64" and (
                    (media_type := source.get("media_type"))
                    and (data := source.get("data"))
                ):
                    formatted_content.append(
                        {
                            "type": "image_url",
                            "image_url": {"url": f"data:{media_type};base64,{data}"},
                        }
                    )
                elif source.get("type") == "url" and (url := source.get("url")):
                    formatted_content.append(
                        {"type": "image_url", "image_url": {"url": url}}
                    )
                else:
                    continue
            else:
                formatted_content.append(block)
    else:
        formatted_content = content

    return formatted_content

Domain

Subdomains

Frequently Asked Questions

What does _format_message_content() do?
_format_message_content() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/base.py.
Where is _format_message_content() defined?
_format_message_content() is defined in libs/partners/openai/langchain_openai/chat_models/base.py at line 228.
What calls _format_message_content()?
_format_message_content() is called by 1 function(s): _convert_message_to_dict.

Analyze Your Own Codebase

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

Try Supermodel Free