RunnableLambda wraps a generic JavaScript function as a LangChain runnable, allowing custom application logic to participate in executable chains and LCEL compositions. section 6.5.1
Use RunnableLambda when a chain needs to:
- Adapt application logic, such as fetching weather data, before passing results to a prompt template, language model, and output parser. section 6.1.2
- Compose custom functions with other runnables through pipe, while retaining compatibility with LCEL chains. section 6.5.1
- Stream chain output into a Next.js interface by consuming chain.stream results and forwarding updates to createStreamableUI. section 6.1.2
Weather chain with streamed UI output
Create a runnable from fetchWeatherData, then pipe it through promptTemplate, llm, and StringOutputParser. Call chain.stream({ city }), iterate over the results, append each item to textContent, and update a streamable chat component until the stream completes. section 6.1.2
const someFunc = RunnableLambda.from((input) => {
return input;
});
const chain = someFunc.pipe(runnable1);
The compact runnable pattern shows how RunnableLambda.from turns a JavaScript function into a chain component and connects it with pipe. section 6.5.1
