RunnableLambda
Author: Kenny Jung
Peer Review: Junseong Kim, Haseom Shin
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
Overview
RunnableLambda provides a way to integrate custom functions into your LangChain pipeline.
This allows you to define your own custom functions and execute them using RunnableLambda as part of their workflow.
For example, you can build your own logic and execute the functions, performing different tasks such as:
Data preprocessing,
Calculations,
Interactions with external APIs, and
Any other custom logic you need in your chain.
Table of Contents
References
Environment Setup
Setting up your environment is the first step. See the Environment Setup guide for more details.
[Note]
The
langchain-opentutorialis a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials.Check out the
langchain-opentutorialfor more details.
Alternatively, you can set and load OPENAI_API_KEY from a .env file.
[Note] This is only necessary if you haven't already set OPENAI_API_KEY in previous steps.
How to Execute Custom Functions
Important Note
A limitation of RunnableLambda is that custom functions can only accept a single argument. You can wrap custom functions with RunnableLambda to use them in your pipeline.
If you have a function that requires multiple parameters, create a wrapper function:
Accepts a single input (typically a dictionary).
Unpacks this input into multiple arguments inside the wrapper.
Passes these arguments to your original function.
For example:
Execute the chain and verify the result.
Using RunnableConfig as Parameters
RunnableLambda can optionally accept a RunnableConfig object.
This allows you to pass various configuration options to nested executions, including:
Callbacks: For tracking and monitoring function execution.
Tags: For labeling and organizing different runs.
Other configuration: Additional settings to control function behavior.
For example, you can use RunnableConfig to:
Track function performance.
Add logging capabilities.
Group related operations using tags.
Configure error handling and retry logic.
Set timeouts and other execution parameters.
This makes RunnableLambda highly configurable for complex workflows with fine-grained control over execution.
Last updated