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

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

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

    assign

    Assigns new fields to the dict output of this Runnable.

    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'}}}
    Copy
    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]

    Parameters

    NameTypeDescription
    **kwargsRunnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]
    Default:{}

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

    View source on GitHub