langchain_core.runnables.base.chain¶

langchain_core.runnables.base.chain(func: Callable[[Input], Coroutine[Any, Any, Output]]) Runnable[Input, Output][source]¶
langchain_core.runnables.base.chain(func: Callable[[Input], Iterator[Output]]) Runnable[Input, Output]
langchain_core.runnables.base.chain(func: Callable[[Input], AsyncIterator[Output]]) Runnable[Input, Output]
langchain_core.runnables.base.chain(func: Callable[[Input], Output]) Runnable[Input, Output]

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.

Parameters

func – A callable.

Returns

A Runnable.

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}!")
    llm = OpenAI()
    formatted = prompt.invoke(**fields)

    for chunk in llm.stream(formatted):
        yield chunk

Examples using chain¶