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:

ParameterTypeDescriptionDefault Value
modelstr or NoneModel name, in provider:model format. Passing None can be used to create a configurable model.None
model_providerstr or NoneSpecify the provider separately. Used when dynamically obtained or when model cannot be inferred.None
configurable_fields"any", list, or NoneList of fields that can be modified at runtime. None means a fixed model.None
config_prefixstr or NonePrefix for configuration keys in multi-model scenarios to avoid conflicts.None
**kwargsdictParameters 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

from langchain.chat_models import init_chat_model

# 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

from langchain.chat_models import init_chat_model

# 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 prefixInferred provider
gpt-、o1、o3、chatgpt、text-davinciopenai
claudeanthropic
geminigoogle_vertexai
commandcohere
deepseekdeepseek
mistral、mixtralmistralai
grokxai
sonarperplexity
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

# The following two ways of writing are completely equivalent
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

from langchain.chat_models import init_chat_model

# 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

from langchain.chat_models import init_chat_model

# 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 nameInstallation packageRepresentative model
openailangchain-deepseekgpt-4o、gpt-4o-mini
anthropiclangchain-anthropicclaude-sonnet-4-5、claude-opus-4-7
google_genailangchain-google-genaigemini-2.5-flash、gemini-2.5-pro
google_vertexailangchain-google-vertexaigemini-2.5-flash、gemini-2.5-pro
deepseeklangchain-deepseekdeepseek-chat、deepseek-reasoner
mistralailangchain-mistralaimistral-large、mistral-small
groqlangchain-groqllama-3.3-70b、mixtral-8x7b
ollamalangchain-ollamallama3.2、qwen2.5
fireworkslangchain-fireworksaccounts/fireworks/models/llama-v3p1-70b
togetherlangchain-togethermeta-llama/Llama-3.3-70B
xailangchain-xaigrok-3
openrouterlangchain-openrouteropenai/gpt-4o、anthropic/claude-sonnet
perplexitylangchain-perplexitysonar、sonar-pro
huggingfacelangchain-huggingfaceVarious HuggingFace models
coherelangchain-coherecommand-r-plus

Common kwargs Parameters

kwargs parameters are passed directly to the underlying model class. Commonly used ones include:

Example

from langchain.chat_models import init_chat_model

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)
ParameterTypeDescriptionApplicable providers
temperaturefloatControls randomness, 0~2, default value varies by modelMost
max_tokensintLimits the maximum number of output tokensAll
timeoutint or floatRequest timeout in secondsAll
max_retriesintNumber of retries after request failureMost
base_urlstrCustom API endpointMost
rate_limiterBaseRateLimiterRate limiter instanceMost
top_pfloatNucleus sampling parameter, 0~1Most
stoplist[str]Stop sequences; the model stops generating when it encounters these wordsMost

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

from langchain.chat_models import init_chat_model

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

ValueMeaning
NoneNot 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

When 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.

Other Extensions