← All notes

LangChain messages: SystemMessage, HumanMessage, AIMessage and ToolMessage carry role, content and metadata

Answer: Build a list of message objects (SystemMessage, HumanMessage, AIMessage, ToolMessage) and pass it to model.invoke(messages), which returns an AIMessage. Messages are the fundamental unit of context for models in LangChain and carry a role, content and optional metadata.

Example

from langchain.chat_models import init_chat_model
from langchain.messages import SystemMessage, HumanMessage, AIMessage, ToolMessage

model = init_chat_model("gpt-5-nano")
messages = [
    SystemMessage("You are a poetry expert"),
    HumanMessage("Write a haiku about spring"),
]
response = model.invoke(messages)          # AIMessage

ai = AIMessage(content=[], tool_calls=[{"name": "get_weather",
     "args": {"location": "San Francisco"}, "id": "call_123"}])
result = ToolMessage(content="Sunny, 72F", tool_call_id="call_123")

Message types

| Type | Role | |---|---| | SystemMessage | an initial set of instructions that primes the model's behavior: tone, role, guidelines | | HumanMessage | user input and interactions with the model | | AIMessage | responses generated by the model, including text content, tool calls, and metadata | | ToolMessage | passes the results of a single tool execution back to the model |

Three input formats

AIMessage fields

A ToolMessage's tool_call_id must match the call ID from the AIMessage. LangChain's standard message type works across all model providers.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: LangChain docs, Messages; langchain-ai/docs (GitHub), messages.mdx (source page). MIT (https://raw.githubusercontent.com/langchain-ai/docs/main/LICENSE). Copyright LangChain. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: LangChain docs, Messages; langchain-ai/docs (GitHub), messages.mdx (source page). MIT (https://raw.githubusercontent.com/langchain-ai/docs/main/LICENSE). Copyright LangChain.

Cite this

Permanent ID: iil:note:am853768

iinventedlight. 2026. "LangChain messages: SystemMessage, HumanMessage, AIMessage and ToolMessage carry role, content and metadata." iinventedlight Library, iil:note:am853768, version of 2026-11-18. https://iinventedlight.com/library/notes/langchain-messages/. License: CC-BY-4.0.

@misc{iil_note_am853768,
  author = {{iinventedlight}},
  title = {{LangChain messages: SystemMessage, HumanMessage, AIMessage and ToolMessage carry role, content and metadata}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/langchain-messages/},
  note = {Permanent ID iil:note:am853768. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/langchain-messages.md.