0
0
LangchainHow-ToBeginner ยท 4 min read

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

ConceptDescriptionExample
pipe()Chains two LangChain components, passing output to inputchain1.pipe(chain2)
Input/Output keysOutput key of first must match input key of secondOutput 'text' โ†’ Input 'text'
Return valueReturns a new combined chaincombined_chain = chain1.pipe(chain2)
Use caseBuild sequential workflows easilyGenerate 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.