LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • MCP Adapters
    Standard Tests
    Text Splitters
    • Overview
    • Agents
    • Callbacks
    • Chains
    • Chat models
    • Embeddings
    • Evaluation
    • Globals
    • Hub
    • Memory
    • Output parsers
    • Retrievers
    • Runnables
    • LangSmith
    • Storage
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    MCP Adapters
    Standard Tests
    Text Splitters
    OverviewAgentsCallbacksChainsChat modelsEmbeddingsEvaluationGlobalsHubMemoryOutput parsersRetrieversRunnablesLangSmithStorage
    Language
    Theme
    Pythonlangchain-classicchat_modelsbaseinit_chat_model
    Function●Since v1.0Deprecated

    init_chat_model

    Copy
    init_chat_model(
      model: str | None = None,
      *,
      model_provider: str | None

    Used in Docs

    • Build a multi-source knowledge base with routing
    • Build a personal assistant with subagents
    • Build a SQL agent
    • Build customer support with handoffs
    • Evaluate a complex agent
    (37 more not shown)
    View source on GitHub
    =
    None
    ,
    configurable_fields
    :
    Literal
    [
    'any'
    ]
    |
    list
    [
    str
    ]
    |
    tuple
    [
    str
    ,
    .
    .
    .
    ]
    |
    None
    =
    None
    ,
    config_prefix
    :
    str
    |
    None
    =
    None
    ,
    **
    kwargs
    :
    Any
    =
    {
    }
    )
    ->
    BaseChatModel
    |
    _ConfigurableModel

    Parameters

    NameTypeDescription
    modelstr | None
    Default:None

    Name of the model to use, with provider prefix — e.g., 'openai:gpt-5.5'.

    A bare model name (e.g., 'claude-opus-4-7') is also accepted; we will attempt to infer the provider from the prefix using the mapping below. Inference is best-effort and not guaranteed, so prefer the prefixed form when possible.

    Prefer pinned model IDs over moving aliases (e.g., 'claude-haiku-4-5-20251001' rather than 'claude-haiku-4-5') so behavior does not drift if the alias is repointed upstream.

    Inferred providers by prefix (case-insensitive):

    • gpt-... | o1... | o3... -> openai
    • claude... -> anthropic
    • amazon.... | anthropic.... | meta.... -> bedrock
    • gemini... -> google_vertexai
    • command... -> cohere
    • accounts/fireworks... -> fireworks
    • mistral... | mixtral... -> mistralai
    • deepseek... -> deepseek
    • grok... -> xai
    • sonar... -> perplexity
    • solar... -> upstage
    • chatgpt... | text-davinci... -> openai (legacy)
    model_providerstr | None
    Default:None

    Provider of the model, passed separately instead of as a prefix on model.

    Equivalent to the prefix form — e.g., model='claude-sonnet-4-5', model_provider='anthropic' behaves the same as model='anthropic:claude-sonnet-4-5'.

    Prefer the prefix form on model for most usage. Reach for this kwarg when:

    • The provider is dynamic (read from config or an env var) and you'd otherwise concatenate strings.
    • You want model and model_provider to be independently swappable at runtime via configurable_fields (e.g., to route the same model name to a different host).

    Supported values and the integration package each requires:

    • openai -> langchain-openai
    • anthropic -> langchain-anthropic
    • azure_openai -> langchain-openai
    • azure_ai -> langchain-azure-ai
    • google_vertexai -> langchain-google-vertexai
    • google_genai -> langchain-google-genai
    • bedrock -> langchain-aws
    • bedrock_converse -> langchain-aws
    • cohere -> langchain-cohere
    • fireworks -> langchain-fireworks
    • together -> langchain-together
    • mistralai -> langchain-mistralai
    • huggingface -> langchain-huggingface
    • groq -> langchain-groq
    • ollama -> langchain-ollama
    • google_anthropic_vertex -> langchain-google-vertexai
    • deepseek -> langchain-deepseek
    • ibm -> langchain-ibm
    • nvidia -> langchain-nvidia-ai-endpoints
    • xai -> langchain-xai
    • perplexity -> langchain-perplexity
    • upstage -> langchain-upstage
    configurable_fieldsLiteral['any'] | list[str] | tuple[str, ...] | None
    Default:None
    config_prefixstr | None
    Default:None
    **kwargsAny
    Default:{}

    Initialize a chat model from any supported provider using a unified interface.

    Use langchain.chat_models.init_chat_model instead

    This function lives in langchain-classic and is no longer actively maintained. New features and fixes land in the langchain package.

    Update your imports:

    # Don't do this:
    from langchain.chat_models import init_chat_model
    
    # Do this instead:
    from langchain.chat_models import init_chat_model

    Two main use cases:

    1. Fixed model – specify the model upfront and get a ready-to-use chat model.
    2. 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
    Installation requirements

    Requires the integration package for the chosen model provider to be installed.

    See the model_provider parameter below for specific package names (e.g., pip install langchain-openai).

    Refer to the provider integration's API reference for supported model parameters to use as **kwargs.

    Initialize a non-configurable model
    # pip install langchain langchain-openai
    
    from langchain.chat_models import init_chat_model
    
    gpt_5 = init_chat_model("openai:gpt-5.5", temperature=0)
    gpt_5.invoke("what's your name")
    Partially configurable model with no default
    # pip install langchain langchain-openai
    
    from langchain.chat_models import init_chat_model
    
    # (We don't need to specify configurable=True if a model isn't specified.)
    configurable_model = init_chat_model(temperature=0)
    
    # Use GPT-5.5 to generate the response
    configurable_model.invoke(
        "what's your name",
        config={"configurable": {"model": "gpt-5.5"}},
    )
    Fully configurable model with a default
    # pip install langchain langchain-openai langchain-anthropic
    
    from langchain.chat_models import init_chat_model
    
    configurable_model_with_default = init_chat_model(
        "openai:gpt-5.5",
        configurable_fields="any",  # This allows us to configure other params like temperature, max_tokens, etc at runtime.
        config_prefix="foo",
        temperature=0,
    )
    
    configurable_model_with_default.invoke("what's your name")
    # GPT-5.5 response with temperature 0 (as set in default)
    
    # Invoke overriding model and temperature at runtime via config.
    # Note the use of the "foo_" prefix on the config keys, which matches
    # the config_prefix we set when initializing the model.
    configurable_model_with_default.invoke(
        "what's your name",
        config={
            "configurable": {
                "foo_model": "anthropic:claude-opus-4-7",
                "foo_temperature": 0.6,
            }
        },
    )
    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:

    # pip install langchain langchain-openai langchain-anthropic
    
    from langchain.chat_models import init_chat_model
    from pydantic import BaseModel, Field
    
    class GetWeather(BaseModel):
        '''Get the current weather in a given location'''
    
        location: str = Field(
            ..., description="The city and state, e.g. San Francisco, CA"
        )
    
    class GetPopulation(BaseModel):
        '''Get the current population in a given location'''
    
        location: str = Field(
            ..., description="The city and state, e.g. San Francisco, CA"
        )
    
    configurable_model = init_chat_model(
        "gpt-5.5", configurable_fields=("model", "model_provider"), temperature=0
    )
    
    configurable_model_with_tools = configurable_model.bind_tools(
        [
            GetWeather,
            GetPopulation,
        ]
    )
    configurable_model_with_tools.invoke(
        "Which city is hotter today and which is bigger: LA or NY?"
    )
    # Use GPT-5.5
    
    configurable_model_with_tools.invoke(
        "Which city is hotter today and which is bigger: LA or NY?",
        config={"configurable": {"model": "claude-opus-4-7"}},
    )
    # Use Opus 4.7

    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.

    Fields are assumed to have 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").

    Security note

    Setting configurable_fields="any" means fields like api_key, base_url, etc., can be altered at runtime, potentially redirecting model requests to a different service/user.

    Make sure that if you're accepting untrusted configurations that you enumerate the configurable_fields=(...) explicitly.

    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: A BaseRateLimiter instance to control request rate.

    Refer to the specific model provider's integration reference for all available parameters.