LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corelanguage_modelschat_modelsBaseChatModel
    Class●Since v0.1

    BaseChatModel

    Base class for chat models.

    Copy
    BaseChatModel(
        self,
        *args: Any = (),
        **kwargs: Any = {},
    )

    Bases

    BaseLanguageModel[AIMessage]ABC

    Key imperative methods:

    Methods that actually call the underlying model.

    This table provides a brief overview of the main imperative methods. Please see the base Runnable reference for full documentation.

    Method Input Output Description
    invoke str | `list[dict tuple BaseMessage]|PromptValue`
    ainvoke ''' BaseMessage Defaults to running invoke in an async executor.
    stream ''' Iterator[BaseMessageChunk] Defaults to yielding output of invoke.
    astream ''' AsyncIterator[BaseMessageChunk] Defaults to yielding output of ainvoke.
    astream_events ''' AsyncIterator[StreamEvent] Event types: on_chat_model_start, on_chat_model_stream, on_chat_model_end.
    batch list['''] list[BaseMessage] Defaults to running invoke in concurrent threads.
    abatch list['''] list[BaseMessage] Defaults to running ainvoke in concurrent threads.
    batch_as_completed list['''] Iterator[tuple[int, Union[BaseMessage, Exception]]] Defaults to running invoke in concurrent threads.
    abatch_as_completed list['''] AsyncIterator[tuple[int, Union[BaseMessage, Exception]]] Defaults to running ainvoke in concurrent threads.

    Key declarative methods:

    Methods for creating another Runnable using the chat model.

    This table provides a brief overview of the main declarative methods. Please see the reference for each method for full documentation.

    Method Description
    bind_tools Create chat model that can call tools.
    with_structured_output Create wrapper that structures model output using schema.
    with_retry Create wrapper that retries model calls on failure.
    with_fallbacks Create wrapper that falls back to other models on failure.
    configurable_fields Specify init args of the model that can be configured at runtime via the RunnableConfig.
    configurable_alternatives Specify alternative models which can be swapped in at runtime via the RunnableConfig.

    Creating custom chat model:

    Custom chat model implementations should inherit from this class. Please reference the table below for information about which methods and properties are required or optional for implementations.

    Method/Property Description Required
    _generate Use to generate a chat result from a prompt Required
    _llm_type (property) Used to uniquely identify the type of the model. Used for logging. Required
    _identifying_params (property) Represent model parameterization for tracing purposes. Optional
    _stream Use to implement streaming Optional
    _agenerate Use to implement a native async method Optional
    _astream Use to implement async version of _stream Optional

    Attributes

    attribute
    rate_limiter: BaseRateLimiter | None

    An optional rate limiter to use for limiting the number of requests.

    attribute
    disable_streaming: bool | Literal['tool_calling']

    Whether to disable streaming for this model.

    If streaming is bypassed, then stream/astream/astream_events will defer to invoke/ainvoke.

    • If True, will always bypass streaming case.
    • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke) only when the tools argument is provided. This offers the best of both worlds.
    • If False (Default), will always use streaming case if available.

    The main reason for this flag is that code might be written using stream and a user may want to swap out a given model for another model whose implementation does not properly support streaming.

    attribute
    output_version: str | None

    Version of AIMessage output format to store in message content.

    AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

    Supported values:

    • 'v0': provider-specific format in content (can lazily-parse with content_blocks)
    • 'v1': standardized format in content (consistent with content_blocks)

    Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

    attribute
    profile: ModelProfile | None

    Profile detailing model capabilities.

    Beta feature

    This is a beta feature. The format of model profiles is subject to change.

    If not specified, automatically loaded from the provider package on initialization if data is available.

    Example profile data includes context window sizes, supported modalities, or support for tool calling, structured output, and other features.

    attribute
    model_config
    attribute
    OutputType: Any

    Get the output type for this Runnable.

    Methods

    method
    invoke
    method
    ainvoke
    method
    stream
    method
    astream
    method
    generate

    Pass a sequence of prompts to the model and return model generations.

    This method should make use of batched calls for models that expose a batched API.

    Use this method when you want to:

    1. Take advantage of batched calls,
    2. Need more output from the model than just the top generated value,
    3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).
    method
    agenerate

    Asynchronously pass a sequence of prompts to a model and return generations.

    This method should make use of batched calls for models that expose a batched API.

    Use this method when you want to:

    1. Take advantage of batched calls,
    2. Need more output from the model than just the top generated value,
    3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).
    method
    generate_prompt
    method
    agenerate_prompt
    method
    dict

    Return a dictionary of the LLM.

    method
    bind_tools

    Bind tools to the model.

    method
    with_structured_output

    Model wrapper that returns outputs formatted to match the given schema.

    Inherited fromBaseLanguageModel

    Attributes

    Acache: BaseCache | bool | None
    —

    Whether to cache the response.

    Averbose: bool
    —

    Whether to log the tool's progress.

    Acallbacks: Callbacks
    —

    Callbacks for this call and any sub-calls (e.g. a Chain calling an LLM).

    Atags: list[str] | None
    —

    Optional list of tags associated with the retriever.

    Ametadata: dict[str, Any] | None
    —

    Optional metadata associated with the retriever.

    Acustom_get_token_ids: Callable[[str], list[int]] | None
    —

    Optional encoder to use for counting tokens.

    AInputType: Any

    Methods

    Mset_verbose
    —

    If verbose is None, set it.

    Mget_token_ids
    —

    Return the ordered IDs of the tokens in a text.

    Mget_num_tokens
    —

    Get the number of tokens present in the text.

    Mget_num_tokens_from_messages
    —

    Get the number of tokens in the messages.

    Inherited fromRunnableSerializable

    Attributes

    Aname: str
    —

    The name of the function.

    Methods

    Mto_json
    —

    Convert the graph to a JSON-serializable format.

    Mconfigurable_fieldsMconfigurable_alternatives
    —

    Configure alternatives for Runnable objects that can be set at runtime.

    Inherited fromSerializable

    Attributes

    Alc_secrets: dict[str, str]
    —

    A map of constructor argument names to secret ids.

    Alc_attributes: dict
    —

    List of attribute names that should be included in the serialized kwargs.

    Methods

    Mis_lc_serializable
    —

    Return True as this class is serializable.

    Mget_lc_namespace
    —

    Get the namespace of the LangChain object.

    Mlc_id
    —

    Return a unique identifier for this class for serialization purposes.

    Mto_json
    —

    Convert the graph to a JSON-serializable format.

    Mto_json_not_implemented
    —

    Serialize a "not implemented" object.

    Inherited fromRunnable

    Attributes

    Aname: str
    —

    The name of the function.

    AInputType: AnyAinput_schema: type[BaseModel]
    —

    The type of input this Runnable accepts specified as a Pydantic model.

    Aoutput_schema: type[BaseModel]
    —

    Output schema.

    Aconfig_specs: list[ConfigurableFieldSpec]

    Methods

    Mget_nameMget_input_schemaMget_input_jsonschema
    —

    Get a JSON schema that represents the input to the Runnable.

    Mget_output_schemaMget_output_jsonschema
    —

    Get a JSON schema that represents the output of the Runnable.

    Mconfig_schema
    —

    The type of config this Runnable accepts specified as a Pydantic model.

    Mget_config_jsonschema
    —

    Get a JSON schema that represents the config of the Runnable.

    Mget_graphMget_prompts
    —

    Return a list of prompts used by this Runnable.

    Mpipe
    —

    Pipe Runnable objects.

    Mpick
    —

    Pick keys from the output dict of this Runnable.

    Massign
    —

    Merge the Dict input with the output produced by the mapping argument.

    MbatchMbatch_as_completed
    —

    Run invoke in parallel on a list of inputs.

    MabatchMabatch_as_completed
    —

    Run ainvoke in parallel on a list of inputs.

    Mastream_log
    —

    Stream all output from a Runnable, as reported to the callback system.

    Mastream_events
    —

    Generate a stream of events.

    MtransformMatransformMbind
    —

    Bind arguments to a Runnable, returning a new Runnable.

    Mwith_configMwith_listeners
    —

    Bind lifecycle listeners to a Runnable, returning a new Runnable.

    Mwith_alisteners
    —

    Bind async lifecycle listeners to a Runnable.

    Mwith_types
    —

    Bind input and output types to a Runnable, returning a new Runnable.

    Mwith_retry
    —

    Create a new Runnable that retries the original Runnable on exceptions.

    Mmap
    —

    Map a function to multiple iterables.

    Mwith_fallbacks
    —

    Add fallbacks to a Runnable, returning a new Runnable.

    Mas_tool
    —

    Create a BaseTool from a Runnable.

    View source on GitHub