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-corerunnablesbasechain
    Function●Since v0.1

    chain

    Decorate a function to make it a Runnable.

    Sets the name of the Runnable to the name of the function. Any runnables called by the function will be traced as dependencies.

    Copy
    chain(
      func: Callable[[Input], Output] | Callable[[Input], Iterator[Output]] | Callable[[Input], Coroutine[Any, Any, Output]] | Callable[[Input], AsyncIterator[Output]]
    ) -> Runnable[Input, Output]

    Example:

    from langchain_core.runnables import chain
    from langchain_core.prompts import PromptTemplate
    from langchain_openai import OpenAI
    
    @chain
    def my_func(fields):
        prompt = PromptTemplate("Hello, {name}!")
        model = OpenAI()
        formatted = prompt.invoke(**fields)
    
        for chunk in model.stream(formatted):
            yield chunk

    Used in Docs

    • Build a semantic search engine with LangChain
    • Trace LangChain applications (Python and JS/TS)
    • Agentql integration
    • Anchor browser integration
    • Dappier integration

    Parameters

    NameTypeDescription
    func*Callable[[Input], Output] | Callable[[Input], Iterator[Output]] | Callable[[Input], Coroutine[Any, Any, Output]] | Callable[[Input], AsyncIterator[Output]]

    A Callable.

    View source on GitHub