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-coremessagesaiAIMessage
    Class●Since v0.1

    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.

    Copy
    AIMessage(
      self,
      content: str | list[str | dict] | None = None,
      content_blocks: list[types.ContentBlock] | None = None,
      **kwargs: Any = {}
    )

    Bases

    BaseMessage

    Used in Docs

    • Agents
    • Build a custom SQL agent
    • Custom middleware
    • Frontend
    • Guardrails
    (11 more not shown)

    Parameters

    NameTypeDescription
    contentstr | list[str | dict] | None
    Default:None

    The content of the message.

    content_blockslist[types.ContentBlock] | None
    Default:None

    Typed standard content.

    **kwargsAny
    Default:{}

    Additional arguments to pass to the parent class.

    Constructors

    constructor
    __init__
    NameType
    contentstr | list[str | dict] | None
    content_blockslist[types.ContentBlock] | None

    Attributes

    attribute
    tool_calls: list[ToolCall]

    If present, tool calls associated with the message.

    attribute
    invalid_tool_calls: list[InvalidToolCall]

    If present, tool calls with parsing errors associated with the message.

    attribute
    usage_metadata: UsageMetadata | None

    If present, usage metadata for a message, such as token counts.

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

    attribute
    type: Literal['ai']

    The type of the message (used for deserialization).

    attribute
    lc_attributes: dict

    Attributes to be serialized.

    Includes all attributes, even if they are derived from other initialization arguments.

    attribute
    content_blocks: 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.

    Methods

    method
    pretty_repr

    Return a pretty representation of the message for display.

    Inherited fromBaseMessage

    Attributes

    Acontent: str | list[str | dict]
    —

    The contents of the message.

    Aadditional_kwargs: dict
    —

    Currently inherited from BaseMessage, but not used.

    Aresponse_metadata: dict
    —

    Currently inherited from BaseMessage, but not used.

    Aname: str
    —

    The name of the function.

    Aid: str
    —

    The unique identifier of the node.

    Amodel_configAtext: str
    —

    Prompt text.

    Methods

    Mis_lc_serializable
    —

    Return True as this class is serializable.

    Mget_lc_namespace
    —

    Get the namespace of the LangChain object.

    Mpretty_print
    —

    Print a pretty representation of the message.

    Inherited fromSerializable

    Attributes

    Alc_secrets: dict[str, str]
    —

    A map of constructor argument names to secret ids.

    Amodel_config

    Methods

    Mis_lc_serializable
    —

    Return True as this class is serializable.

    Mget_lc_namespace
    —

    Get the namespace of the LangChain object.

    Mlc_id
    —

    Return a unique identifier for this class for serialization purposes.

    Mto_json
    —

    Convert the graph to a JSON-serializable format.

    Mto_json_not_implemented
    —

    Serialize a "not implemented" object.

    View source on GitHub