LangChain Model Invocation -- init_chat_model() Function
LangChain init_chat_model() is one of the most commonly used functions in LangChain. It lets you connect to over 20 model providers in a unified way, without needing to remember each provider's class names and parameter differences.
Syntax
The syntax of the init_chat_model() function is as follows:
from langchain.chat_models import init_chat_model
# 完整语法
model = init_chat_model(
model, # str | None:模型名称(provider:model 格式)
*,
model_provider=None, # str | None:单独的模型提供商
configurable_fields=None, # None | "any" | list[str]:可运行时修改的字段
config_prefix=None, # str | None:配置键前缀
**kwargs, # 模型特定参数(temperature、max_tokens 等)
)Parameter Description:
| Parameter | Type | Description | Default Value |
|---|---|---|---|
| model | str or None | Model name, in provider:model format. Passing None can be used to create a configurable model. | None |
| model_provider | str or None | Specify the provider separately. Used when dynamically obtained or when model cannot be inferred. | None |
| configurable_fields | "any", list, or None | List of fields that can be modified at runtime. None means a fixed model. | None |
| config_prefix | str or None | Prefix for configuration keys in multi-model scenarios to avoid conflicts. | None |
| **kwargs | dict | Parameters passed to the underlying model. | None |
Detailed Explanation of the model Parameter
provider:model format (recommended)
The format isprovider:model name, separated by a colon:
Example
# provider:model format
model = init_chat_model("deepseek:deepseek-v4-flash")
model = init_chat_model("anthropic:claude-sonnet-4-5-20250929")
model = init_chat_model("deepseek:deepseek-chat")
model = init_chat_model("ollama:llama3.2")
model = init_chat_model("groq:llama-3.3-70b")
Automatically Inferring the Model Provider
If you don't specify the provider prefix, LangChain will try to infer it from the model name:
Example
# Automatically infer provider (based on model name prefix)
model = init_chat_model("deepseek-v4-flash") # → openai
model = init_chat_model("claude-sonnet-4-5") # → anthropic
model = init_chat_model("deepseek-chat") # → deepseek
model = init_chat_model("grok-3") # → xai
model = init_chat_model("mistral-large") # → mistralai
| Model name prefix | Inferred provider |
|---|---|
| gpt-、o1、o3、chatgpt、text-davinci | openai |
| claude | anthropic |
| gemini | google_vertexai |
| command | cohere |
| deepseek | deepseek |
| mistral、mixtral | mistralai |
| grok | xai |
| sonar | perplexity |
| amazon.、anthropic.、meta. | bedrock |
Automatic inference is convenient, but not guaranteed to be 100% correct. For example, the gemini prefix may point to google_vertexai or google_genai, and future versions may change the default inference result. It is recommended to always use the provider:model format in production environments.
model_provider Parameter
When model_provider is specified separately, the effect is equivalent to the provider:model format:
Example
model = init_chat_model("claude-sonnet-4-5", model_provider="anthropic")
model = init_chat_model("anthropic:claude-sonnet-4-5")
Scenarios for using model_provider:
- When dynamically reading the provider name from a configuration file
- When the provider name and model name need to be configured independently (switched separately at runtime)
- When the model name cannot be automatically inferred and it is inconvenient to concatenate strings
Fixed Model vs Configurable Model
init_chat_model() has two usage modes:
Mode 1: Fixed Model
Specify a specific model string, returns a BaseChatModel instance that can be used directly:
Example
# model specified, returns a fixed model
model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0.7)
response = model.invoke("Introduce Python Python")
print(response.content)
If .env is in the current directory, use the following code to load the configuration from the current path:
import os from dotenv import load_dotenv # 加载当前目录 .env 文件 load_dotenv() from langchain.chat_models import init_chat_model # 指定了 model,返回固定模型 model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0.7) response = model.invoke("介绍Example") print(response.content)
Mode 2: Configurable Model
Don't specify model (or set it to None) to create a model that can be dynamically switched at runtime:
Example
# Don't specify model, returns a configurable model
# You can fix some parameters (e.g., temperature=0.7), and specify the rest at runtime
configurable_model = init_chat_model(temperature=0.7)
# Specify the model via config at runtime
response = configurable_model.invoke(
"Introduce Python Python",
config={"configurable": {"model": "deepseek-v4-flash"}}
)
print(response.content)
# The same model instance can execute with different models
response = configurable_model.invoke(
"Introduce Python Python",
config={"configurable": {"model": "claude-sonnet-4-5"}}
)
print(response.content)
Configurable models are very useful in A/B testing and cost optimization. You can switch models or adjust parameters by modifying the configuration without restarting the service.
All Supported Model Providers
The following are the providers natively supported by init_chat_model() and their installation packages:
| provider name | Installation package | Representative model |
|---|---|---|
| openai | langchain-deepseek | gpt-4o、gpt-4o-mini |
| anthropic | langchain-anthropic | claude-sonnet-4-5、claude-opus-4-7 |
| google_genai | langchain-google-genai | gemini-2.5-flash、gemini-2.5-pro |
| google_vertexai | langchain-google-vertexai | gemini-2.5-flash、gemini-2.5-pro |
| deepseek | langchain-deepseek | deepseek-chat、deepseek-reasoner |
| mistralai | langchain-mistralai | mistral-large、mistral-small |
| groq | langchain-groq | llama-3.3-70b、mixtral-8x7b |
| ollama | langchain-ollama | llama3.2、qwen2.5 |
| fireworks | langchain-fireworks | accounts/fireworks/models/llama-v3p1-70b |
| together | langchain-together | meta-llama/Llama-3.3-70B |
| xai | langchain-xai | grok-3 |
| openrouter | langchain-openrouter | openai/gpt-4o、anthropic/claude-sonnet |
| perplexity | langchain-perplexity | sonar、sonar-pro |
| huggingface | langchain-huggingface | Various HuggingFace models |
| cohere | langchain-cohere | command-r-plus |
Common kwargs Parameters
kwargs parameters are passed directly to the underlying model class. Commonly used ones include:
Example
model = init_chat_model(
"deepseek:deepseek-v4-flash",
# Controls output randomness (0~2), the smaller the value, the more stable the output
temperature=0.3,
# Limits the maximum number of output tokens (controls cost)
max_tokens=200,
# Request timeout (seconds)
timeout=30,
# Number of retries after failure
max_retries=2,
# Custom API address (proxy/relay scenarios)
# base_url="https://your-proxy.com/v1",
# Rate limiter (controls request frequency)
# rate_limiter=MyRateLimiter(requests_per_second=5),
)
response = model.invoke("What is Python Python?")
print(response.content)
| Parameter | Type | Description | Applicable providers |
|---|---|---|---|
| temperature | float | Controls randomness, 0~2, default value varies by model | Most |
| max_tokens | int | Limits the maximum number of output tokens | All |
| timeout | int or float | Request timeout in seconds | All |
| max_retries | int | Number of retries after request failure | Most |
| base_url | str | Custom API endpoint | Most |
| rate_limiter | BaseRateLimiter | Rate limiter instance | Most |
| top_p | float | Nucleus sampling parameter, 0~1 | Most |
| stop | list[str] | Stop sequences; the model stops generating when it encounters these words | Most |
temperature and top_p are usually not set at the same time. temperature controls the "shape of the distribution", while top_p controls the "candidate range". For most scenarios, adjusting only temperature is sufficient.
ConfigurableModel—Runtime Model Switching
ConfigurableModel is an advanced usage of init_chat_model(), allowing you to dynamically specify models and parameters at runtime:
Example
# Create a configurable model and set default values
model = init_chat_model(
"deepseek:deepseek-v4-flash", # Default model
configurable_fields="any", # All parameters can be modified at runtime
config_prefix="my", # Configuration key prefix
temperature=0.3, # Default temperature
)
# Run with default configuration
response = model.invoke("Introduce Python")
print(f"Default config: {response.content[:50]}...")
# Override model and parameters at runtime (note the my_ prefix)
response = model.invoke(
"Introduce Python Python",
config={
"configurable": {
"my_model": "deepseek:deepseek-v4-pro", # Switch model
"my_temperature": 0.9, # Adjust temperature
}
}
)
print(f"Override config: {response.content[:50]}...")
Possible values of configurable_fields:
| Value | Meaning |
|---|---|
| None | Not configurable, returns an ordinary BaseChatModel (fixed model mode) |
| "any" | All parameters are configurable (note the security risk: api_key etc. can also be modified) |
| ["model", "temperature"] | Only the fields specified in the list are configurable |
Other ExtensionsWhen using configurable_fields="any", be careful about security: if you trust an insecure configuration source, sensitive fields such as api_key and base_url may be tampered with. In production environments, it is recommended to explicitly list the configurable fields.