LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-coremessagesai
    Module●Since v0.1

    ai

    AI message.

    Attributes

    attribute
    LC_AUTO_PREFIX: str

    LangChain auto-generated ID prefix for messages and content blocks.

    attribute
    LC_ID_PREFIX: str

    Internal tracing/callback system identifier.

    Used for:

    • Tracing. Every LangChain operation (LLM call, chain execution, tool use, etc.) gets a unique run_id (UUID)
    • Enables tracking parent-child relationships between operations
    attribute
    logger

    Functions

    function
    merge_content

    Merge multiple message contents.

    function
    default_tool_chunk_parser

    Best-effort parsing of tool chunks.

    function
    default_tool_parser

    Best-effort parsing of tools.

    function
    create_invalid_tool_call

    Create an invalid tool call.

    function
    create_tool_call

    Create a tool call.

    function
    create_tool_call_chunk

    Create a tool call chunk.

    function
    merge_dicts

    Merge dictionaries.

    Merge many dicts, handling specific scenarios where a key exists in both dictionaries but has a value of None in 'left'. In such cases, the method uses the value from 'right' for that key in the merged dictionary.

    function
    merge_lists

    Add many lists, handling None.

    function
    parse_partial_json

    Parse a JSON string that may be missing closing braces.

    function
    add_ai_message_chunks

    Add multiple AIMessageChunks together.

    function
    add_usage

    Recursively add two UsageMetadata objects.

    function
    subtract_usage

    Recursively subtract two UsageMetadata objects.

    Token counts cannot be negative so the actual operation is max(left - right, 0).

    Classes

    class
    BaseMessage

    Base abstract message class.

    Messages are the inputs and outputs of a chat model.

    Examples include HumanMessage, AIMessage, and SystemMessage.

    class
    BaseMessageChunk

    Message chunk, which can be concatenated with other Message chunks.

    class
    InvalidToolCall

    Allowance for errors made by LLM.

    Here we add an error key to surface errors made during generation (e.g., invalid JSON arguments.)

    class
    ToolCall

    Represents an AI's request to call a tool.

    class
    ToolCallChunk

    A chunk of a tool call (yielded when streaming).

    When merging ToolCallChunk objects (e.g., via AIMessageChunk.__add__), all string attributes are concatenated. Chunks are only merged if their values of index are equal and not None.

    Example:

    left_chunks = [ToolCallChunk(name="foo", args='{"a":', index=0)]
    right_chunks = [ToolCallChunk(name=None, args="1}", index=0)]
    
    (
        AIMessageChunk(content="", tool_call_chunks=left_chunks)
        + AIMessageChunk(content="", tool_call_chunks=right_chunks)
    ).tool_call_chunks == [ToolCallChunk(name="foo", args='{"a":1}', index=0)]
    class
    InputTokenDetails

    Breakdown of input token counts.

    Does not need to sum to full input token count. Does not need to have all keys.

    class
    OutputTokenDetails

    Breakdown of output token counts.

    Does not need to sum to full output token count. Does not need to have all keys.

    class
    UsageMetadata

    Usage metadata for a message, such as token counts.

    This is a standard representation of token usage that is consistent across models.

    class
    AIMessage

    Message from an AI.

    An AIMessage is returned from a chat model as a response to a prompt.

    This message represents the output of the model and consists of both the raw output as returned by the model and standardized fields (e.g., tool calls, usage metadata) added by the LangChain framework.

    class
    AIMessageChunk

    Message chunk from an AI (yielded when streaming).

    Modules

    module
    types

    Standard, multimodal content blocks for Large Language Model I/O.

    This module provides standardized data structures for representing inputs to and outputs from LLMs. The core abstraction is the Content Block, a TypedDict.

    Rationale

    Different LLM providers use distinct and incompatible API schemas. This module provides a unified, provider-agnostic format to facilitate these interactions. A message to or from a model is simply a list of content blocks, allowing for the natural interleaving of text, images, and other content in a single ordered sequence.

    An adapter for a specific provider is responsible for translating this standard list of blocks into the format required by its API.

    Extensibility

    Data not yet mapped to a standard block may be represented using the NonStandardContentBlock, which allows for provider-specific data to be included without losing the benefits of type checking and validation.

    Furthermore, provider-specific fields within a standard block are fully supported by default in the extras field of each block. This allows for additional metadata to be included without breaking the standard structure. For example, Google's thought signature:

    AIMessage(
        content=[
            {
                "type": "text",
                "text": "J'adore la programmation.",
                "extras": {"signature": "EpoWCpc..."},  # Thought signature
            }
        ], ...
    )
    Note

    Following widespread adoption of PEP 728, we intend to add extra_items=Any as a param to Content Blocks. This will signify to type checkers that additional provider-specific fields are allowed outside of the extras field, and that will become the new standard approach to adding provider-specific metadata.

    Note

    Example with PEP 728 provider-specific fields:

    # Content block definition
    # NOTE: `extra_items=Any`
    class TextContentBlock(TypedDict, extra_items=Any):
        type: Literal["text"]
        id: NotRequired[str]
        text: str
        annotations: NotRequired[list[Annotation]]
        index: NotRequired[int]
    from langchain_core.messages.content import TextContentBlock
    
    # Create a text content block with provider-specific fields
    my_block: TextContentBlock = {
        # Add required fields
        "type": "text",
        "text": "Hello, world!",
        # Additional fields not specified in the TypedDict
        # These are valid with PEP 728 and are typed as Any
        "openai_metadata": {"model": "gpt-4", "temperature": 0.7},
        "anthropic_usage": {"input_tokens": 10, "output_tokens": 20},
        "custom_field": "any value",
    }
    
    # Mutating an existing block to add provider-specific fields
    openai_data = my_block["openai_metadata"]  # Type: Any

    Example Usage

    # Direct construction
    from langchain_core.messages.content import TextContentBlock, ImageContentBlock
    
    multimodal_message: AIMessage(
        content_blocks=[
            TextContentBlock(type="text", text="What is shown in this image?"),
            ImageContentBlock(
                type="image",
                url="https://www.langchain.com/images/brand/langchain_logo_text_w_white.png",
                mime_type="image/png",
            ),
        ]
    )
    
    # Using factories
    from langchain_core.messages.content import create_text_block, create_image_block
    
    multimodal_message: AIMessage(
        content=[
            create_text_block("What is shown in this image?"),
            create_image_block(
                url="https://www.langchain.com/images/brand/langchain_logo_text_w_white.png",
                mime_type="image/png",
            ),
        ]
    )

    Factory functions offer benefits such as:

    • Automatic ID generation (when not provided)
    • No need to manually specify the type field
    View source on GitHub