Home / Function/ assign() — langchain Function Reference

assign() — langchain Function Reference

Architecture documentation for the assign() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  d62a2628_dde6_6010_a0c7_9a14c5388292["assign()"]
  4a62481c_02cb_a5de_1833_50669d5351a6["Runnable"]
  d62a2628_dde6_6010_a0c7_9a14c5388292 -->|defined in| 4a62481c_02cb_a5de_1833_50669d5351a6
  style d62a2628_dde6_6010_a0c7_9a14c5388292 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/runnables/base.py lines 772–817

    def assign(
        self,
        **kwargs: Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]],
    ) -> RunnableSerializable[Any, Any]:
        """Assigns new fields to the `dict` output of this `Runnable`.

        ```python
        from langchain_core.language_models.fake import FakeStreamingListLLM
        from langchain_core.output_parsers import StrOutputParser
        from langchain_core.prompts import SystemMessagePromptTemplate
        from langchain_core.runnables import Runnable
        from operator import itemgetter

        prompt = (
            SystemMessagePromptTemplate.from_template("You are a nice assistant.")
            + "{question}"
        )
        model = FakeStreamingListLLM(responses=["foo-lish"])

        chain: Runnable = prompt | model | {"str": StrOutputParser()}

        chain_with_assign = chain.assign(hello=itemgetter("str") | model)

        print(chain_with_assign.input_schema.model_json_schema())
        # {'title': 'PromptInput', 'type': 'object', 'properties':
        {'question': {'title': 'Question', 'type': 'string'}}}
        print(chain_with_assign.output_schema.model_json_schema())
        # {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
        {'str': {'title': 'Str',
        'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}
        ```

        Args:
            **kwargs: A mapping of keys to `Runnable` or `Runnable`-like objects
                that will be invoked with the entire output dict of this `Runnable`.

        Returns:
            A new `Runnable`.

        """
        # Import locally to prevent circular import
        from langchain_core.runnables.passthrough import RunnableAssign  # noqa: PLC0415

        return self | RunnableAssign(RunnableParallel[dict[str, Any]](kwargs))

Domain

Subdomains

Frequently Asked Questions

What does assign() do?
assign() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is assign() defined?
assign() is defined in libs/core/langchain_core/runnables/base.py at line 772.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free