Skip to main content

How to Create a Chat Model with init_chat_model in LangChain

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

init_chat_model is LangChain's single way in to every chat model: you give it the provider and the model's name in one string, such as "anthropic:claude-opus-5-5", and it returns a model object you can call. Switching to another model is then a change to that string. This guide creates a Claude chat model with LangChain 1.5.2, asks it a question, compares the three ways to name the provider, and shows the setting that Claude's newer models refuse.

Syntax​

Syntax
from langchain.chat_models import init_chat_model

model = init_chat_model("<provider>:<model>", <setting>=<value>, ...)
reply = model.invoke("<question>")
PartWhat it means
<provider>Who runs the model, such as anthropic for Claude. That provider's integration package has to be installed, here langchain-anthropic
<model>The model's ID at that provider, such as claude-opus-5-5
<setting>=<value>Optional settings, passed on to the provider's model class, such as max_tokens=1000
model.invoke()Sends the question to the model and waits for the whole reply
replyAn AIMessage; reply.text is the reply's text

LangChain's reference for init_chat_model describes the first argument as the "Name of the model to use, with provider prefix — e.g., 'openai:gpt-5.5'."

The Example​

A project called primary-colours asks Claude Opus 5.5 for the three primary colours of light. A second script creates the same model in the three ways init_chat_model accepts, and a third tries the temperature setting.

Prerequisites​

  • uv. This guide uses uv 0.13.10 with Python 3.14.10.
  • A Claude API key with credit. The Claude API has no free tier. Claude Opus 5.5 costs $4 per million input tokens and $20 per million output tokens, and a one-line question like this one uses a few dozen tokens each way.

Step 1: Create the Project​

Terminal
uv init --python 3.14 --no-package primary-colours
cd primary-colours
uv add langchain langchain-anthropic python-dotenv
Output
Initialized project `primary-colours` at `/Users/you/source/primary-colours`
Using CPython 3.14.10
Creating virtual environment at: .venv
Resolved 47 packages in 32ms
Installed 45 packages in 161ms

The list of the 45 packages follows; among them are langchain==1.5.2, langchain-core==1.9.1, langchain-anthropic==1.9.0, anthropic==2.17.0 and python-dotenv==1.2.4. python-dotenv reads the API key from a .env file, so put your key in a file called .env in the project folder:

.env
ANTHROPIC_API_KEY=sk-ant-your-key-here

Keep that file out of Git: How to Keep a .env File Out of Git with .gitignore shows how. The other ways to hand LangChain the key are in How to Set the Anthropic API Key for LangChain in Python.

Step 2: Create the Model and Ask It a Question​

main.py
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model

load_dotenv() # Reads ANTHROPIC_API_KEY from the .env file.

model = init_chat_model("anthropic:claude-opus-5-5", max_tokens=1000)
reply = model.invoke("Name the three primary colours of light, separated by commas.")
print(reply.text)

The main.py script in a macOS-style code window: it loads the .env file, creates the model with init_chat_model("anthropic", max_tokens=1000), invokes it with a question about the primary colours of light and prints reply.text.

load_dotenv() puts the key from .env where Anthropic's library looks for it. init_chat_model("anthropic:claude-opus-5-5", max_tokens=1000) creates the model: anthropic before the colon picks the Claude integration, and claude-opus-5-5 after it picks the model. max_tokens=1000 caps the reply at 1,000 tokens, so one answer can't run long. model.invoke(...) sends the question, and reply.text is the answer as plain text.

Step 3: Run It​

Terminal
uv run main.py
Output
Red, green, blue

The terminal after uv run main.py: Claude answers Red, green, blue.

Claude answered in the format the question asked for. Your wording may differ, because the model writes each answer anew. The command works unchanged in PowerShell.

Step 4: Name the Provider in Three Ways​

init_chat_model also accepts the model's name without the provider, and the provider as a separate argument. This script creates the model all three ways, and prints which class each one made and which model it uses. It doesn't call Claude, so it needs no key:

names.py
from langchain.chat_models import init_chat_model

with_prefix = init_chat_model("anthropic:claude-opus-5-5")
guessed = init_chat_model("claude-opus-5-5")
separate = init_chat_model("claude-opus-5-5", model_provider="anthropic")

print(type(with_prefix).__name__, with_prefix.model)
print(type(guessed).__name__, guessed.model)
print(type(separate).__name__, separate.model)

The names.py script in a macOS-style code window: three models are created with the prefixed name, the bare name, and the bare name plus model_provider="anthropic", then the class name and model of each are printed.

Terminal
uv run names.py
Output
ChatAnthropic claude-opus-5-5
ChatAnthropic claude-opus-5-5
ChatAnthropic claude-opus-5-5

The terminal after uv run names.py: three lines, each ChatAnthropic claude-opus-5-5.

All three made a ChatAnthropic, LangChain's class for Claude, with the same model. The bare name worked because LangChain guesses the provider from how the name starts, and the reference lists claude... as anthropic. It also warns that "Inference is best-effort and not guaranteed, so prefer the prefixed form when possible." model_provider="anthropic" is the third form; the reference says model='claude-sonnet-4-5', model_provider='anthropic' "behaves the same as model='anthropic:claude-sonnet-4-5'".

Settings That Newer Claude Models Refuse​

Many tutorials set temperature=0 to make answers more predictable. On Claude Opus 5.5 that setting fails:

temperature.py
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model

load_dotenv()

model = init_chat_model("anthropic:claude-opus-5-5", temperature=0)
reply = model.invoke("Name the three primary colours of light, separated by commas.")
print(reply.text)
Terminal
uv run temperature.py
Output
Traceback (most recent call last):
...
langchain_anthropic.chat_models.AnthropicInvalidRequestError: 400 `temperature` is deprecated for this model. (request_id: req_011C…, path: /v1/messages, type: invalid_request_error)

The terminal after uv run temperature.py: the traceback, shortened, ends with AnthropicInvalidRequestError: 400 temperature is deprecated for this model.

I've shortened the traceback to its first and last lines. Creating the model worked; Claude's API rejected the request when invoke sent it. LangChain's Claude integration page explains: "Newer Claude models reject non-default sampling parameters", and its advice is to "Remove the sampling parameters rather than adjusting their values". The same goes for top_p and top_k. Leave them out, as main.py does, and steer the answer with the wording of your question instead.

Common Mistakes​

MistakeWhat happensDo this instead
Installing langchain without langchain-anthropicImportError: Initializing ChatAnthropic requires the langchain-anthropic package. Please install it with `pip install langchain-anthropic`uv add langchain-anthropic. With uv, use uv add rather than the pip install the message suggests
Setting temperature, top_p or top_k on a newer Claude modelAnthropicInvalidRequestError: 400 `temperature` is deprecated for this model.Remove the setting
Relying on the bare model nameWorks for claude-... names, but the provider is a guessWrite the prefix: "anthropic:claude-opus-5-5"

Troubleshooting​

SymptomLikely causeFix
TypeError: Anthropic authentication failed: no API key or authorization credentials were provided.No key reached the modelCheck the .env file and load_dotenv(); Fixed: TypeError: Anthropic authentication failed covers a script outside the project
401 invalid x-api-keyThe key is wrong, revoked, or an old one set in the terminal wins over .envSee How to Set the Anthropic API Key for LangChain in Python
400 ... deprecated for this modelA sampling setting such as temperatureRemove it

FAQ​

What does init_chat_model return?​

An object of the provider's own class: ChatAnthropic for Claude, as names.py printed. You can also create that class directly, with from langchain_anthropic import ChatAnthropic. init_chat_model saves you from knowing each provider's class and import.

How do I use another provider's model?​

Install its integration package and change the string: the reference's example is 'openai:gpt-5.5', which needs langchain-openai and an OpenAI key. The rest of the code stays the same.

Why claude-opus-5-5 without a date?​

Claude's model IDs are fixed snapshots. Anthropic's models overview says "Every Claude model ID is a pinned snapshot, including the dateless IDs used from the 4.6 generation on." The reference's advice to "Prefer pinned model IDs over moving aliases" is therefore met by claude-opus-5-5.

Conclusion​

init_chat_model("anthropic:claude-opus-5-5", max_tokens=1000) created a ChatAnthropic model, invoke sent it a question, and reply.text printed Red, green, blue. The bare name and model_provider="anthropic" made the same model, and temperature=0 was rejected with a 400 error, because newer Claude models refuse non-default sampling settings.

Additional Resources​