init_chat_model
Requires the integration package for the chosen model provider to be installed (e.g.,
pip install langchain-openai).Refer to the provider integration’s API reference for supported model parameters to use as **kwargs.Two main use cases
- Fixed model – specify the model upfront and get a ready-to-use chat model.
- Configurable model – choose to specify parameters (including model name) at runtime via
config. Makes it easy to switch between models/providers without changing your code.
The model name, optionally prefixed with provider (e.g.,
'openai:gpt-4o').Prefer exact model IDs from provider docs over aliases for reliable behavior (e.g., dated versions like '...-20250514' instead of '...-latest').Will attempt to infer model_provider from model if not specified.The following providers will be inferred based on these model prefixes:gpt-...|o1...|o3...→openaiclaude...→anthropicamazon...→bedrockgemini...→google_vertexaicommand...→cohereaccounts/fireworks...→fireworksmistral...→mistralaideepseek...→deepseekgrok...→xaisonar...→perplexitysolar...→upstage
The model provider if not specified as part of the model arg (see above).Supported
model_provider values and the corresponding integration package are:openai→langchain-openaianthropic→langchain-anthropicazure_openai→langchain-openaiazure_ai→langchain-azure-aigoogle_vertexai→langchain-google-vertexaigoogle_genai→langchain-google-genaianthropic_bedrock→langchain-awsbedrock→langchain-awsbedrock_converse→langchain-awscohere→langchain-coherefireworks→langchain-fireworkstogether→langchain-togethermistralai→langchain-mistralaihuggingface→langchain-huggingfacegroq→langchain-groqollama→langchain-ollamagoogle_anthropic_vertex→langchain-google-vertexaideepseek→langchain-deepseekibm→langchain-ibmnvidia→langchain-nvidia-ai-endpointsxai→langchain-xaiopenrouter→langchain-openrouterperplexity→langchain-perplexityupstage→langchain-upstage
Which model parameters are configurable at runtime:
None: No configurable fields (i.e., a fixed model).'any': All fields are configurable. See security note below.list[str] | Tuple[str, ...]: Specified fields are configurable.
config_prefix stripped if a config_prefix is specified.If model is specified, then defaults to None.If model is not specified, then defaults to ("model", "model_provider").Optional prefix for configuration keys.Useful when you have multiple configurable models in the same application.If
'config_prefix' is a non-empty string then model will be configurable at runtime via the config["configurable"]["{config_prefix}_{param}"] keys. See examples below.If 'config_prefix' is an empty string then model will be configurable via config["configurable"]["{param}"].Additional model-specific keyword args to pass to the underlying chat model’s
__init__ method. Common parameters include:temperature: Model temperature for controlling randomness.max_tokens: Maximum number of output tokens.timeout: Maximum time (in seconds) to wait for a response.max_retries: Maximum number of retry attempts for failed requests.base_url: Custom API endpoint URL.rate_limiter: ABaseRateLimiterinstance to control request rate.
A
BaseChatModel corresponding to the model_name and model_provider specified if configurability is inferred to be False. If configurable, a chat model emulator that initializes the underlying model at runtime once a config is passed in.Examples
Initialize a non-configurable model
Partially configurable model with no default
Fully configurable model with a default
Bind tools to a configurable model
You can call any chat model declarative methods on a configurable model in the same way that you would with a normal model:BaseChatModel
langchain_core for convenience.
See the LangChain Core documentation for the full API reference.