How to Use Pipe Operator in LangChain for Chaining
In LangChain, the
pipe operator allows you to chain multiple components by passing the output of one directly as the input to the next. You use it by calling pipe() on a LangChain object and providing the next component, enabling clean and readable sequential workflows.Syntax
The pipe operator in LangChain is used to chain components so the output of one becomes the input of the next. The basic syntax is:
component1.pipe(component2)Here, component1 and component2 are LangChain objects like chains or tools. The pipe() method returns a new chain that runs component1 then passes its output to component2.
python
result_chain = component1.pipe(component2)
Example
This example shows how to chain two simple LangChain chains using pipe(). The first chain generates a greeting, and the second chain adds a farewell message.
python
from langchain.chains import LLMChain from langchain.llms import OpenAI from langchain.prompts import PromptTemplate # Initialize OpenAI LLM llm = OpenAI(temperature=0) # First chain: generate greeting prompt1 = PromptTemplate(template="Say hello to {name}.", input_variables=["name"]) greeting_chain = LLMChain(llm=llm, prompt=prompt1) # Second chain: add farewell prompt2 = PromptTemplate(template="Add a farewell to this message: {text}", input_variables=["text"]) farewell_chain = LLMChain(llm=llm, prompt=prompt2) # Chain them with pipe combined_chain = greeting_chain.pipe(farewell_chain) # Run the combined chain output = combined_chain.run({"name": "Alice"}) print(output)
Output
Good morning, Alice.
Goodbye!
Common Pitfalls
- Not matching input/output keys: The output key of the first component must match the input key expected by the second.
- Forgetting to use
pipe(): Trying to call components sequentially without chaining causes errors. - Using incompatible components: Only LangChain objects that support
pipe()can be chained this way.
python
from langchain.chains import LLMChain # Wrong: output key mismatch # greeting_chain outputs 'text', but farewell_chain expects 'message' # This will cause an error combined_chain = greeting_chain.pipe(farewell_chain) # Right: adjust prompt input variable to 'text' or map keys properly # Or use a custom chain to rename keys between steps
Quick Reference
| Concept | Description | Example |
|---|---|---|
| pipe() | Chains two LangChain components, passing output to input | chain1.pipe(chain2) |
| Input/Output keys | Output key of first must match input key of second | Output 'text' โ Input 'text' |
| Return value | Returns a new combined chain | combined_chain = chain1.pipe(chain2) |
| Use case | Build sequential workflows easily | Generate text โ Summarize text |
Key Takeaways
Use
pipe() to chain LangChain components for smooth data flow.Ensure output keys from one component match input keys of the next.
The pipe operator returns a new chain combining both steps.
Only compatible LangChain objects support
pipe() chaining.Chaining improves readability and modularity of LangChain workflows.