AI / LangGraph LangChain Interview questions
How do you use the pipe operator in LCEL?
The pipe operator | in LCEL connects two Runnable objects so that the output of the left side becomes the input of the right side. It is syntactic sugar for RunnableSequence(left, right) and works because LangChain overloads Python's __or__ and __ror__ dunder methods on the Runnable base class.
Basic usage — each step must accept what the previous step returns:
from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI from langchain_core.output_parsers import StrOutputParser # prompt returns ChatPromptValue # model accepts ChatPromptValue, returns AIMessage # parser accepts AIMessage, returns str chain = ( ChatPromptTemplate.from_template("Explain {concept} in one sentence.") | ChatOpenAI(model="gpt-4o-mini") | StrOutputParser() ) print(chain.invoke({"concept": "recursion"}))
You can also chain dicts (automatically wrapped in RunnableParallel) or lambda functions (wrapped in RunnableLambda). Input/output type compatibility is checked lazily at runtime — LangChain will raise a clear error if types don't align.
# Dict shorthand for RunnableParallel at the start: chain = ( {"context": retriever, "question": RunnablePassthrough()} | rag_prompt | ChatOpenAI() | StrOutputParser() )
More Related questions...