From dc66737f03912a75ffb8190930b0ee9caeaeb805 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Thu, 7 Aug 2025 15:17:25 -0400 Subject: [PATCH] fix: docs and formatting (#32448) --- docs/api_reference/create_api_rst.py | 4 +- docs/docs/how_to/graph_constructing.ipynb | 4 +- docs/docs/how_to/semantic-chunker.ipynb | 4 +- docs/docs/how_to/sql_csv.ipynb | 53 +- docs/docs/integrations/graphs/kuzu_db.ipynb | 5 +- docs/docs/integrations/graphs/memgraph.ipynb | 4 +- libs/core/langchain_core/runnables/base.py | 932 ++++++++++--------- libs/core/langchain_core/v1/__init__.py | 2 +- libs/core/langchain_core/v1/chat_models.py | 175 ++-- 9 files changed, 602 insertions(+), 581 deletions(-) diff --git a/docs/api_reference/create_api_rst.py b/docs/api_reference/create_api_rst.py index de5480713dd..2edf6dfe030 100644 --- a/docs/api_reference/create_api_rst.py +++ b/docs/api_reference/create_api_rst.py @@ -217,8 +217,8 @@ def _load_package_modules( # Get the full namespace of the module namespace = str(relative_module_name).replace(".py", "").replace("/", ".") # Keep only the top level namespace - # (but make special exception for content_blocks and messages.v1) - if namespace == "messages.content_blocks" or namespace == "messages.v1": + # (but make special exception for content_blocks and v1.messages) + if namespace == "messages.content_blocks" or namespace == "v1.messages": top_namespace = namespace # Keep full namespace for content_blocks else: top_namespace = namespace.split(".")[0] diff --git a/docs/docs/how_to/graph_constructing.ipynb b/docs/docs/how_to/graph_constructing.ipynb index 5d33db58c42..1e64d15fc57 100644 --- a/docs/docs/how_to/graph_constructing.ipynb +++ b/docs/docs/how_to/graph_constructing.ipynb @@ -122,13 +122,13 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", - "from langchain_experimental.graph_transformers import LLMGraphTransformer\n", + "# from langchain_experimental.graph_transformers import LLMGraphTransformer\n", "from langchain_openai import ChatOpenAI\n", "\n", "llm = ChatOpenAI(temperature=0, model_name=\"gpt-4-turbo\")\n", diff --git a/docs/docs/how_to/semantic-chunker.ipynb b/docs/docs/how_to/semantic-chunker.ipynb index aa71aeafcc1..4316dd10872 100644 --- a/docs/docs/how_to/semantic-chunker.ipynb +++ b/docs/docs/how_to/semantic-chunker.ipynb @@ -74,12 +74,12 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "a88ff70c", "metadata": {}, "outputs": [], "source": [ - "from langchain_experimental.text_splitter import SemanticChunker\n", + "# from langchain_experimental.text_splitter import SemanticChunker\n", "from langchain_openai.embeddings import OpenAIEmbeddings\n", "\n", "text_splitter = SemanticChunker(OpenAIEmbeddings())" diff --git a/docs/docs/how_to/sql_csv.ipynb b/docs/docs/how_to/sql_csv.ipynb index 9f9cf6a990e..2b60fe430a5 100644 --- a/docs/docs/how_to/sql_csv.ipynb +++ b/docs/docs/how_to/sql_csv.ipynb @@ -612,56 +612,11 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "35ea904e-795f-411b-bef8-6484dbb6e35c", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m\n", - "Invoking: `python_repl_ast` with `{'query': \"df[['Age', 'Fare']].corr().iloc[0,1]\"}`\n", - "\n", - "\n", - "\u001b[0m\u001b[36;1m\u001b[1;3m0.11232863699941621\u001b[0m\u001b[32;1m\u001b[1;3m\n", - "Invoking: `python_repl_ast` with `{'query': \"df[['Fare', 'Survived']].corr().iloc[0,1]\"}`\n", - "\n", - "\n", - "\u001b[0m\u001b[36;1m\u001b[1;3m0.2561785496289603\u001b[0m\u001b[32;1m\u001b[1;3mThe correlation between Age and Fare is approximately 0.112, and the correlation between Fare and Survival is approximately 0.256.\n", - "\n", - "Therefore, the correlation between Fare and Survival (0.256) is greater than the correlation between Age and Fare (0.112).\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': \"What's the correlation between age and fare? is that greater than the correlation between fare and survival?\",\n", - " 'output': 'The correlation between Age and Fare is approximately 0.112, and the correlation between Fare and Survival is approximately 0.256.\\n\\nTherefore, the correlation between Fare and Survival (0.256) is greater than the correlation between Age and Fare (0.112).'}" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from langchain_experimental.agents import create_pandas_dataframe_agent\n", - "\n", - "agent = create_pandas_dataframe_agent(\n", - " llm, df, agent_type=\"openai-tools\", verbose=True, allow_dangerous_code=True\n", - ")\n", - "agent.invoke(\n", - " {\n", - " \"input\": \"What's the correlation between age and fare? is that greater than the correlation between fare and survival?\"\n", - " }\n", - ")" - ] + "outputs": [], + "source": "from langchain_experimental.agents import create_pandas_dataframe_agent\n\nagent = create_pandas_dataframe_agent(\n llm, df, agent_type=\"openai-tools\", verbose=True, allow_dangerous_code=True\n)\nagent.invoke(\n {\n \"input\": \"What's the correlation between age and fare? is that greater than the correlation between fare and survival?\"\n }\n)" }, { "cell_type": "markdown", @@ -786,4 +741,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file diff --git a/docs/docs/integrations/graphs/kuzu_db.ipynb b/docs/docs/integrations/graphs/kuzu_db.ipynb index a030a205380..e5c34711f79 100644 --- a/docs/docs/integrations/graphs/kuzu_db.ipynb +++ b/docs/docs/integrations/graphs/kuzu_db.ipynb @@ -132,12 +132,13 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain_core.documents import Document\n", - "from langchain_experimental.graph_transformers import LLMGraphTransformer\n", + "\n", + "# from langchain_experimental.graph_transformers import LLMGraphTransformer\n", "from langchain_openai import ChatOpenAI\n", "\n", "# Define the LLMGraphTransformer\n", diff --git a/docs/docs/integrations/graphs/memgraph.ipynb b/docs/docs/integrations/graphs/memgraph.ipynb index fd7f4336e77..1b9d7c88762 100644 --- a/docs/docs/integrations/graphs/memgraph.ipynb +++ b/docs/docs/integrations/graphs/memgraph.ipynb @@ -548,12 +548,12 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain_core.documents import Document\n", - "from langchain_experimental.graph_transformers import LLMGraphTransformer" + "# from langchain_experimental.graph_transformers import LLMGraphTransformer" ] }, { diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py index d456b7e12a2..3448835e032 100644 --- a/libs/core/langchain_core/runnables/base.py +++ b/libs/core/langchain_core/runnables/base.py @@ -1,4 +1,4 @@ -"""Base classes and utilities for Runnables.""" +"""Base classes and utilities for ``Runnable``s.""" from __future__ import annotations @@ -111,17 +111,17 @@ class Runnable(ABC, Generic[Input, Output]): Key Methods =========== - - **invoke/ainvoke**: Transforms a single input into an output. - - **batch/abatch**: Efficiently transforms multiple inputs into outputs. - - **stream/astream**: Streams output from a single input as it's produced. - - **astream_log**: Streams output and selected intermediate results from an input. + - **``invoke``/``ainvoke``**: Transforms a single input into an output. + - **``batch``/``abatch``**: Efficiently transforms multiple inputs into outputs. + - **``stream``/``astream``**: Streams output from a single input as it's produced. + - **``astream_log``**: Streams output and selected intermediate results from an input. Built-in optimizations: - **Batch**: By default, batch runs invoke() in parallel using a thread pool executor. Override to optimize batching. - - **Async**: Methods with "a" suffix are asynchronous. By default, they execute + - **Async**: Methods with ``'a'`` suffix are asynchronous. By default, they execute the sync counterpart using asyncio's thread pool. Override for native async. @@ -129,24 +129,24 @@ class Runnable(ABC, Generic[Input, Output]): execution, add tags and metadata for tracing and debugging etc. Runnables expose schematic information about their input, output and config via - the input_schema property, the output_schema property and config_schema method. + the ``input_schema`` property, the ``output_schema`` property and ``config_schema`` method. LCEL and Composition ==================== - The LangChain Expression Language (LCEL) is a declarative way to compose Runnables + The LangChain Expression Language (LCEL) is a declarative way to compose ``Runnables`` into chains. Any chain constructed this way will automatically have sync, async, batch, and streaming support. - The main composition primitives are RunnableSequence and RunnableParallel. + The main composition primitives are ``RunnableSequence`` and ``RunnableParallel``. - **RunnableSequence** invokes a series of runnables sequentially, with + **``RunnableSequence``** invokes a series of runnables sequentially, with one Runnable's output serving as the next's input. Construct using - the `|` operator or by passing a list of runnables to RunnableSequence. + the ``|`` operator or by passing a list of runnables to ``RunnableSequence``. - **RunnableParallel** invokes runnables concurrently, providing the same input + **``RunnableParallel``** invokes runnables concurrently, providing the same input to each. Construct it using a dict literal within a sequence or by passing a - dict to RunnableParallel. + dict to ``RunnableParallel``. For example, @@ -171,11 +171,11 @@ class Runnable(ABC, Generic[Input, Output]): Standard Methods ================ - All Runnables expose additional methods that can be used to modify their behavior + All ``Runnable``s expose additional methods that can be used to modify their behavior (e.g., add a retry policy, add lifecycle listeners, make them configurable, etc.). - These methods will work on any Runnable, including Runnable chains constructed - by composing other Runnables. See the individual methods for details. + These methods will work on any ``Runnable``, including ``Runnable`` chains constructed + by composing other ``Runnable``s. See the individual methods for details. For example, @@ -232,17 +232,17 @@ class Runnable(ABC, Generic[Input, Output]): config={'callbacks': [ConsoleCallbackHandler()]} ) - For a UI (and much more) checkout LangSmith: https://docs.smith.langchain.com/ + For a UI (and much more) checkout `LangSmith `__. """ # noqa: E501 name: Optional[str] - """The name of the Runnable. Used for debugging and tracing.""" + """The name of the ``Runnable``. Used for debugging and tracing.""" def get_name( self, suffix: Optional[str] = None, *, name: Optional[str] = None ) -> str: - """Get the name of the Runnable.""" + """Get the name of the ``Runnable``.""" if name: name_ = name elif hasattr(self, "name") and self.name: @@ -273,7 +273,7 @@ class Runnable(ABC, Generic[Input, Output]): @property def InputType(self) -> type[Input]: # noqa: N802 - """The type of input this Runnable accepts specified as a type annotation.""" + """The type of input this ``Runnable`` accepts specified as a type annotation.""" # noqa: E501 # First loop through all parent classes and if any of them is # a pydantic model, we will pick up the generic parameterization # from that model via the __pydantic_generic_metadata__ attribute. @@ -299,7 +299,7 @@ class Runnable(ABC, Generic[Input, Output]): @property def OutputType(self) -> type[Output]: # noqa: N802 - """The type of output this Runnable produces specified as a type annotation.""" + """The type of output this ``Runnable`` produces specified as a type annotation.""" # noqa: E501 # First loop through bases -- this will help generic # any pydantic models. for base in self.__class__.mro(): @@ -321,7 +321,7 @@ class Runnable(ABC, Generic[Input, Output]): @property def input_schema(self) -> type[BaseModel]: - """The type of input this Runnable accepts specified as a pydantic model.""" + """The type of input this ``Runnable`` accepts specified as a pydantic model.""" return self.get_input_schema() def get_input_schema( @@ -330,9 +330,9 @@ class Runnable(ABC, Generic[Input, Output]): ) -> type[BaseModel]: """Get a pydantic model that can be used to validate input to the Runnable. - Runnables that leverage the configurable_fields and configurable_alternatives - methods will have a dynamic input schema that depends on which - configuration the Runnable is invoked with. + ``Runnable``s that leverage the ``configurable_fields`` and + ``configurable_alternatives`` methods will have a dynamic input schema that + depends on which configuration the ``Runnable`` is invoked with. This method allows to get an input schema for a specific configuration. @@ -367,13 +367,13 @@ class Runnable(ABC, Generic[Input, Output]): def get_input_jsonschema( self, config: Optional[RunnableConfig] = None ) -> dict[str, Any]: - """Get a JSON schema that represents the input to the Runnable. + """Get a JSON schema that represents the input to the ``Runnable``. Args: config: A config to use when generating the schema. Returns: - A JSON schema that represents the input to the Runnable. + A JSON schema that represents the input to the ``Runnable``. Example: @@ -395,18 +395,18 @@ class Runnable(ABC, Generic[Input, Output]): @property def output_schema(self) -> type[BaseModel]: - """The type of output this Runnable produces specified as a pydantic model.""" + """The type of output this ``Runnable`` produces specified as a pydantic model.""" # noqa: E501 return self.get_output_schema() def get_output_schema( self, config: Optional[RunnableConfig] = None, # noqa: ARG002 ) -> type[BaseModel]: - """Get a pydantic model that can be used to validate output to the Runnable. + """Get a pydantic model that can be used to validate output to the ``Runnable``. - Runnables that leverage the configurable_fields and configurable_alternatives - methods will have a dynamic output schema that depends on which - configuration the Runnable is invoked with. + ``Runnable``s that leverage the ``configurable_fields`` and + ``configurable_alternatives`` methods will have a dynamic output schema that + depends on which configuration the ``Runnable`` is invoked with. This method allows to get an output schema for a specific configuration. @@ -441,13 +441,13 @@ class Runnable(ABC, Generic[Input, Output]): def get_output_jsonschema( self, config: Optional[RunnableConfig] = None ) -> dict[str, Any]: - """Get a JSON schema that represents the output of the Runnable. + """Get a JSON schema that represents the output of the ``Runnable``. Args: config: A config to use when generating the schema. Returns: - A JSON schema that represents the output of the Runnable. + A JSON schema that represents the output of the ``Runnable``. Example: @@ -469,22 +469,23 @@ class Runnable(ABC, Generic[Input, Output]): @property def config_specs(self) -> list[ConfigurableFieldSpec]: - """List configurable fields for this Runnable.""" + """List configurable fields for this ``Runnable``.""" return [] def config_schema( self, *, include: Optional[Sequence[str]] = None ) -> type[BaseModel]: - """The type of config this Runnable accepts specified as a pydantic model. + """The type of config this ``Runnable`` accepts specified as a pydantic model. - To mark a field as configurable, see the `configurable_fields` - and `configurable_alternatives` methods. + To mark a field as configurable, see the ``configurable_fields`` + and ``configurable_alternatives`` methods. Args: include: A list of fields to include in the config schema. Returns: A pydantic model that can be used to validate config. + """ include = include or [] config_specs = self.config_specs @@ -519,20 +520,21 @@ class Runnable(ABC, Generic[Input, Output]): def get_config_jsonschema( self, *, include: Optional[Sequence[str]] = None ) -> dict[str, Any]: - """Get a JSON schema that represents the config of the Runnable. + """Get a JSON schema that represents the config of the ``Runnable``. Args: include: A list of fields to include in the config schema. Returns: - A JSON schema that represents the config of the Runnable. + A JSON schema that represents the config of the ``Runnable``. .. versionadded:: 0.3.0 + """ return self.config_schema(include=include).model_json_schema() def get_graph(self, config: Optional[RunnableConfig] = None) -> Graph: - """Return a graph representation of this Runnable.""" + """Return a graph representation of this ``Runnable``.""" graph = Graph() try: input_node = graph.add_node(self.get_input_schema(config)) @@ -552,7 +554,7 @@ class Runnable(ABC, Generic[Input, Output]): def get_prompts( self, config: Optional[RunnableConfig] = None ) -> list[BasePromptTemplate]: - """Return a list of prompts used by this Runnable.""" + """Return a list of prompts used by this ``Runnable``.""" from langchain_core.prompts.base import BasePromptTemplate return [ @@ -571,7 +573,7 @@ class Runnable(ABC, Generic[Input, Output]): Mapping[str, Union[Runnable[Any, Other], Callable[[Any], Other], Any]], ], ) -> RunnableSerializable[Input, Other]: - """Compose this Runnable with another object to create a RunnableSequence.""" + """Compose this ``Runnable`` with another object to create a ``RunnableSequence``.""" # noqa: E501 return RunnableSequence(self, coerce_to_runnable(other)) def __ror__( @@ -584,7 +586,7 @@ class Runnable(ABC, Generic[Input, Output]): Mapping[str, Union[Runnable[Other, Any], Callable[[Other], Any], Any]], ], ) -> RunnableSerializable[Other, Output]: - """Compose this Runnable with another object to create a RunnableSequence.""" + """Compose this ``Runnable`` with another object to create a ``RunnableSequence``.""" # noqa: E501 return RunnableSequence(coerce_to_runnable(other), self) def pipe( @@ -592,9 +594,9 @@ class Runnable(ABC, Generic[Input, Output]): *others: Union[Runnable[Any, Other], Callable[[Any], Other]], name: Optional[str] = None, ) -> RunnableSerializable[Input, Other]: - """Compose this Runnable with Runnable-like objects to make a RunnableSequence. + """Compose this ``Runnable`` with ``Runnable``-like objects to make a ``RunnableSequence``. - Equivalent to `RunnableSequence(self, *others)` or `self | others[0] | ...` + Equivalent to ``RunnableSequence(self, *others)`` or ``self | others[0] | ...`` Example: .. code-block:: python @@ -621,11 +623,11 @@ class Runnable(ABC, Generic[Input, Output]): await sequence.abatch([1, 2, 3]) # -> [4, 6, 8] - """ + """ # noqa: E501 return RunnableSequence(self, *others, name=name) def pick(self, keys: Union[str, list[str]]) -> RunnableSerializable[Any, Any]: - """Pick keys from the output dict of this Runnable. + """Pick keys from the output dict of this ``Runnable``. Pick single key: .. code-block:: python @@ -688,9 +690,9 @@ class Runnable(ABC, Generic[Input, Output]): ], ], ) -> RunnableSerializable[Any, Any]: - """Assigns new fields to the dict output of this Runnable. + """Assigns new fields to the dict output of this ``Runnable``. - Returns a new Runnable. + Returns a new ``Runnable``. .. code-block:: python @@ -735,15 +737,16 @@ class Runnable(ABC, Generic[Input, Output]): """Transform a single input into an output. Args: - input: The input to the Runnable. - config: A config to use when invoking the Runnable. + input: The input to the ``Runnable``. + config: A config to use when invoking the ``Runnable``. The config supports standard keys like ``'tags'``, ``'metadata'`` for tracing purposes, ``'max_concurrency'`` for controlling how much work to - do in parallel, and other keys. Please refer to the RunnableConfig + do in parallel, and other keys. Please refer to the ``RunnableConfig`` for more details. Defaults to None. Returns: - The output of the Runnable. + The output of the ``Runnable``. + """ async def ainvoke( @@ -752,12 +755,13 @@ class Runnable(ABC, Generic[Input, Output]): config: Optional[RunnableConfig] = None, **kwargs: Any, ) -> Output: - """Default implementation of ainvoke, calls invoke from a thread. + """Default implementation of ``ainvoke``, calls ``invoke`` from a thread. The default implementation allows usage of async code even if - the Runnable did not implement a native async version of invoke. + the ``Runnable`` did not implement a native async version of ``invoke``. Subclasses should override this method if they can run asynchronously. + """ return await run_in_executor(config, self.invoke, input, config, **kwargs) @@ -774,7 +778,8 @@ class Runnable(ABC, Generic[Input, Output]): The default implementation of batch works well for IO bound runnables. Subclasses should override this method if they can batch more efficiently; - e.g., if the underlying Runnable uses an API which supports a batch mode. + e.g., if the underlying ``Runnable`` uses an API which supports a batch mode. + """ if not inputs: return [] @@ -825,9 +830,10 @@ class Runnable(ABC, Generic[Input, Output]): return_exceptions: bool = False, **kwargs: Optional[Any], ) -> Iterator[tuple[int, Union[Output, Exception]]]: - """Run invoke in parallel on a list of inputs. + """Run ``invoke`` in parallel on a list of inputs. Yields results as they complete. + """ if not inputs: return @@ -876,26 +882,27 @@ class Runnable(ABC, Generic[Input, Output]): return_exceptions: bool = False, **kwargs: Optional[Any], ) -> list[Output]: - """Default implementation runs ainvoke in parallel using asyncio.gather. + """Default implementation runs ``ainvoke`` in parallel using ``asyncio.gather``. - The default implementation of batch works well for IO bound runnables. + The default implementation of ``batch`` works well for IO bound runnables. Subclasses should override this method if they can batch more efficiently; - e.g., if the underlying Runnable uses an API which supports a batch mode. + e.g., if the underlying ``Runnable`` uses an API which supports a batch mode. Args: - inputs: A list of inputs to the Runnable. - config: A config to use when invoking the Runnable. + inputs: A list of inputs to the ``Runnable``. + config: A config to use when invoking the ``Runnable``. The config supports standard keys like ``'tags'``, ``'metadata'`` for tracing purposes, ``'max_concurrency'`` for controlling how much work to - do in parallel, and other keys. Please refer to the RunnableConfig + do in parallel, and other keys. Please refer to the ``RunnableConfig`` for more details. Defaults to None. return_exceptions: Whether to return exceptions instead of raising them. Defaults to False. - kwargs: Additional keyword arguments to pass to the Runnable. + kwargs: Additional keyword arguments to pass to the ``Runnable``. Returns: - A list of outputs from the Runnable. + A list of outputs from the ``Runnable``. + """ if not inputs: return [] @@ -944,23 +951,24 @@ class Runnable(ABC, Generic[Input, Output]): return_exceptions: bool = False, **kwargs: Optional[Any], ) -> AsyncIterator[tuple[int, Union[Output, Exception]]]: - """Run ainvoke in parallel on a list of inputs. + """Run ``ainvoke`` in parallel on a list of inputs. Yields results as they complete. Args: - inputs: A list of inputs to the Runnable. - config: A config to use when invoking the Runnable. + inputs: A list of inputs to the ``Runnable``. + config: A config to use when invoking the ``Runnable``. The config supports standard keys like ``'tags'``, ``'metadata'`` for tracing purposes, ``'max_concurrency'`` for controlling how much work to - do in parallel, and other keys. Please refer to the RunnableConfig + do in parallel, and other keys. Please refer to the ``RunnableConfig`` for more details. Defaults to None. return_exceptions: Whether to return exceptions instead of raising them. Defaults to False. - kwargs: Additional keyword arguments to pass to the Runnable. + kwargs: Additional keyword arguments to pass to the ``Runnable``. Yields: - A tuple of the index of the input and the output from the Runnable. + A tuple of the index of the input and the output from the ``Runnable``. + """ if not inputs: return @@ -1000,17 +1008,18 @@ class Runnable(ABC, Generic[Input, Output]): config: Optional[RunnableConfig] = None, **kwargs: Optional[Any], ) -> Iterator[Output]: - """Default implementation of stream, which calls invoke. + """Default implementation of ``stream``, which calls ``invoke``. Subclasses should override this method if they support streaming output. Args: - input: The input to the Runnable. - config: The config to use for the Runnable. Defaults to None. - kwargs: Additional keyword arguments to pass to the Runnable. + input: The input to the ``Runnable``. + config: The config to use for the ``Runnable``. Defaults to None. + kwargs: Additional keyword arguments to pass to the ``Runnable``. Yields: - The output of the Runnable. + The output of the ``Runnable``. + """ yield self.invoke(input, config, **kwargs) @@ -1020,17 +1029,18 @@ class Runnable(ABC, Generic[Input, Output]): config: Optional[RunnableConfig] = None, **kwargs: Optional[Any], ) -> AsyncIterator[Output]: - """Default implementation of astream, which calls ainvoke. + """Default implementation of ``astream``, which calls ``ainvoke``. Subclasses should override this method if they support streaming output. Args: - input: The input to the Runnable. - config: The config to use for the Runnable. Defaults to None. - kwargs: Additional keyword arguments to pass to the Runnable. + input: The input to the ``Runnable``. + config: The config to use for the ``Runnable``. Defaults to None. + kwargs: Additional keyword arguments to pass to the ``Runnable``. Yields: - The output of the Runnable. + The output of the ``Runnable``. + """ yield await self.ainvoke(input, config, **kwargs) @@ -1083,7 +1093,7 @@ class Runnable(ABC, Generic[Input, Output]): exclude_tags: Optional[Sequence[str]] = None, **kwargs: Any, ) -> Union[AsyncIterator[RunLogPatch], AsyncIterator[RunLog]]: - """Stream all output from a Runnable, as reported to the callback system. + """Stream all output from a ``Runnable``, as reported to the callback system. This includes all inner runs of LLMs, Retrievers, Tools, etc. @@ -1094,20 +1104,21 @@ class Runnable(ABC, Generic[Input, Output]): The Jsonpatch ops can be applied in order to construct state. Args: - input: The input to the Runnable. - config: The config to use for the Runnable. + input: The input to the ``Runnable``. + config: The config to use for the ``Runnable``. diff: Whether to yield diffs between each step or the current state. - with_streamed_output_list: Whether to yield the streamed_output list. + with_streamed_output_list: Whether to yield the ``streamed_output`` list. include_names: Only include logs with these names. include_types: Only include logs with these types. include_tags: Only include logs with these tags. exclude_names: Exclude logs with these names. exclude_types: Exclude logs with these types. exclude_tags: Exclude logs with these tags. - kwargs: Additional keyword arguments to pass to the Runnable. + kwargs: Additional keyword arguments to pass to the ``Runnable``. Yields: - A RunLogPatch or RunLog object. + A ``RunLogPatch`` or ``RunLog`` object. + """ from langchain_core.tracers.log_stream import ( LogStreamCallbackHandler, @@ -1155,73 +1166,73 @@ class Runnable(ABC, Generic[Input, Output]): ) -> AsyncIterator[StreamEvent]: """Generate a stream of events. - Use to create an iterator over StreamEvents that provide real-time information - about the progress of the Runnable, including StreamEvents from intermediate + Use to create an iterator over ``StreamEvents`` that provide real-time information + about the progress of the ``Runnable``, including ``StreamEvents`` from intermediate results. - A StreamEvent is a dictionary with the following schema: + A ``StreamEvent`` is a dictionary with the following schema: - ``event``: **str** - Event names are of the format: - on_[runnable_type]_(start|stream|end). - - ``name``: **str** - The name of the Runnable that generated the event. + ``on_[runnable_type]_(start|stream|end)``. + - ``name``: **str** - The name of the ``Runnable`` that generated the event. - ``run_id``: **str** - randomly generated ID associated with the given - execution of the Runnable that emitted the event. A child Runnable that gets - invoked as part of the execution of a parent Runnable is assigned its own + execution of the ``Runnable`` that emitted the event. A child ``Runnable`` that gets + invoked as part of the execution of a parent ``Runnable`` is assigned its own unique ID. - ``parent_ids``: **list[str]** - The IDs of the parent runnables that generated - the event. The root Runnable will have an empty list. The order of the parent + the event. The root ``Runnable`` will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list. - - ``tags``: **Optional[list[str]]** - The tags of the Runnable that generated + - ``tags``: **Optional[list[str]]** - The tags of the ``Runnable`` that generated the event. - - ``metadata``: **Optional[dict[str, Any]]** - The metadata of the Runnable that + - ``metadata``: **Optional[dict[str, Any]]** - The metadata of the ``Runnable`` that generated the event. - ``data``: **dict[str, Any]** - Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table. - .. NOTE:: This reference table is for the V2 version of the schema. + .. note:: + This reference table is for the v2 version of the schema. - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | event | name | chunk | input | output | - +======================+==================+=================================+===============================================+=================================================+ - | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_llm_start | [model name] | | {'input': 'hello'} | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_llm_stream | [model name] | 'Hello' | | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_llm_end | [model name] | | 'Hello human!' | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_chain_start | format_docs | | | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_chain_stream | format_docs | "hello world!, goodbye world!" | | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_chain_end | format_docs | | [Document(...)] | "hello world!, goodbye world!" | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_retriever_start | [retriever name] | | {"query": "hello"} | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_prompt_start | [template_name] | | {"question": "hello"} | | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ - | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | - +----------------------+------------------+---------------------------------+-----------------------------------------------+-------------------------------------------------+ + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | event | name | chunk | input | output | + +==========================+==================+=====================================+===================================================+=====================================================+ + | ``on_chat_model_start`` | [model name] | | ``{"messages": [[SystemMessage, HumanMessage]]}`` | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_chat_model_stream`` | [model name] | ``AIMessageChunk(content="hello")`` | | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_chat_model_end`` | [model name] | | ``{"messages": [[SystemMessage, HumanMessage]]}`` | ``AIMessageChunk(content="hello world")`` | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_llm_start`` | [model name] | | ``{'input': 'hello'}`` | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_llm_stream`` | [model name] | ``'Hello' `` | | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_llm_end`` | [model name] | | ``'Hello human!'`` | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_chain_start`` | format_docs | | | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_chain_stream`` | format_docs | ``'hello world!, goodbye world!'`` | | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_chain_end`` | format_docs | | ``[Document(...)]`` | ``'hello world!, goodbye world!'`` | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_tool_start`` | some_tool | | ``{"x": 1, "y": "2"}`` | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_tool_end`` | some_tool | | | ``{"x": 1, "y": "2"}`` | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_retriever_start`` | [retriever name] | | ``{"query": "hello"}`` | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_retriever_end`` | [retriever name] | | ``{"query": "hello"}`` | ``[Document(...), ..]`` | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_prompt_start`` | [template_name] | | ``{"question": "hello"}`` | | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ + | ``on_prompt_end`` | [template_name] | | ``{"question": "hello"}`` | ``ChatPromptValue(messages: [SystemMessage, ...])`` | + +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ In addition to the standard events, users can also dispatch custom events (see example below). - Custom events will be only be surfaced with in the `v2` version of the API! + Custom events will be only be surfaced with in the v2 version of the API! A custom event has following format: @@ -1235,7 +1246,7 @@ class Runnable(ABC, Generic[Input, Output]): Here are declarations associated with the standard events shown above: - `format_docs`: + ``format_docs``: .. code-block:: python @@ -1245,7 +1256,7 @@ class Runnable(ABC, Generic[Input, Output]): format_docs = RunnableLambda(format_docs) - `some_tool`: + ``some_tool``: .. code-block:: python @@ -1254,7 +1265,7 @@ class Runnable(ABC, Generic[Input, Output]): '''Some_tool.''' return {"x": x, "y": y} - `prompt`: + ``prompt``: .. code-block:: python @@ -1339,29 +1350,29 @@ class Runnable(ABC, Generic[Input, Output]): print(event) Args: - input: The input to the Runnable. - config: The config to use for the Runnable. - version: The version of the schema to use either `v2` or `v1`. - Users should use `v2`. - `v1` is for backwards compatibility and will be deprecated + input: The input to the ``Runnable``. + config: The config to use for the ``Runnable``. + version: The version of the schema to use either ``'v2'`` or ``'v1'``. + Users should use ``'v2'``. + ``'v1'`` is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. - custom events will only be surfaced in `v2`. - include_names: Only include events from runnables with matching names. - include_types: Only include events from runnables with matching types. - include_tags: Only include events from runnables with matching tags. - exclude_names: Exclude events from runnables with matching names. - exclude_types: Exclude events from runnables with matching types. - exclude_tags: Exclude events from runnables with matching tags. - kwargs: Additional keyword arguments to pass to the Runnable. - These will be passed to astream_log as this implementation - of astream_events is built on top of astream_log. + custom events will only be surfaced in ``'v2'``. + include_names: Only include events from ``Runnables`` with matching names. + include_types: Only include events from ``Runnables`` with matching types. + include_tags: Only include events from ``Runnables`` with matching tags. + exclude_names: Exclude events from ``Runnables`` with matching names. + exclude_types: Exclude events from ``Runnables`` with matching types. + exclude_tags: Exclude events from ``Runnables`` with matching tags. + kwargs: Additional keyword arguments to pass to the ``Runnable``. + These will be passed to ``astream_log`` as this implementation + of ``astream_events`` is built on top of ``astream_log``. Yields: - An async stream of StreamEvents. + An async stream of ``StreamEvents``. Raises: - NotImplementedError: If the version is not `v1` or `v2`. + NotImplementedError: If the version is not ``'v1'`` or ``'v2'``. """ # noqa: E501 from langchain_core.tracers.event_stream import ( @@ -1411,19 +1422,20 @@ class Runnable(ABC, Generic[Input, Output]): config: Optional[RunnableConfig] = None, **kwargs: Optional[Any], ) -> Iterator[Output]: - """Default implementation of transform, which buffers input and calls astream. + """Default implementation of transform, which buffers input and calls ``astream``. Subclasses should override this method if they can start producing output while input is still being generated. Args: - input: An iterator of inputs to the Runnable. - config: The config to use for the Runnable. Defaults to None. - kwargs: Additional keyword arguments to pass to the Runnable. + input: An iterator of inputs to the ``Runnable``. + config: The config to use for the ``Runnable``. Defaults to None. + kwargs: Additional keyword arguments to pass to the ``Runnable``. Yields: - The output of the Runnable. - """ + The output of the ``Runnable``. + + """ # noqa: E501 final: Input got_first_val = False @@ -1453,19 +1465,20 @@ class Runnable(ABC, Generic[Input, Output]): config: Optional[RunnableConfig] = None, **kwargs: Optional[Any], ) -> AsyncIterator[Output]: - """Default implementation of atransform, which buffers input and calls astream. + """Default implementation of atransform, which buffers input and calls ``astream``. Subclasses should override this method if they can start producing output while input is still being generated. Args: - input: An async iterator of inputs to the Runnable. - config: The config to use for the Runnable. Defaults to None. - kwargs: Additional keyword arguments to pass to the Runnable. + input: An async iterator of inputs to the ``Runnable``. + config: The config to use for the ``Runnable``. Defaults to None. + kwargs: Additional keyword arguments to pass to the ``Runnable``. Yields: - The output of the Runnable. - """ + The output of the ``Runnable``. + + """ # noqa: E501 final: Input got_first_val = False @@ -1491,16 +1504,16 @@ class Runnable(ABC, Generic[Input, Output]): yield output def bind(self, **kwargs: Any) -> Runnable[Input, Output]: - """Bind arguments to a Runnable, returning a new Runnable. + """Bind arguments to a ``Runnable``, returning a new ``Runnable``. - Useful when a Runnable in a chain requires an argument that is not - in the output of the previous Runnable or included in the user input. + Useful when a ``Runnable`` in a chain requires an argument that is not + in the output of the previous ``Runnable`` or included in the user input. Args: - kwargs: The arguments to bind to the Runnable. + kwargs: The arguments to bind to the ``Runnable``. Returns: - A new Runnable with the arguments bound. + A new ``Runnable`` with the arguments bound. Example: @@ -1538,14 +1551,15 @@ class Runnable(ABC, Generic[Input, Output]): # Sadly Unpack is not well-supported by mypy so this will have to be untyped **kwargs: Any, ) -> Runnable[Input, Output]: - """Bind config to a Runnable, returning a new Runnable. + """Bind config to a ``Runnable``, returning a new ``Runnable``. Args: - config: The config to bind to the Runnable. - kwargs: Additional keyword arguments to pass to the Runnable. + config: The config to bind to the ``Runnable``. + kwargs: Additional keyword arguments to pass to the ``Runnable``. Returns: - A new Runnable with the config bound. + A new ``Runnable`` with the config bound. + """ return RunnableBinding( bound=self, @@ -1569,22 +1583,22 @@ class Runnable(ABC, Generic[Input, Output]): Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]] ] = None, ) -> Runnable[Input, Output]: - """Bind lifecycle listeners to a Runnable, returning a new Runnable. + """Bind lifecycle listeners to a ``Runnable``, returning a new ``Runnable``. - The Run object contains information about the run, including its id, - type, input, output, error, start_time, end_time, and any tags or metadata - added to the run. + The Run object contains information about the run, including its ``id``, + ``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and + any tags or metadata added to the run. Args: - on_start: Called before the Runnable starts running, with the Run object. - Defaults to None. - on_end: Called after the Runnable finishes running, with the Run object. - Defaults to None. - on_error: Called if the Runnable throws an error, with the Run object. - Defaults to None. + on_start: Called before the ``Runnable`` starts running, with the ``Run`` + object. Defaults to None. + on_end: Called after the ``Runnable`` finishes running, with the ``Run`` + object. Defaults to None. + on_error: Called if the ``Runnable`` throws an error, with the ``Run`` + object. Defaults to None. Returns: - A new Runnable with the listeners bound. + A new ``Runnable`` with the listeners bound. Example: @@ -1636,22 +1650,22 @@ class Runnable(ABC, Generic[Input, Output]): on_end: Optional[AsyncListener] = None, on_error: Optional[AsyncListener] = None, ) -> Runnable[Input, Output]: - """Bind async lifecycle listeners to a Runnable, returning a new Runnable. + """Bind async lifecycle listeners to a ``Runnable``, returning a new ``Runnable``. - The Run object contains information about the run, including its id, - type, input, output, error, start_time, end_time, and any tags or metadata - added to the run. + The Run object contains information about the run, including its ``id``, + ``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and + any tags or metadata added to the run. Args: - on_start: Called asynchronously before the Runnable starts running, - with the Run object. Defaults to None. - on_end: Called asynchronously after the Runnable finishes running, - with the Run object. Defaults to None. - on_error: Called asynchronously if the Runnable throws an error, - with the Run object. Defaults to None. + on_start: Called asynchronously before the ``Runnable`` starts running, + with the ``Run`` object. Defaults to None. + on_end: Called asynchronously after the ``Runnable`` finishes running, + with the ``Run`` object. Defaults to None. + on_error: Called asynchronously if the ``Runnable`` throws an error, + with the ``Run`` object. Defaults to None. Returns: - A new Runnable with the listeners bound. + A new ``Runnable`` with the listeners bound. Example: @@ -1702,7 +1716,7 @@ class Runnable(ABC, Generic[Input, Output]): on end callback ends at 2025-03-01T07:05:29.883893+00:00 on end callback ends at 2025-03-01T07:05:30.884831+00:00 - """ + """ # noqa: E501 from langchain_core.tracers.root_listeners import AsyncRootListenersTracer return RunnableBinding( @@ -1727,11 +1741,11 @@ class Runnable(ABC, Generic[Input, Output]): input_type: Optional[type[Input]] = None, output_type: Optional[type[Output]] = None, ) -> Runnable[Input, Output]: - """Bind input and output types to a Runnable, returning a new Runnable. + """Bind input and output types to a ``Runnable``, returning a new ``Runnable``. Args: - input_type: The input type to bind to the Runnable. Defaults to None. - output_type: The output type to bind to the Runnable. Defaults to None. + input_type: The input type to bind to the ``Runnable``. Defaults to None. + output_type: The output type to bind to the ``Runnable``. Defaults to None. Returns: A new Runnable with the types bound. @@ -1810,12 +1824,12 @@ class Runnable(ABC, Generic[Input, Output]): ) def map(self) -> Runnable[list[Input], list[Output]]: - """Return a new Runnable that maps a list of inputs to a list of outputs. + """Return a new ``Runnable`` that maps a list of inputs to a list of outputs. - Calls invoke() with each input. + Calls ``invoke`` with each input. Returns: - A new Runnable that maps a list of inputs to a list of outputs. + A new ``Runnable`` that maps a list of inputs to a list of outputs. Example: @@ -1839,22 +1853,22 @@ class Runnable(ABC, Generic[Input, Output]): exceptions_to_handle: tuple[type[BaseException], ...] = (Exception,), exception_key: Optional[str] = None, ) -> RunnableWithFallbacksT[Input, Output]: - """Add fallbacks to a Runnable, returning a new Runnable. + """Add fallbacks to a ``Runnable``, returning a new ``Runnable``. - The new Runnable will try the original Runnable, and then each fallback + The new ``Runnable`` will try the original ``Runnable``, and then each fallback in order, upon failures. Args: - fallbacks: A sequence of runnables to try if the original Runnable fails. + fallbacks: A sequence of runnables to try if the original ``Runnable`` fails. exceptions_to_handle: A tuple of exception types to handle. - Defaults to (Exception,). + Defaults to ``(Exception,)``. exception_key: If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, - exceptions will not be passed to fallbacks. If used, the base Runnable + exceptions will not be passed to fallbacks. If used, the base ``Runnable`` and its fallbacks must accept a dictionary as input. Defaults to None. Returns: - A new Runnable that will try the original Runnable, and then each + A new ``Runnable`` that will try the original ``Runnable``, and then each fallback in order, upon failures. Example: @@ -1881,18 +1895,18 @@ class Runnable(ABC, Generic[Input, Output]): print(''.join(runnable.stream({}))) #foo bar Args: - fallbacks: A sequence of runnables to try if the original Runnable fails. + fallbacks: A sequence of runnables to try if the original ``Runnable`` fails. exceptions_to_handle: A tuple of exception types to handle. exception_key: If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, - exceptions will not be passed to fallbacks. If used, the base Runnable + exceptions will not be passed to fallbacks. If used, the base ``Runnable`` and its fallbacks must accept a dictionary as input. Returns: - A new Runnable that will try the original Runnable, and then each + A new ``Runnable`` that will try the original ``Runnable``, and then each fallback in order, upon failures. - """ + """ # noqa: E501 from langchain_core.runnables.fallbacks import RunnableWithFallbacks return RunnableWithFallbacks( @@ -1917,10 +1931,11 @@ class Runnable(ABC, Generic[Input, Output]): serialized: Optional[dict[str, Any]] = None, **kwargs: Optional[Any], ) -> Output: - """Helper method to transform an Input value to an Output value, with callbacks. + """Helper method to transform an ``Input`` value to an ``Output`` value, with callbacks. - Use this method to implement invoke() in subclasses. - """ + Use this method to implement ``invoke`` in subclasses. + + """ # noqa: E501 config = ensure_config(config) callback_manager = get_callback_manager_for_config(config) run_manager = callback_manager.on_chain_start( @@ -1967,10 +1982,10 @@ class Runnable(ABC, Generic[Input, Output]): serialized: Optional[dict[str, Any]] = None, **kwargs: Optional[Any], ) -> Output: - """Helper method to transform an Input value to an Output value, with callbacks. + """Helper method to transform an ``Input`` value to an ``Output`` value, with callbacks. - Use this method to implement ainvoke() in subclasses. - """ + Use this method to implement ``ainvoke`` in subclasses. + """ # noqa: E501 config = ensure_config(config) callback_manager = get_async_callback_manager_for_config(config) run_manager = await callback_manager.on_chain_start( @@ -2016,8 +2031,9 @@ class Runnable(ABC, Generic[Input, Output]): ) -> list[Output]: """Transform a list of inputs to a list of outputs, with callbacks. - Helper method to transform an Input value to an Output value, - with callbacks. Use this method to implement invoke() in subclasses. + Helper method to transform an ``Input`` value to an ``Output`` value, + with callbacks. Use this method to implement ``invoke`` in subclasses. + """ if not inputs: return [] @@ -2089,9 +2105,11 @@ class Runnable(ABC, Generic[Input, Output]): ) -> list[Output]: """Transform a list of inputs to a list of outputs, with callbacks. - Helper method to transform an Input value to an Output value, + Helper method to transform an ``Input`` value to an ``Output`` value, with callbacks. - Use this method to implement invoke() in subclasses. + + Use this method to implement ``invoke`` in subclasses. + """ if not inputs: return [] @@ -2163,9 +2181,11 @@ class Runnable(ABC, Generic[Input, Output]): ) -> Iterator[Output]: """Transform a stream with config. - Helper method to transform an Iterator of Input values into an Iterator of - Output values, with callbacks. - Use this to implement `stream()` or `transform()` in Runnable subclasses. + Helper method to transform an ``Iterator`` of ``Input`` values into an + ``Iterator`` of ``Output`` values, with callbacks. + + Use this to implement ``stream`` or ``transform`` in ``Runnable`` subclasses. + """ # Mixin that is used by both astream log and astream events implementation from langchain_core.tracers._streaming import _StreamingCallbackHandler @@ -2267,9 +2287,11 @@ class Runnable(ABC, Generic[Input, Output]): ) -> AsyncIterator[Output]: """Transform a stream with config. - Helper method to transform an Async Iterator of Input values into an Async - Iterator of Output values, with callbacks. - Use this to implement `astream()` or `atransform()` in Runnable subclasses. + Helper method to transform an Async ``Iterator`` of ``Input`` values into an + Async ``Iterator`` of ``Output`` values, with callbacks. + + Use this to implement ``astream`` or ``atransform`` in ``Runnable`` subclasses. + """ # Mixin that is used by both astream log and astream events implementation from langchain_core.tracers._streaming import _StreamingCallbackHandler @@ -2363,12 +2385,12 @@ class Runnable(ABC, Generic[Input, Output]): arg_types: Optional[dict[str, type]] = None, message_version: Literal["v0", "v1"] = "v0", ) -> BaseTool: - """Create a BaseTool from a Runnable. + """Create a ``BaseTool`` from a ``Runnable``. - ``as_tool`` will instantiate a BaseTool with a name, description, and - ``args_schema`` from a Runnable. Where possible, schemas are inferred + ``as_tool`` will instantiate a ``BaseTool`` with a name, description, and + ``args_schema`` from a ``Runnable``. Where possible, schemas are inferred from ``runnable.get_input_schema``. Alternatively (e.g., if the - Runnable takes a dict as input and the specific dict keys are not typed), + ``Runnable`` takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with ``args_schema``. You can also pass ``arg_types`` to just specify the required arguments and their types. @@ -2377,14 +2399,14 @@ class Runnable(ABC, Generic[Input, Output]): name: The name of the tool. Defaults to None. description: The description of the tool. Defaults to None. arg_types: A dictionary of argument names to types. Defaults to None. - message_version: Version of ToolMessage to return given + message_version: Version of ``ToolMessage`` to return given :class:`~langchain_core.messages.content_blocks.ToolCall` input. - If ``"v0"``, output will be a v0 :class:`~langchain_core.messages.tool.ToolMessage`. - If ``"v1"``, output will be a v1 :class:`~langchain_core.v1.messages.ToolMessage`. + If ``'v0'``, output will be a v0 :class:`~langchain_core.messages.tool.ToolMessage`. + If ``'v1'``, output will be a v1 :class:`~langchain_core.v1.messages.ToolMessage`. Returns: - A BaseTool instance. + A ``BaseTool`` instance. Typed dict input: @@ -2484,10 +2506,11 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]): @override def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]: - """Serialize the Runnable to JSON. + """Serialize the ``Runnable`` to JSON. Returns: - A JSON-serializable representation of the Runnable. + A JSON-serializable representation of the ``Runnable``. + """ dumped = super().to_json() with contextlib.suppress(Exception): @@ -2497,13 +2520,13 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]): def configurable_fields( self, **kwargs: AnyConfigurableField ) -> RunnableSerializable[Input, Output]: - """Configure particular Runnable fields at runtime. + """Configure particular ``Runnable`` fields at runtime. Args: - **kwargs: A dictionary of ConfigurableField instances to configure. + **kwargs: A dictionary of ``ConfigurableField`` instances to configure. Returns: - A new Runnable with the fields configured. + A new ``Runnable`` with the fields configured. .. code-block:: python @@ -2552,20 +2575,20 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]): prefix_keys: bool = False, **kwargs: Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]], ) -> RunnableSerializable[Input, Output]: - """Configure alternatives for Runnables that can be set at runtime. + """Configure alternatives for ``Runnables`` that can be set at runtime. Args: - which: The ConfigurableField instance that will be used to select the + which: The ``ConfigurableField`` instance that will be used to select the alternative. default_key: The default key to use if no alternative is selected. Defaults to ``'default'``. - prefix_keys: Whether to prefix the keys with the ConfigurableField id. + prefix_keys: Whether to prefix the keys with the ``ConfigurableField`` id. Defaults to False. - **kwargs: A dictionary of keys to Runnable instances or callables that - return Runnable instances. + **kwargs: A dictionary of keys to ``Runnable`` instances or callables that + return ``Runnable`` instances. Returns: - A new Runnable with the alternatives configured. + A new ``Runnable`` with the alternatives configured. .. code-block:: python @@ -2679,25 +2702,26 @@ def _seq_output_schema( class RunnableSequence(RunnableSerializable[Input, Output]): - """Sequence of Runnables, where the output of each is the input of the next. + """Sequence of ``Runnables``, where the output of each is the input of the next. - **RunnableSequence** is the most important composition operator in LangChain + **``RunnableSequence``** is the most important composition operator in LangChain as it is used in virtually every chain. - A RunnableSequence can be instantiated directly or more commonly by using the `|` - operator where either the left or right operands (or both) must be a Runnable. + A ``RunnableSequence`` can be instantiated directly or more commonly by using the + ``|`` operator where either the left or right operands (or both) must be a + ``Runnable``. - Any RunnableSequence automatically supports sync, async, batch. + Any ``RunnableSequence`` automatically supports sync, async, batch. - The default implementations of `batch` and `abatch` utilize threadpools and - asyncio gather and will be faster than naive invocation of invoke or ainvoke - for IO bound Runnables. + The default implementations of ``batch`` and ``abatch`` utilize threadpools and + asyncio gather and will be faster than naive invocation of ``invoke`` or ``ainvoke`` + for IO bound ``Runnable``s. Batching is implemented by invoking the batch method on each component of the - RunnableSequence in order. + ``RunnableSequence`` in order. - A RunnableSequence preserves the streaming properties of its components, so if all - components of the sequence implement a `transform` method -- which + A ``RunnableSequence`` preserves the streaming properties of its components, so if + all components of the sequence implement a ``transform`` method -- which is the method that implements the logic to map a streaming input to a streaming output -- then the sequence will be able to stream input to output! @@ -2705,15 +2729,16 @@ class RunnableSequence(RunnableSerializable[Input, Output]): streaming will only begin after this component is run. If there are multiple blocking components, streaming begins after the last one. - Please note: RunnableLambdas do not support `transform` by default! So if - you need to use a RunnableLambdas be careful about where you place them in a - RunnableSequence (if you need to use the .stream()/.astream() methods). + Please note: ``RunnableLambdas`` do not support ``transform`` by default! So if + you need to use a ``RunnableLambdas`` be careful about where you place them in a + ``RunnableSequence`` (if you need to use the ``stream``/``astream`` + methods). If you need arbitrary logic and need streaming, you can subclass - Runnable, and implement `transform` for whatever logic you need. + Runnable, and implement ``transform`` for whatever logic you need. Here is a simple example that uses simple functions to illustrate the use of - RunnableSequence: + ``RunnableSequence``: .. code-block:: python @@ -2762,11 +2787,11 @@ class RunnableSequence(RunnableSerializable[Input, Output]): # purposes. It allows specifying the `Input` on the first type, the `Output` of # the last type. first: Runnable[Input, Any] - """The first Runnable in the sequence.""" + """The first ``Runnable`` in the sequence.""" middle: list[Runnable[Any, Any]] = Field(default_factory=list) - """The middle Runnables in the sequence.""" + """The middle ``Runnable`` in the sequence.""" last: Runnable[Any, Output] - """The last Runnable in the sequence.""" + """The last ``Runnable`` in the sequence.""" def __init__( self, @@ -2776,13 +2801,13 @@ class RunnableSequence(RunnableSerializable[Input, Output]): middle: Optional[list[Runnable[Any, Any]]] = None, last: Optional[Runnable[Any, Any]] = None, ) -> None: - """Create a new RunnableSequence. + """Create a new ``RunnableSequence``. Args: steps: The steps to include in the sequence. - name: The name of the Runnable. Defaults to None. - first: The first Runnable in the sequence. Defaults to None. - middle: The middle Runnables in the sequence. Defaults to None. + name: The name of the ``Runnable``. Defaults to None. + first: The first ``Runnable`` in the sequence. Defaults to None. + middle: The middle ``Runnables`` in the sequence. Defaults to None. last: The last Runnable in the sequence. Defaults to None. Raises: @@ -2813,10 +2838,10 @@ class RunnableSequence(RunnableSerializable[Input, Output]): @property def steps(self) -> list[Runnable[Any, Any]]: - """All the Runnables that make up the sequence in order. + """All the ``Runnable``s that make up the sequence in order. Returns: - A list of Runnables. + A list of ``Runnable``s. """ return [self.first, *self.middle, self.last] @@ -2827,7 +2852,9 @@ class RunnableSequence(RunnableSerializable[Input, Output]): Returns: True if the object is serializable, False otherwise. - Defaults to True. + + Defaults to True. + """ return True @@ -2838,26 +2865,27 @@ class RunnableSequence(RunnableSerializable[Input, Output]): @property @override def InputType(self) -> type[Input]: - """The type of the input to the Runnable.""" + """The type of the input to the ``Runnable``.""" return self.first.InputType @property @override def OutputType(self) -> type[Output]: - """The type of the output of the Runnable.""" + """The type of the output of the ``Runnable``.""" return self.last.OutputType @override def get_input_schema( self, config: Optional[RunnableConfig] = None ) -> type[BaseModel]: - """Get the input schema of the Runnable. + """Get the input schema of the ``Runnable``. Args: config: The config to use. Defaults to None. Returns: - The input schema of the Runnable. + The input schema of the ``Runnable``. + """ return _seq_input_schema(self.steps, config) @@ -2865,23 +2893,25 @@ class RunnableSequence(RunnableSerializable[Input, Output]): def get_output_schema( self, config: Optional[RunnableConfig] = None ) -> type[BaseModel]: - """Get the output schema of the Runnable. + """Get the output schema of the ``Runnable``. Args: config: The config to use. Defaults to None. Returns: - The output schema of the Runnable. + The output schema of the ``Runnable``. + """ return _seq_output_schema(self.steps, config) @property @override def config_specs(self) -> list[ConfigurableFieldSpec]: - """Get the config specs of the Runnable. + """Get the config specs of the ``Runnable``. Returns: - The config specs of the Runnable. + The config specs of the ``Runnable``. + """ from langchain_core.beta.runnables.context import ( CONTEXT_CONFIG_PREFIX, @@ -2929,16 +2959,17 @@ class RunnableSequence(RunnableSerializable[Input, Output]): @override def get_graph(self, config: Optional[RunnableConfig] = None) -> Graph: - """Get the graph representation of the Runnable. + """Get the graph representation of the ``Runnable``. Args: config: The config to use. Defaults to None. Returns: - The graph representation of the Runnable. + The graph representation of the ``Runnable``. Raises: - ValueError: If a Runnable has no first or last node. + ValueError: If a ``Runnable`` has no first or last node. + """ from langchain_core.runnables.graph import Graph @@ -3476,19 +3507,19 @@ class RunnableSequence(RunnableSerializable[Input, Output]): class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): - """Runnable that runs a mapping of Runnables in parallel. + """Runnable that runs a mapping of ``Runnable``s in parallel. Returns a mapping of their outputs. - RunnableParallel is one of the two main composition primitives for the LCEL, - alongside RunnableSequence. It invokes Runnables concurrently, providing the same - input to each. + ``RunnableParallel`` is one of the two main composition primitives for the LCEL, + alongside ``RunnableSequence``. It invokes ``Runnable``s concurrently, providing the + same input to each. - A RunnableParallel can be instantiated directly or by using a dict literal within a - sequence. + A ``RunnableParallel`` can be instantiated directly or by using a dict literal + within a sequence. Here is a simple example that uses functions to illustrate the use of - RunnableParallel: + ``RunnableParallel``: .. code-block:: python @@ -3527,8 +3558,8 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): sequence.batch([1, 2, 3]) await sequence.abatch([1, 2, 3]) - RunnableParallel makes it easy to run Runnables in parallel. In the below example, - we simultaneously stream output from two different Runnables: + ``RunnableParallel`` makes it easy to run ``Runnable``s in parallel. In the below + example, we simultaneously stream output from two different ``Runnables``: .. code-block:: python @@ -3577,11 +3608,12 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): Mapping[str, Union[Runnable[Input, Any], Callable[[Input], Any]]], ], ) -> None: - """Create a RunnableParallel. + """Create a ``RunnableParallel``. Args: steps__: The steps to include. Defaults to None. **kwargs: Additional steps to include. + """ merged = {**steps__} if steps__ is not None else {} merged.update(kwargs) @@ -3607,14 +3639,15 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): def get_name( self, suffix: Optional[str] = None, *, name: Optional[str] = None ) -> str: - """Get the name of the Runnable. + """Get the name of the ``Runnable``. Args: suffix: The suffix to use. Defaults to None. name: The name to use. Defaults to None. Returns: - The name of the Runnable. + The name of the ``Runnable``. + """ name = name or self.name or f"RunnableParallel<{','.join(self.steps__.keys())}>" return super().get_name(suffix, name=name) @@ -3622,7 +3655,7 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): @property @override def InputType(self) -> Any: - """The type of the input to the Runnable.""" + """The type of the input to the ``Runnable``.""" for step in self.steps__.values(): if step.InputType: return step.InputType @@ -3633,13 +3666,14 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): def get_input_schema( self, config: Optional[RunnableConfig] = None ) -> type[BaseModel]: - """Get the input schema of the Runnable. + """Get the input schema of the ``Runnable``. Args: config: The config to use. Defaults to None. Returns: - The input schema of the Runnable. + The input schema of the ``Runnable``. + """ if all( s.get_input_schema(config).model_json_schema().get("type", "object") @@ -3663,13 +3697,14 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): def get_output_schema( self, config: Optional[RunnableConfig] = None ) -> type[BaseModel]: - """Get the output schema of the Runnable. + """Get the output schema of the ``Runnable``. Args: config: The config to use. Defaults to None. Returns: - The output schema of the Runnable. + The output schema of the ``Runnable``. + """ fields = {k: (v.OutputType, ...) for k, v in self.steps__.items()} return create_model_v2(self.get_name("Output"), field_definitions=fields) @@ -3677,10 +3712,11 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): @property @override def config_specs(self) -> list[ConfigurableFieldSpec]: - """Get the config specs of the Runnable. + """Get the config specs of the ``Runnable``. Returns: - The config specs of the Runnable. + The config specs of the ``Runnable``. + """ return get_unique_config_specs( spec for step in self.steps__.values() for spec in step.config_specs @@ -3688,16 +3724,17 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): @override def get_graph(self, config: Optional[RunnableConfig] = None) -> Graph: - """Get the graph representation of the Runnable. + """Get the graph representation of the ``Runnable``. Args: config: The config to use. Defaults to None. Returns: - The graph representation of the Runnable. + The graph representation of the ``Runnable``. Raises: - ValueError: If a Runnable has no first or last node. + ValueError: If a ``Runnable`` has no first or last node. + """ from langchain_core.runnables.graph import Graph @@ -3994,22 +4031,24 @@ RunnableMap = RunnableParallel class RunnableGenerator(Runnable[Input, Output]): - """Runnable that runs a generator function. + """``Runnable`` that runs a generator function. - RunnableGenerators can be instantiated directly or by using a generator within + ``RunnableGenerator``s can be instantiated directly or by using a generator within a sequence. - RunnableGenerators can be used to implement custom behavior, such as custom output - parsers, while preserving streaming capabilities. Given a generator function with - a signature Iterator[A] -> Iterator[B], wrapping it in a RunnableGenerator allows - it to emit output chunks as soon as they are streamed in from the previous step. + ``RunnableGenerator``s can be used to implement custom behavior, such as custom + output parsers, while preserving streaming capabilities. Given a generator function + with a signature ``Iterator[A] -> Iterator[B]``, wrapping it in a + ``RunnableGenerator`` allows it to emit output chunks as soon as they are streamed + in from the previous step. - Note that if a generator function has a signature A -> Iterator[B], such that it - requires its input from the previous step to be completed before emitting chunks - (e.g., most LLMs need the entire prompt available to start generating), it can - instead be wrapped in a RunnableLambda. + .. note:: + If a generator function has a ``signature A -> Iterator[B]``, such that it + requires its input from the previous step to be completed before emitting chunks + (e.g., most LLMs need the entire prompt available to start generating), it can + instead be wrapped in a ``RunnableLambda``. - Here is an example to show the basic mechanics of a RunnableGenerator: + Here is an example to show the basic mechanics of a ``RunnableGenerator``: .. code-block:: python @@ -4038,7 +4077,7 @@ class RunnableGenerator(Runnable[Input, Output]): await runnable.ainvoke(None) # "Have a nice day" [p async for p in runnable.astream(None)] # ["Have", " a", " nice", " day"] - RunnableGenerator makes it easy to implement custom behavior within a streaming + ``RunnableGenerator`` makes it easy to implement custom behavior within a streaming context. Below we show an example: .. code-block:: python @@ -4092,15 +4131,16 @@ class RunnableGenerator(Runnable[Input, Output]): *, name: Optional[str] = None, ) -> None: - """Initialize a RunnableGenerator. + """Initialize a ``RunnableGenerator``. Args: transform: The transform function. atransform: The async transform function. Defaults to None. - name: The name of the Runnable. Defaults to None. + name: The name of the ``Runnable``. Defaults to None. Raises: TypeError: If the transform is not a generator function. + """ if atransform is not None: self._atransform = atransform @@ -4295,12 +4335,12 @@ class RunnableGenerator(Runnable[Input, Output]): class RunnableLambda(Runnable[Input, Output]): - """RunnableLambda converts a python callable into a Runnable. + """``RunnableLambda`` converts a python callable into a ``Runnable``. - Wrapping a callable in a RunnableLambda makes the callable usable + Wrapping a callable in a ``RunnableLambda`` makes the callable usable within either a sync or async context. - RunnableLambda can be composed as any other Runnable and provides + ``RunnableLambda`` can be composed as any other ``Runnable`` and provides seamless integration with LangChain tracing. ``RunnableLambda`` is best suited for code that does not need to support @@ -4377,7 +4417,7 @@ class RunnableLambda(Runnable[Input, Output]): ] = None, name: Optional[str] = None, ) -> None: - """Create a RunnableLambda from a callable, and async callable or both. + """Create a ``RunnableLambda`` from a callable, and async callable or both. Accepts both sync and async variants to allow providing efficient implementations for sync and async execution. @@ -4386,11 +4426,12 @@ class RunnableLambda(Runnable[Input, Output]): func: Either sync or async callable afunc: An async callable that takes an input and returns an output. Defaults to None. - name: The name of the Runnable. Defaults to None. + name: The name of the ``Runnable``. Defaults to None. Raises: - TypeError: If the func is not a callable type. - TypeError: If both func and afunc are provided. + TypeError: If the ``func`` is not a callable type. + TypeError: If both ``func`` and ``afunc`` are provided. + """ if afunc is not None: self.afunc = afunc @@ -4429,7 +4470,7 @@ class RunnableLambda(Runnable[Input, Output]): @property @override def InputType(self) -> Any: - """The type of the input to this Runnable.""" + """The type of the input to this ``Runnable``.""" func = getattr(self, "func", None) or self.afunc try: params = inspect.signature(func).parameters @@ -4444,13 +4485,14 @@ class RunnableLambda(Runnable[Input, Output]): def get_input_schema( self, config: Optional[RunnableConfig] = None ) -> type[BaseModel]: - """The pydantic schema for the input to this Runnable. + """The pydantic schema for the input to this ``Runnable``. Args: config: The config to use. Defaults to None. Returns: - The input schema for this Runnable. + The input schema for this ``Runnable``. + """ func = getattr(self, "func", None) or self.afunc @@ -4488,10 +4530,11 @@ class RunnableLambda(Runnable[Input, Output]): @property @override def OutputType(self) -> Any: - """The type of the output of this Runnable as a type annotation. + """The type of the output of this ``Runnable`` as a type annotation. Returns: - The type of the output of this Runnable. + The type of the output of this ``Runnable``. + """ func = getattr(self, "func", None) or self.afunc try: @@ -4537,11 +4580,12 @@ class RunnableLambda(Runnable[Input, Output]): @functools.cached_property def deps(self) -> list[Runnable]: - """The dependencies of this Runnable. + """The dependencies of this ``Runnable``. Returns: - The dependencies of this Runnable. If the function has nonlocal - variables that are Runnables, they are considered dependencies. + The dependencies of this ``Runnable``. If the function has nonlocal + variables that are ``Runnable``s, they are considered dependencies. + """ if hasattr(self, "func"): objects = get_function_nonlocals(self.func) @@ -4605,7 +4649,7 @@ class RunnableLambda(Runnable[Input, Output]): __hash__ = None # type: ignore[assignment] def __repr__(self) -> str: - """A string representation of this Runnable.""" + """A string representation of this ``Runnable``.""" if self._repr is None: if hasattr(self, "func") and isinstance(self.func, itemgetter): self._repr = f"RunnableLambda({str(self.func)[len('operator.') :]})" @@ -4771,18 +4815,19 @@ class RunnableLambda(Runnable[Input, Output]): config: Optional[RunnableConfig] = None, **kwargs: Optional[Any], ) -> Output: - """Invoke this Runnable synchronously. + """Invoke this ``Runnable`` synchronously. Args: - input: The input to this Runnable. + input: The input to this ``Runnable``. config: The config to use. Defaults to None. kwargs: Additional keyword arguments. Returns: - The output of this Runnable. + The output of this ``Runnable``. Raises: - TypeError: If the Runnable is a coroutine function. + TypeError: If the ``Runnable`` is a coroutine function. + """ if hasattr(self, "func"): return self._call_with_config( @@ -4801,15 +4846,16 @@ class RunnableLambda(Runnable[Input, Output]): config: Optional[RunnableConfig] = None, **kwargs: Optional[Any], ) -> Output: - """Invoke this Runnable asynchronously. + """Invoke this ``Runnable`` asynchronously. Args: - input: The input to this Runnable. + input: The input to this ``Runnable``. config: The config to use. Defaults to None. kwargs: Additional keyword arguments. Returns: - The output of this Runnable. + The output of this ``Runnable``. + """ return await self._acall_with_config( self._ainvoke, @@ -5039,12 +5085,13 @@ class RunnableLambda(Runnable[Input, Output]): class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]): - """Runnable that calls another Runnable for each element of the input sequence. + """``Runnable`` that calls another ``Runnable`` for each element of the input sequence. - Use only if creating a new RunnableEach subclass with different __init__ args. + Use only if creating a new ``RunnableEach`` subclass with different ``__init__`` args. - See documentation for RunnableEach for more details. - """ + See documentation for ``RunnableEach`` for more details. + + """ # noqa: E501 bound: Runnable[Input, Output] @@ -5169,13 +5216,13 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]): class RunnableEach(RunnableEachBase[Input, Output]): - """Runnable that calls another Runnable for each element of the input sequence. + """``Runnable`` that calls another ``Runnable`` for each element of the input sequence. - It allows you to call multiple inputs with the bounded Runnable. + It allows you to call multiple inputs with the bounded ``Runnable``. - RunnableEach makes it easy to run multiple inputs for the Runnable. + ``RunnableEach`` makes it easy to run multiple inputs for the ``Runnable``. In the below example, we associate and run three inputs - with a Runnable: + with a ``Runnable``: .. code-block:: python @@ -5194,7 +5241,7 @@ class RunnableEach(RunnableEachBase[Input, Output]): {'topic':'Biology'}]) print(output) # noqa: T201 - """ + """ # noqa: E501 @override def get_name( @@ -5227,22 +5274,23 @@ class RunnableEach(RunnableEachBase[Input, Output]): Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]] ] = None, ) -> RunnableEach[Input, Output]: - """Bind lifecycle listeners to a Runnable, returning a new Runnable. + """Bind lifecycle listeners to a ``Runnable``, returning a new ``Runnable``. - The Run object contains information about the run, including its id, - type, input, output, error, start_time, end_time, and any tags or metadata - added to the run. + The ``Run`` object contains information about the run, including its ``id``, + ``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and + any tags or metadata added to the run. Args: - on_start: Called before the Runnable starts running, with the Run object. - Defaults to None. - on_end: Called after the Runnable finishes running, with the Run object. - Defaults to None. - on_error: Called if the Runnable throws an error, with the Run object. - Defaults to None. + on_start: Called before the ``Runnable`` starts running, with the ``Run`` + object. Defaults to None. + on_end: Called after the ``Runnable`` finishes running, with the ``Run`` + object. Defaults to None. + on_error: Called if the ``Runnable`` throws an error, with the ``Run`` + object. Defaults to None. Returns: - A new Runnable with the listeners bound. + A new ``Runnable`` with the listeners bound. + """ return RunnableEach( bound=self.bound.with_listeners( @@ -5257,23 +5305,24 @@ class RunnableEach(RunnableEachBase[Input, Output]): on_end: Optional[AsyncListener] = None, on_error: Optional[AsyncListener] = None, ) -> RunnableEach[Input, Output]: - """Bind async lifecycle listeners to a Runnable, returning a new Runnable. + """Bind async lifecycle listeners to a ``Runnable``, returning a new ``Runnable``. - The Run object contains information about the run, including its id, - type, input, output, error, start_time, end_time, and any tags or metadata - added to the run. + The ``Run`` object contains information about the run, including its ``id``, + ``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and + any tags or metadata added to the run. Args: - on_start: Called asynchronously before the Runnable starts running, - with the Run object. Defaults to None. - on_end: Called asynchronously after the Runnable finishes running, - with the Run object. Defaults to None. - on_error: Called asynchronously if the Runnable throws an error, - with the Run object. Defaults to None. + on_start: Called asynchronously before the ``Runnable`` starts running, + with the ``Run`` object. Defaults to None. + on_end: Called asynchronously after the ``Runnable`` finishes running, + with the ``Run`` object. Defaults to None. + on_error: Called asynchronously if the ``Runnable`` throws an error, + with the ``Run`` object. Defaults to None. Returns: - A new Runnable with the listeners bound. - """ + A new ``Runnable`` with the listeners bound. + + """ # noqa: E501 return RunnableEach( bound=self.bound.with_alisteners( on_start=on_start, on_end=on_end, on_error=on_error @@ -5282,43 +5331,46 @@ class RunnableEach(RunnableEachBase[Input, Output]): class RunnableBindingBase(RunnableSerializable[Input, Output]): - """Runnable that delegates calls to another Runnable with a set of kwargs. + """``Runnable`` that delegates calls to another ``Runnable`` with a set of kwargs. - Use only if creating a new RunnableBinding subclass with different __init__ args. + Use only if creating a new ``RunnableBinding`` subclass with different ``__init__`` + args. + + See documentation for ``RunnableBinding`` for more details. - See documentation for RunnableBinding for more details. """ bound: Runnable[Input, Output] - """The underlying Runnable that this Runnable delegates to.""" + """The underlying ``Runnable`` that this ``Runnable`` delegates to.""" kwargs: Mapping[str, Any] = Field(default_factory=dict) - """kwargs to pass to the underlying Runnable when running. + """kwargs to pass to the underlying ``Runnable`` when running. - For example, when the Runnable binding is invoked the underlying - Runnable will be invoked with the same input but with these additional + For example, when the ``Runnable`` binding is invoked the underlying + ``Runnable`` will be invoked with the same input but with these additional kwargs. + """ config: RunnableConfig = Field(default_factory=RunnableConfig) - """The config to bind to the underlying Runnable.""" + """The config to bind to the underlying ``Runnable``.""" config_factories: list[Callable[[RunnableConfig], RunnableConfig]] = Field( default_factory=list ) - """The config factories to bind to the underlying Runnable.""" + """The config factories to bind to the underlying ``Runnable``.""" # Union[Type[Input], BaseModel] + things like list[str] custom_input_type: Optional[Any] = None - """Override the input type of the underlying Runnable with a custom type. + """Override the input type of the underlying ``Runnable`` with a custom type. - The type can be a pydantic model, or a type annotation (e.g., `list[str]`). + The type can be a pydantic model, or a type annotation (e.g., ``list[str]``). """ # Union[Type[Output], BaseModel] + things like list[str] custom_output_type: Optional[Any] = None - """Override the output type of the underlying Runnable with a custom type. + """Override the output type of the underlying ``Runnable`` with a custom type. - The type can be a pydantic model, or a type annotation (e.g., `list[str]`). + The type can be a pydantic model, or a type annotation (e.g., ``list[str]``). """ model_config = ConfigDict( @@ -5338,25 +5390,25 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): custom_output_type: Optional[Union[type[Output], BaseModel]] = None, **other_kwargs: Any, ) -> None: - """Create a RunnableBinding from a Runnable and kwargs. + """Create a ``RunnableBinding`` from a ``Runnable`` and kwargs. Args: - bound: The underlying Runnable that this Runnable delegates calls to. - kwargs: optional kwargs to pass to the underlying Runnable, when running - the underlying Runnable (e.g., via `invoke`, `batch`, - `transform`, or `stream` or async variants) + bound: The underlying ``Runnable`` that this ``Runnable`` delegates calls to. + kwargs: optional kwargs to pass to the underlying ``Runnable``, when running + the underlying ``Runnable`` (e.g., via ``invoke``, ``batch``, + ``transform``, or ``stream`` or async variants) Defaults to None. - config: optional config to bind to the underlying Runnable. + config: optional config to bind to the underlying ``Runnable``. Defaults to None. config_factories: optional list of config factories to apply to the - config before binding to the underlying Runnable. + config before binding to the underlying ``Runnable``. Defaults to None. custom_input_type: Specify to override the input type of the underlying - Runnable with a custom type. Defaults to None. + ``Runnable`` with a custom type. Defaults to None. custom_output_type: Specify to override the output type of the underlying - Runnable with a custom type. Defaults to None. + ``Runnable`` with a custom type. Defaults to None. **other_kwargs: Unpacked into the base class. - """ + """ # noqa: E501 super().__init__( # type: ignore[call-arg] bound=bound, kwargs=kwargs or {}, @@ -5430,7 +5482,7 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): def get_lc_namespace(cls) -> list[str]: """Get the namespace of the langchain object. - Defaults to ["langchain", "schema", "runnable"]. + Defaults to ``["langchain", "schema", "runnable"]``. """ return ["langchain", "schema", "runnable"] @@ -5683,28 +5735,28 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): class RunnableBinding(RunnableBindingBase[Input, Output]): - """Wrap a Runnable with additional functionality. + """Wrap a ``Runnable`` with additional functionality. - A RunnableBinding can be thought of as a "runnable decorator" that - preserves the essential features of Runnable; i.e., batching, streaming, + A ``RunnableBinding`` can be thought of as a "runnable decorator" that + preserves the essential features of ``Runnable``; i.e., batching, streaming, and async support, while adding additional functionality. - Any class that inherits from Runnable can be bound to a `RunnableBinding`. - Runnables expose a standard set of methods for creating `RunnableBindings` - or sub-classes of `RunnableBindings` (e.g., `RunnableRetry`, - `RunnableWithFallbacks`) that add additional functionality. + Any class that inherits from ``Runnable`` can be bound to a ``RunnableBinding``. + Runnables expose a standard set of methods for creating ``RunnableBindings`` + or sub-classes of ``RunnableBindings`` (e.g., ``RunnableRetry``, + ``RunnableWithFallbacks``) that add additional functionality. These methods include: - - ``bind``: Bind kwargs to pass to the underlying Runnable when running it. - - ``with_config``: Bind config to pass to the underlying Runnable when running it. - - ``with_listeners``: Bind lifecycle listeners to the underlying Runnable. - - ``with_types``: Override the input and output types of the underlying Runnable. - - ``with_retry``: Bind a retry policy to the underlying Runnable. - - ``with_fallbacks``: Bind a fallback policy to the underlying Runnable. + - ``bind``: Bind kwargs to pass to the underlying ``Runnable`` when running it. + - ``with_config``: Bind config to pass to the underlying ``Runnable`` when running it. + - ``with_listeners``: Bind lifecycle listeners to the underlying ``Runnable``. + - ``with_types``: Override the input and output types of the underlying ``Runnable``. + - ``with_retry``: Bind a retry policy to the underlying ``Runnable``. + - ``with_fallbacks``: Bind a fallback policy to the underlying ``Runnable``. Example: - `bind`: Bind kwargs to pass to the underlying Runnable when running it. + `bind`: Bind kwargs to pass to the underlying ``Runnable`` when running it. .. code-block:: python @@ -5718,7 +5770,8 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): runnable_binding = model.bind(stop=['-']) runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot` - Can also be done by instantiating a RunnableBinding directly (not recommended): + Can also be done by instantiating a ``RunnableBinding`` directly (not + recommended): .. code-block:: python @@ -5729,18 +5782,19 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): ) runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot` - """ + """ # noqa: E501 @override def bind(self, **kwargs: Any) -> Runnable[Input, Output]: - """Bind additional kwargs to a Runnable, returning a new Runnable. + """Bind additional kwargs to a ``Runnable``, returning a new ``Runnable``. Args: - **kwargs: The kwargs to bind to the Runnable. + **kwargs: The kwargs to bind to the ``Runnable``. Returns: - A new Runnable with the same type and config as the original, + A new ``Runnable`` with the same type and config as the original, but with the additional kwargs bound. + """ return self.__class__( bound=self.bound, @@ -5781,22 +5835,22 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]] ] = None, ) -> Runnable[Input, Output]: - """Bind lifecycle listeners to a Runnable, returning a new Runnable. + """Bind lifecycle listeners to a ``Runnable``, returning a new ``Runnable``. - The Run object contains information about the run, including its id, - type, input, output, error, start_time, end_time, and any tags or metadata - added to the run. + The ``Run`` object contains information about the run, including its ``id``, + ``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and + any tags or metadata added to the run. Args: - on_start: Called before the Runnable starts running, with the Run object. - Defaults to None. - on_end: Called after the Runnable finishes running, with the Run object. - Defaults to None. - on_error: Called if the Runnable throws an error, with the Run object. - Defaults to None. + on_start: Called before the ``Runnable`` starts running, with the ``Run`` + object. Defaults to None. + on_end: Called after the ``Runnable`` finishes running, with the ``Run`` + object. Defaults to None. + on_error: Called if the ``Runnable`` throws an error, with the ``Run`` + object. Defaults to None. Returns: - A new Runnable with the listeners bound. + A new ``Runnable`` with the listeners bound. """ from langchain_core.tracers.root_listeners import RootListenersTracer @@ -5924,16 +5978,16 @@ RunnableLike = Union[ def coerce_to_runnable(thing: RunnableLike) -> Runnable[Input, Output]: - """Coerce a Runnable-like object into a Runnable. + """Coerce a ``Runnable``-like object into a ``Runnable``. Args: - thing: A Runnable-like object. + thing: A ``Runnable``-like object. Returns: - A Runnable. + A ``Runnable``. Raises: - TypeError: If the object is not Runnable-like. + TypeError: If the object is not ``Runnable``-like. """ if isinstance(thing, Runnable): return thing @@ -5982,16 +6036,16 @@ def chain( Callable[[Input], AsyncIterator[Output]], ], ) -> Runnable[Input, Output]: - """Decorate a function to make it a Runnable. + """Decorate a function to make it a ``Runnable``. - Sets the name of the Runnable to the name of the function. + Sets the name of the ``Runnable`` to the name of the function. Any runnables called by the function will be traced as dependencies. Args: - func: A callable. + func: A ``Callable``. Returns: - A Runnable. + A ``Runnable``. Example: diff --git a/libs/core/langchain_core/v1/__init__.py b/libs/core/langchain_core/v1/__init__.py index ad3d9e1cc1c..c30509440e5 100644 --- a/libs/core/langchain_core/v1/__init__.py +++ b/libs/core/langchain_core/v1/__init__.py @@ -1 +1 @@ -"""LangChain v1.0 types.""" +"""LangChain v1.0.0 types.""" diff --git a/libs/core/langchain_core/v1/chat_models.py b/libs/core/langchain_core/v1/chat_models.py index 09a0ab6e1fb..d51607c8efa 100644 --- a/libs/core/langchain_core/v1/chat_models.py +++ b/libs/core/langchain_core/v1/chat_models.py @@ -105,13 +105,14 @@ def _format_for_tracing(messages: Sequence[MessageV1]) -> list[MessageV1]: - Update image content blocks to OpenAI Chat Completions format (backward compatibility). - - Add "type" key to content blocks that have a single key. + - Add ``'type'`` key to content blocks that have a single key. Args: messages: List of messages to format. Returns: List of messages formatted for tracing. + """ messages_to_trace = [] for message in messages: @@ -142,10 +143,11 @@ def generate_from_stream(stream: Iterator[AIMessageChunkV1]) -> AIMessageV1: """Generate from a stream. Args: - stream: Iterator of AIMessageChunkV1. + stream: Iterator of ``AIMessageChunkV1``. Returns: AIMessageV1: aggregated message. + """ generation = next(stream, None) if generation: @@ -162,10 +164,11 @@ async def agenerate_from_stream( """Async generate from a stream. Args: - stream: Iterator of AIMessageChunkV1. + stream: Iterator of ``AIMessageChunkV1``. Returns: AIMessageV1: aggregated message. + """ chunks = [chunk async for chunk in stream] return await run_in_executor(None, generate_from_stream, iter(chunks)) @@ -196,48 +199,48 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): Key imperative methods: Methods that actually call the underlying model. - +---------------------------+----------------------------------------------------------------+---------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ - | Method | Input | Output | Description | - +===========================+================================================================+=====================================================================+==================================================================================================+ - | `invoke` | str | list[dict | tuple | BaseMessage] | PromptValue | BaseMessage | A single chat model call. | - +---------------------------+----------------------------------------------------------------+---------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ - | `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. | - +---------------------------+----------------------------------------------------------------+---------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ + +-----------------------------+----------------------------------------------------------------+-------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ + | Method | Input | Output | Description | + +=============================+================================================================+=========================================================================+==================================================================================================+ + | ``invoke`` | str | list[dict | tuple | BaseMessage] | PromptValue | ``BaseMessage`` | A single chat model call. | + +-----------------------------+----------------------------------------------------------------+-------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ + | ``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. | + +-----------------------------+----------------------------------------------------------------+-------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ - This table provides a brief overview of the main imperative methods. Please see the base Runnable reference for full documentation. + This table provides a brief overview of the main imperative methods. Please see the base ``Runnable`` reference for full documentation. Key declarative methods: - Methods for creating another Runnable using the ChatModel. + Methods for creating another ``Runnable`` using the ``ChatModel``. - +----------------------------------+-----------------------------------------------------------------------------------------------------------+ - | Method | Description | - +==================================+===========================================================================================================+ - | `bind_tools` | Create ChatModel 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. | - +----------------------------------+-----------------------------------------------------------------------------------------------------------+ + +------------------------------------+-----------------------------------------------------------------------------------------------------------+ + | Method | Description | + +====================================+===========================================================================================================+ + | ``bind_tools`` | Create ``ChatModel`` 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``. | + +------------------------------------+-----------------------------------------------------------------------------------------------------------+ This table provides a brief overview of the main declarative methods. Please see the reference for each method for full documentation. @@ -246,24 +249,23 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): Please reference the table below for information about which methods and properties are required or optional for implementations. - +----------------------------------+--------------------------------------------------------------------+-------------------+ - | Method/Property | Description | Required/Optional | - +==================================+====================================================================+===================+ - | `_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 | - +----------------------------------+--------------------------------------------------------------------+-------------------+ + +------------------------------------+--------------------------------------------------------------------+-------------------+ + | Method/Property | Description | Required/Optional | + +====================================+====================================================================+===================+ + | ``_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 | + +------------------------------------+--------------------------------------------------------------------+-------------------+ - Follow the guide for more information on how to implement a custom Chat Model: - [Guide](https://python.langchain.com/docs/how_to/custom_chat_model/). + `Follow the guide for more information on how to implement a custom Chat Model. `__ """ # noqa: E501 @@ -283,9 +285,10 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): 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 + 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 the implementation does not properly support streaming. + """ cache: Union[BaseCache, bool, None] = Field(default=None, exclude=True) @@ -294,9 +297,10 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): * If true, will use the global cache. * If false, will not use a cache * If None, will use the global cache if it's set, otherwise no cache. - * If instance of BaseCache, will use the provided cache. + * If instance of ``BaseCache``, will use the provided cache. Caching is not currently supported for streaming methods of models. + """ verbose: bool = Field(default_factory=_get_verbosity, exclude=True, repr=False) """Whether to print out response text.""" @@ -332,6 +336,7 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): Returns: The verbosity setting to use. + """ if verbose is None: return _get_verbosity() @@ -340,7 +345,7 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): @property @override def InputType(self) -> Any: - """Get the input type for this runnable.""" + """Get the input type for this ``Runnable``.""" from langchain_core.prompt_values import ( ChatPromptValueConcrete, StringPromptValue, @@ -358,7 +363,7 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): @property @override def OutputType(self) -> Any: - """Get the output type for this runnable.""" + """Get the output type for this ``Runnable``.""" return AIMessageV1 def _convert_input(self, model_input: LanguageModelInput) -> list[MessageV1]: @@ -825,10 +830,11 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): Args: tools: Sequence of tools to bind to the model. - tool_choice: The tool to use. If "any" then any tool can be used. + tool_choice: The tool to use. If ``'any'`` then any tool can be used. Returns: - A Runnable that returns a message. + A ``Runnable`` that returns a message. + """ raise NotImplementedError @@ -853,30 +859,30 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): validated by the Pydantic class. Otherwise the model output will be a dict and will not be validated. See :meth:`langchain_core.utils.function_calling.convert_to_openai_tool` for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or TypedDict class. + schema fields when specifying a Pydantic or ``TypedDict`` class. include_raw: If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True - then both the raw model response (a BaseMessage) and the parsed model + then both the raw model response (a ``BaseMessage``) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict - with keys "raw", "parsed", and "parsing_error". + with keys ``'raw'``, ``'parsed'``, and ``'parsing_error'``. Returns: - A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`. + A ``Runnable`` that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`. - If ``include_raw`` is False and ``schema`` is a Pydantic class, Runnable outputs + If ``include_raw`` is False and ``schema`` is a Pydantic class, ``Runnable`` outputs an instance of ``schema`` (i.e., a Pydantic object). - Otherwise, if ``include_raw`` is False then Runnable outputs a dict. + Otherwise, if ``include_raw`` is False then ``Runnable`` outputs a dict. - If ``include_raw`` is True, then Runnable outputs a dict with keys: - - ``"raw"``: BaseMessage + If ``include_raw`` is True, then ``Runnable`` outputs a dict with keys: + - ``"raw"``: ``BaseMessage`` - ``"parsed"``: None if there was a parsing error, otherwise the type depends on the ``schema`` as described above. - - ``"parsing_error"``: Optional[BaseException] + - ``"parsing_error"``: ``Optional[BaseException]`` - Example: Pydantic schema (include_raw=False): + Example: Pydantic schema ``(include_raw=False)``: .. code-block:: python from pydantic import BaseModel @@ -896,7 +902,7 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' # ) - Example: Pydantic schema (include_raw=True): + Example: Pydantic schema ``(include_raw=True)``: .. code-block:: python from pydantic import BaseModel @@ -916,7 +922,7 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): # 'parsing_error': None # } - Example: Dict schema (include_raw=False): + Example: Dict schema ``(include_raw=False)``: .. code-block:: python from pydantic import BaseModel @@ -939,7 +945,8 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): .. versionchanged:: 0.2.26 - Added support for TypedDict class. + Added support for ``TypedDict`` class. + """ # noqa: E501 _ = kwargs.pop("method", None) _ = kwargs.pop("strict", None) @@ -997,7 +1004,8 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): Returns: A list of ids corresponding to the tokens in the text, in order they occur - in the text. + in the text. + """ if self.custom_get_token_ids is not None: return self.custom_get_token_ids(text) @@ -1013,6 +1021,7 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): Returns: The integer number of tokens in the text. + """ return len(self.get_token_ids(text)) @@ -1025,16 +1034,18 @@ class BaseChatModel(RunnableSerializable[LanguageModelInput, AIMessageV1], ABC): Useful for checking if an input fits in a model's context window. - **Note**: the base implementation of get_num_tokens_from_messages ignores - tool schemas. + .. note:: + The base implementation of ``get_num_tokens_from_messages`` ignores tool + schemas. Args: messages: The message inputs to tokenize. - tools: If provided, sequence of dict, BaseModel, function, or BaseTools - to be converted to tool schemas. + tools: If provided, sequence of ``dict``, ``BaseModel``, function, or + ``BaseTools`` to be converted to tool schemas. Returns: The sum of the number of tokens across the messages. + """ messages_v0 = [convert_from_v1_message(message) for message in messages] if tools is not None: