Skip to main content

Chat Models

init_chat_model builds a chat model from a model name and provider string, so switching providers is a one-line change rather than a different import per vendor.

from langchain.chat_models import init_chat_model

model = init_chat_model("gpt-4o-mini", model_provider="openai")
# model = init_chat_model("claude-sonnet-4-5", model_provider="anthropic")

response = model.invoke("What is retrieval-augmented generation?")
print(response.content)

Common parameters

ParameterEffectSane default
temperaturerandomness of sampling; 0 is near-deterministic0 for extraction/tool-calling, 0.7 for creative text
max_tokenscaps output lengthset explicitly — providers cap differently and unbounded output costs money
timeoutseconds before the call is abandoned30–60s for interactive use
max_retriesautomatic retry count on transient errors2
model = init_chat_model(
"gpt-4o-mini",
model_provider="openai",
temperature=0,
max_tokens=512,
timeout=30,
max_retries=2,
)
tip

Provider portability is real for the basics (invoke, stream, temperature) and leaky for the advanced features — batch structured-output modes, prompt caching controls, and reasoning-effort knobs differ per provider. Check the provider's integration page before relying on anything beyond the core Runnable methods.

See also