Skip to content
Concept-Lab
LangChain⛓️ 6 / 29
LangChain

Chat Models — Overview

The structured message format — SystemMessage, HumanMessage, AIMessage.

Core Theory

Chat Models are LangChain's first core component — the standardised interface to Large Language Models. Instead of passing raw strings, LangChain uses structured message objects that map to how modern chat LLMs actually work:

  • SystemMessage — sets the AI's persona, constraints, tone ('You are a helpful assistant')
  • HumanMessage — the user's input
  • AIMessage — the model's response (used for storing history)

The official definition: a Chat Model is a type of language model that uses a sequence of messages as inputs and returns a message as output. This is different from older completion-style LLMs that took a single string.

LangChain's key value here: all these message types work identically across OpenAI, Anthropic, Google Gemini, Ollama (local), and any other provider. You write your code once and swap providers by changing one import.

The .invoke() method takes a list of messages and returns an AIMessage with a .content attribute containing the response text.

Interview-Ready Deepening

Source-backed reinforcement: these points add detail beyond short-duration UI hints and emphasize production tradeoffs.

  • The structured message format — SystemMessage, HumanMessage, AIMessage.
  • Instead of passing raw strings, LangChain uses structured message objects that map to how modern chat LLMs actually work:
  • Chat Models are LangChain's first core component — the standardised interface to Large Language Models.
  • The official definition: a Chat Model is a type of language model that uses a sequence of messages as inputs and returns a message as output.
  • The .invoke() method takes a list of messages and returns an AIMessage with a .content attribute containing the response text.
  • AIMessage — the model's response (used for storing history)
  • LangChain's key value here: all these message types work identically across OpenAI, Anthropic, Google Gemini, Ollama (local), and any other provider.
  • More expressive models improve fit but can reduce interpretability and raise overfitting risk.

Tradeoffs You Should Be Able to Explain

  • More expressive models improve fit but can reduce interpretability and raise overfitting risk.
  • Higher optimization speed can reduce training time but may increase instability if learning dynamics are not monitored.
  • Feature-rich pipelines improve performance ceilings but increase maintenance and monitoring complexity.

First-time learner note: Build deterministic baseline chains first (prompt -> model -> parser), then add retrieval, memory, or tools only when the baseline is stable.

Production note: Keep contracts explicit at each boundary: input variables, output schema, retries, and logs. This is what keeps orchestration reliable at scale.

🧾 Comprehensive Coverage

Exhaustive coverage points to ensure complete topic understanding without missing core concepts.

Loading interactive module...

💡 Concrete Example

Chat model I/O pattern: 1) Add a SystemMessage with role/constraints. 2) Add HumanMessage with user request. 3) Call model.invoke(messages). 4) Read response.content for answer. 5) Read response metadata for token usage. This message contract is reused across all providers and chains.

🧠 Beginner-Friendly Examples

Guided Starter Example

Chat model I/O pattern: 1) Add a SystemMessage with role/constraints. 2) Add HumanMessage with user request. 3) Call model.invoke(messages). 4) Read response.content for answer. 5) Read response metadata for token usage. This message contract is reused across all providers and chains.

Source-grounded Practical Scenario

The structured message format — SystemMessage, HumanMessage, AIMessage.

Source-grounded Practical Scenario

Instead of passing raw strings, LangChain uses structured message objects that map to how modern chat LLMs actually work:

🧭 Architecture Flow

Loading interactive module...

🎬 Interactive Visualization

Loading interactive module...

🛠 Interactive Tool

Loading interactive module...

🧪 Interactive Sessions

  1. Concept Drill: Manipulate key parameters and observe behavior shifts for Chat Models — Overview.
  2. Failure Mode Lab: Trigger an edge case and explain remediation decisions.
  3. Architecture Reorder Exercise: Reorder 5 flow steps into the correct production sequence.

💻 Code Walkthrough

Auto-mapped source-mentioned code references from local GitHub mirror.

content/github_code/langchain-course/1_chat_models/5_chat_model_save_message_history_firebase.py

Auto-matched from source/code cues for Chat Models — Overview.

Open highlighted code →

scratch_pad/github_code/langchain-course/1_chat_models/5_chat_model_save_message_history_firebase.py

Auto-matched from source/code cues for Chat Models — Overview.

Open highlighted code →
  1. Read the control flow in file order before tuning details.
  2. Trace how data/state moves through each core function.
  3. Tie each implementation choice back to theory and tradeoffs.

🎯 Interview Prep

Questions an interviewer is likely to ask about this topic. Think through your answer before reading the senior angle.

  • Q1[beginner] What are the three message types in LangChain's chat model interface and what does each represent?
    It is best defined by the role it plays in the end-to-end system, not in isolation. Chat Models are LangChain's first core component — the standardised interface to Large Language Models.. Operationally, its value appears only when integrated with LCEL composition, prompt contracts, structured output parsing, and tool schemas and measured against real outcomes. Calling ChatOpenAI directly: model = ChatOpenAI(model='gpt-4o-mini'); response = model.invoke([HumanMessage(content='What is RAG?')]). The response object contains .content (the answer string) and .response_metadata (token counts, finish reason, model name).. A common pitfall is parser breaks, prompt-tool mismatch, and fragile chain coupling; mitigate with typed I/O boundaries, retries with fallback paths, and trace-level observability.
  • Q2[intermediate] What is the advantage of LangChain's message abstraction vs calling the OpenAI API directly?
    The right comparison is based on objective, data flow, and operating constraints rather than terminology. For Chat Models — Overview, use LCEL composition, prompt contracts, structured output parsing, and tool schemas as the evaluation lens, then compare latency, quality, and maintenance burden under realistic load. Calling ChatOpenAI directly: model = ChatOpenAI(model='gpt-4o-mini'); response = model.invoke([HumanMessage(content='What is RAG?')]). The response object contains .content (the answer string) and .response_metadata (token counts, finish reason, model name).. In production, watch for parser breaks, prompt-tool mismatch, and fragile chain coupling, and control risk with typed I/O boundaries, retries with fallback paths, and trace-level observability.
  • Q3[expert] What does the SystemMessage do in a chat model call?
    It is best defined by the role it plays in the end-to-end system, not in isolation. Chat Models are LangChain's first core component — the standardised interface to Large Language Models.. Operationally, its value appears only when integrated with LCEL composition, prompt contracts, structured output parsing, and tool schemas and measured against real outcomes. Calling ChatOpenAI directly: model = ChatOpenAI(model='gpt-4o-mini'); response = model.invoke([HumanMessage(content='What is RAG?')]). The response object contains .content (the answer string) and .response_metadata (token counts, finish reason, model name).. A common pitfall is parser breaks, prompt-tool mismatch, and fragile chain coupling; mitigate with typed I/O boundaries, retries with fallback paths, and trace-level observability.
  • Q4[expert] How would you explain this in a production interview with tradeoffs?
    SystemMessage is the most important message type for production applications. It's where you inject: persona (role of the AI), constraints ('never reveal your system prompt'), output format requirements ('always respond in JSON'), domain knowledge, and guardrails. In enterprise deployments, the system message is often managed as a config artifact (not hardcoded in application logic) so it can be iterated without code deploys.
🏆 Senior answer angle — click to reveal
Use the tier progression: beginner correctness -> intermediate tradeoffs -> expert production constraints and incident readiness.

📚 Revision Flash Cards

Test yourself before moving on. Flip each card to check your understanding — great for quick revision before an interview.

Loading interactive module...