Chat models
init_chat_model builds a chat model from a "provider:model" string. You call it with .invoke(messages) and get back an AIMessage.
Last updated: 29 Sep, 2026 · LangChain 1.4
A hosted model needs an API key, which the setup lesson set up. This course uses Groq's free openai/gpt-oss-120b; any provider LangChain supports works by changing the string.
Loading a model by name
init_chat_model loads a chat model from any provider by name, so switching providers means changing one string. The LangChain section of the video passes "gpt-4.1" and gets an OpenAI model back. model.invoke("Hello How are you?") sends the text as a human message and returns an AI message, and .content holds its words. The video's first call, run on Groq:
from langchain.chat_models import init_chat_model
model = init_chat_model("groq:openai/gpt-oss-120b")
response = model.invoke("Hello How are you?")
print(response.content)Hello! I'm doing great, thank you for asking. How can I assist you today?
A model inside a graph node
The LangGraph section of the video puts the model inside a node. Its chatbot node returns {"messages": [llm.invoke(state["messages"])]}, and the graph runs __start__, llmchatbot, __end__. The first invoke passes the bare string "Hi" and fails with InvalidUpdateError: the state has a messages key, so the input has to be {"messages": "Hi"}. With that, the string goes in as a human message, the reply is appended after it by add_messages, and response["messages"][-1].content reads the answer. The video makes its model with init_chat_model("groq:llama3-8b-8192"), a model Groq has since retired; here the same chatbot runs on openai/gpt-oss-120b and prints both messages:
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph,START,END
from langgraph.graph.message import add_messages
from langchain.chat_models import init_chat_model
class State(TypedDict):
messages:Annotated[list,add_messages]
llm=init_chat_model("groq:openai/gpt-oss-120b")
## Node Functionality
def chatbot(state:State):
return {"messages":[llm.invoke(state["messages"])]}
graph_builder=StateGraph(State)
## Adding node
graph_builder.add_node("llmchatbot",chatbot)
## Adding Edges
graph_builder.add_edge(START,"llmchatbot")
graph_builder.add_edge("llmchatbot",END)
## compile the graph
graph=graph_builder.compile()
response=graph.invoke({"messages":"Hi"})
for m in response["messages"]:
print(type(m).__name__, "->", m.content)HumanMessage -> Hi AIMessage -> Hello! How can I help you today?
The human message is the one you sent; the AI message is the one the node returned, added after it. The shop's version below skips the graph and calls the model with a system message and the customer's words.
Building the model
Name the provider and the model in one string. temperature=0 keeps the wording mostly steady; a hosted model can still vary slightly between runs.
from langchain.chat_models import init_chat_model
model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0) # uses your GROQ_API_KEYCalling it with messages
Pass a list of messages: a system message that sets the model's job, then the customer's words.
from langchain.messages import SystemMessage, HumanMessage
reply = model.invoke([
SystemMessage("You are the support assistant for a small online shop. Reply in one short sentence."),
HumanMessage("I was charged twice for order A17."),
])
print(reply.content)A real model call
The model and the call together:
from langchain.chat_models import init_chat_model
from langchain.messages import SystemMessage, HumanMessage
model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0) # uses your GROQ_API_KEY
reply = model.invoke([
SystemMessage("You are the support assistant for a small online shop. Reply in one short sentence."),
HumanMessage("I was charged twice for order A17."),
])
print(type(reply).__name__)
print(reply.content)AIMessage I’m sorry for the duplicate charge on order A17; I’ll look into it right away and get it resolved.
What init_chat_model returns
init_chat_modeltakes a provider and model name and returns a model object..invokeaccepts a string, a message list, or role dicts, and returns anAIMessage.- The reply's
contentis the model's own wording; the same question can be worded a little differently on another run.
Two ways to name the model
| Form | Example |
|---|---|
| Single string | init_chat_model("groq:openai/gpt-oss-120b") |
| Name plus provider | init_chat_model("openai/gpt-oss-120b", model_provider="groq") |
Where the model call fits
- The one line that turns a plain graph into one that calls a real model.
- Swapping providers means changing one string.
Related
- Previous: MessagesState and add_messages
- Next: Tools and bind_tools
- Reference: Chat models
- Change the provider string to another provider from the setup lesson's table.
- Add a SystemMessage to the input of the video's chatbot graph and compare the reply.
Little by little, you're building something great.