YouTip LogoYouTip

Langchain Init_Chat_Mode

The `init_chat_model()` function in LangChain is one of the most commonly used functions. It enables you to connect to over 20 model providers in a unified manner, without needing to memorize each provider’s class names or parameter differences. ### Syntax The syntax of the `init_chat_model()` function is as follows: ```python from langchain.chat_models import init_chat_model # Full syntax model = init_chat_model( model, # str | None: model name (in "provider:model" format) *, model_provider=None, # str | None: model provider specified separately configurable_fields=None, # None | "any" | list: fields configurable at runtime config_prefix=None, # str | None: prefix for configuration keys **kwargs, # model-specific arguments (e.g., temperature, max_tokens) ) **Parameter Description:** | Parameter | Type | Description | Default | | --- | --- | --- | --- | | model | str or None | Model name in "provider:model" format. Passing `None` creates a configurable model | None | | model_provider | str or None | Explicitly specify the provider. Useful when dynamically fetching or when the provider cannot be inferred from `model` | None | | configurable_fields | "any" or list or None | List of fields that can be modified at runtime. `None` means the model is fixed | None | | config_prefix | str or None | Prefix for configuration keys in multi-model scenarios to avoid conflicts | None | | **kwargs | dict | Arguments passed to the underlying model | None | ### Detailed Explanation of the `model` Parameter #### "provider:model" Format (Recommended) The format is `provider:model`, separated by a colon: ```python ## 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") #### Automatic Provider Inference If no provider prefix is specified, LangChain attempts to infer the provider from the model name: ```python ## Example from langchain.chat_models import init_chat_model # Automatic provider inference (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 | > Although automatic inference is convenient, it is not guaranteed to be 100% accurate. For example, the `gemini` prefix may refer to either `google_vertexai` or `google_genai`, and future versions may change the default inference result. In production environments, it is recommended to always use the "provider:model" format. ### The `model_provider` Parameter When `model_provider` is specified separately, its effect is equivalent to the "provider:model" format: ```python ## Example # The following two approaches 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`: * 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 string concatenation is inconvenient *** ## Fixed Model vs Configurable Model The `init_chat_model()` function supports two usage patterns: ### Pattern 1: Fixed Model Specify a concrete `model` string to return a ready-to-use `BaseChatModel` instance: ```python ## Example from langchain.chat_models import init_chat_model # Specifying model returns a fixed model model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0.7) response = model.invoke("Introduce TUTORIAL") print(response.content) > If `.env` is in the current directory, use the following code to load configuration from the current path: > > ```python > import os > from dotenv import load_dotenv # Load .env file from current directory > load_dotenv() > from langchain.chat_models import init_chat_model > # Specifying model returns a fixed model > model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0.7) > response = model.invoke("Introduce TUTORIAL") > print(response.content) > ``` ### Pattern 2: Configurable Model Do not specify `model` (or set it to `None`) to create a model that can be dynamically switched at runtime: ```python ## Example from langchain.chat_models import init_chat_model # Not specifying model returns a configurable model # Some parameters can be fixed (e.g., temperature=0.7), others specified at runtime configurable_model = init_chat_model(temperature=0.7) # Specify model at runtime via config response = configurable_model.invoke( "Introduce TUTORIAL", config={"configurable": {"model": "deepseek-v4-flash"}} ) print(response.content) # The same model instance can execute with different models response = configurable_model.invoke( "Introduce TUTORIAL", config={"configurable": {"model": "claude-sonnet-4-5"}} ) print(response.content) > Configurable models are extremely useful for A/B testing and cost optimization. You can switch models or adjust parameters without restarting the service by simply modifying the configuration. *** ## All Supported Model Providers Below are the providers natively supported by `init_chat_model()` and their corresponding installation packages: | Provider Name | Installation Package | Representative Models | | --- | --- | --- | | 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 The `kwargs` arguments are directly passed to the underlying model class. Common ones include: ```python ## Example from langchain.chat_models import init_chat_model model = init_chat_model( "deepseek:deepseek-v4-flash", # Controls randomness (0~2); smaller values yield more deterministic outputs temperature=0.3, # Limits maximum output token count (controls cost) max_tokens=200, # Request timeout in seconds timeout=30, # Number of retries on failure max_retries=2, # Custom API endpoint (for 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(" TUTORIAL What is it?") print(response.content) | Parameter | Type | Description | Applicable Providers | | --- | --- | --- | --- | | temperature | float | Controls randomness, range 0~2; default value varies by model | Most | | max_tokens | int | Limits maximum output token count | 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, range 0~1 | Most | | stop | list | Stop sequences; generation stops when these tokens appear | Most | > `temperature` and `top_p` are typically not set simultaneously. `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 dynamic specification of models and parameters at runtime: ```python ## Example from langchain.chat_models import init_chat_model # Create a configurable model with 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("Introduction") print(f"Default config: {response.content[:50]}...") # Override model and parameters at runtime (note the my_ prefix) response = model.invoke( "Introduce TUTORIAL", config={ "configurable": { "my_model": "deepseek:deepseek-v4-pro", # Switch model "my_temperature": 0.9, # Adjust temperature } } ) print(f"Overridden config: {response.content[:50]}...") Values for `configurable_fields`: | Value | Meaning | | --- | --- | | None | Not configurable; returns a regular `BaseChatModel` (fixed model mode) | | "any" | All parameters are configurable (note security: sensitive fields like `api_key` can also be modified) | | ["model", "temperature"] | Only fields listed here are configurable | > When using `configurable_fields="any"`, be cautious about security: if untrusted configuration sources are used, sensitive fields such as `api_key` and `base_url` may be tampered with. In production environments, it is recommended to explicitly list configurable fields.
← Langchain Model AdvancedLangchain Deepseek β†’