Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toBind runtime argsConfigurable AlternativesAdd fallbacksRun arbitrary functionsUse RunnableParallel/RunnableMapRoute between multiple RunnablesCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageHow toRun arbitrary functionsOn this pageRun arbitrary functionsYou can use arbitrary functions in the pipelineNote that all inputs to these functions need to be a SINGLE argument. If you have a function that accepts multiple arguments, you should write a wrapper that accepts a single input and unpacks it into multiple argument.from langchain.schema.runnable import RunnableLambdafrom langchain.prompts import ChatPromptTemplatefrom langchain.chat_models import ChatOpenAIfrom operator import itemgetterdef length_function(text):    return len(text)def _multiple_length_function(text1, text2):    return len(text1) * len(text2)def multiple_length_function(_dict):    return _multiple_length_function(_dict["text1"], _dict["text2"])prompt = ChatPromptTemplate.from_template("what is {a} + {b}")model = ChatOpenAI()chain1 = prompt | modelchain = {    "a": itemgetter("foo") | RunnableLambda(length_function),    "b": {"text1": itemgetter("foo"), "text2": itemgetter("bar")} | RunnableLambda(multiple_length_function)} | prompt | modelchain.invoke({"foo": "bar", "bar": "gah"})    AIMessage(content='3 + 9 equals 12.', additional_kwargs={}, example=False)Accepting a Runnable Config‚ÄãRunnable lambdas can optionally accept a RunnableConfig, which they can use to pass callbacks, tags, and other configuration information to nested runs.from langchain.schema.runnable import RunnableConfigfrom langchain.schema.output_parser import StrOutputParserimport jsondef parse_or_fix(text:
You can use arbitrary functions in the pipeline
You can use arbitrary functions in the pipeline ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toBind runtime argsConfigurable AlternativesAdd fallbacksRun arbitrary functionsUse RunnableParallel/RunnableMapRoute between multiple RunnablesCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageHow toRun arbitrary functionsOn this pageRun arbitrary functionsYou can use arbitrary functions in the pipelineNote that all inputs to these functions need to be a SINGLE argument. If you have a function that accepts multiple arguments, you should write a wrapper that accepts a single input and unpacks it into multiple argument.from langchain.schema.runnable import RunnableLambdafrom langchain.prompts import ChatPromptTemplatefrom langchain.chat_models import ChatOpenAIfrom operator import itemgetterdef length_function(text):    return len(text)def _multiple_length_function(text1, text2):    return len(text1) * len(text2)def multiple_length_function(_dict):    return _multiple_length_function(_dict["text1"], _dict["text2"])prompt = ChatPromptTemplate.from_template("what is {a} + {b}")model = ChatOpenAI()chain1 = prompt | modelchain = {    "a": itemgetter("foo") | RunnableLambda(length_function),    "b": {"text1": itemgetter("foo"), "text2": itemgetter("bar")} | RunnableLambda(multiple_length_function)} | prompt | modelchain.invoke({"foo": "bar", "bar": "gah"})    AIMessage(content='3 + 9 equals 12.', additional_kwargs={}, example=False)Accepting a Runnable Config‚ÄãRunnable lambdas can optionally accept a RunnableConfig, which they can use to pass callbacks, tags, and other configuration information to nested runs.from langchain.schema.runnable import RunnableConfigfrom langchain.schema.output_parser import StrOutputParserimport jsondef parse_or_fix(text: