The pipe operator helps you connect multiple steps in a chain easily. It makes your code cleaner and shows how data flows from one step to the next.
When you want to run several tasks one after another in a clear order.
When you want to pass the output of one function directly as input to the next.
When you want to build a readable flow of operations without nested code.
When you want to combine simple chains into a bigger process.
When you want to debug or understand how data moves through your program.
Syntax
LangChain
chain1 | chain2 | chain3
The pipe operator '|' connects chains so output flows from left to right.
Each chain receives the previous chain's output as input automatically.
Examples
This runs chainA first, then passes its output to chainB.
LangChain
chainA | chainB
This connects three chains in order, making a smooth flow of data.
LangChain
chain1 | chain2 | chain3
You can group chains with parentheses to control the order of composition.
LangChain
chainX | (chainY | chainZ)
Sample Program
This example shows two chains: one translates English text to French, the other summarizes the French text. Using the pipe operator, we connect them so the translation output goes directly to the summarizer. Finally, we run the combined chain with some input text.
LangChain
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
# Create two simple chains
llm = OpenAI(temperature=0)
chain1 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Translate '{text}' to French."))
chain2 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Summarize the French text: '{text}'."))
# Compose chains using pipe operator
full_chain = chain1 | chain2
# Run the full chain
result = full_chain.run(text="Hello, how are you?")
print(result)
OutputSuccess
Important Notes
Make sure each chain's output matches the next chain's expected input format.
The pipe operator improves readability compared to nested calls.
Summary
The pipe operator connects chains so data flows smoothly from one to the next.
It helps write clear and simple chain compositions.
Use it when you want to build multi-step processes in LangChain.
Practice
(1/5)
1. What is the main purpose of the pipe operator in LangChain chain composition?
easy
A. To stop the data flow between chains
B. To create a new chain without connecting existing ones
C. To connect multiple chains so data flows from one to the next
D. To convert chains into plain text
Solution
Step 1: Understand the pipe operator role
The pipe operator is designed to link chains so output from one becomes input to the next.
Step 2: Compare options with this role
Only To connect multiple chains so data flows from one to the next describes connecting chains for smooth data flow, matching the pipe operator's purpose.
Final Answer:
To connect multiple chains so data flows from one to the next -> Option C
Quick Check:
Pipe operator = chain connection [OK]
Hint: Pipe operator links chains for smooth data flow [OK]
Common Mistakes:
Thinking pipe operator creates chains alone
Believing it stops data flow
Confusing it with data conversion
2. Which of the following is the correct syntax to compose two chains chain1 and chain2 using the pipe operator in LangChain?
easy
A. composed_chain = chain1 | chain2
B. composed_chain = chain1 & chain2
C. composed_chain = chain1 + chain2
D. composed_chain = chain1 >> chain2
Solution
Step 1: Recall pipe operator syntax
In LangChain, the pipe operator is represented by the vertical bar | to compose chains.
Step 2: Match syntax with options
composed_chain = chain1 | chain2 uses | correctly between chain1 and chain2. Others use incorrect operators.
Final Answer:
composed_chain = chain1 | chain2 -> Option A
Quick Check:
Pipe operator = | symbol [OK]
Hint: Pipe operator is the vertical bar | between chains [OK]
A. Using & instead of | causes a syntax or runtime error
B. It correctly composes chains with & operator
C. It overwrites the composed chain without error
D. It creates a new chain that runs both chains in parallel
Solution
Step 1: Identify correct operator for chain composition
LangChain uses the pipe operator | to compose chains, not &.
Step 2: Analyze effect of using & operator
Using & is invalid syntax or unsupported, causing an error when running the code.
Final Answer:
Using & instead of | causes a syntax or runtime error -> Option A
Quick Check:
Wrong operator = error [OK]
Hint: Only use | for chaining; & causes errors [OK]
Common Mistakes:
Assuming & works like |
Ignoring syntax errors from wrong operator
Thinking & runs chains in parallel
5. You want to build a LangChain process where data flows through three chains: chainA, chainB, and chainC. You also want to add a filter chain filterChain that only passes data if it meets a condition after chainB. Which pipe operator composition correctly implements this?
hard
A. finalChain = chainA & chainB | filterChain | chainC
B. finalChain = chainA | filterChain | chainB | chainC
C. finalChain = filterChain | chainA | chainB | chainC
D. finalChain = chainA | chainB | filterChain | chainC
Solution
Step 1: Understand desired data flow order
Data should flow: chainA -> chainB -> filterChain -> chainC, so filterChain filters after chainB.
Step 2: Match pipe composition to order
finalChain = chainA | chainB | filterChain | chainC composes chains in correct order using pipe operator. Others reorder or use wrong operator.