LangGraphLangGraph 1.2 · Python 3.10+
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
38 small wins to finish your pathNext lesson →

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 models with init_chat_model · from the Complete Agentic AI Course In 10 Hours · 39:40 to 44:46

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:

ExampleAPI keyFrom the video, 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)
Running the basic chatbot graph · from the Complete Agentic AI Course In 10 Hours · 197:13 to 201:12

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:

ExampleAPI keyFrom the video, run on Groq
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)

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.

python
from langchain.chat_models import init_chat_model

model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0)  # uses your GROQ_API_KEY

Calling it with messages

Pass a list of messages: a system message that sets the model's job, then the customer's words.

python
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:

ExampleAPI key
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)

What init_chat_model returns

  • init_chat_model takes a provider and model name and returns a model object.
  • .invoke accepts a string, a message list, or role dicts, and returns an AIMessage.
  • The reply's content is the model's own wording; the same question can be worded a little differently on another run.

Two ways to name the model

FormExample
Single stringinit_chat_model("groq:openai/gpt-oss-120b")
Name plus providerinit_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.
Watch out. Model ids change as providers retire models. Check the provider's current list before shipping, and keep the id in one place so a swap is one edit.
Try it yourself
  • 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.