AI / LangGraph LangChain Interview questions
What are RunnablePassthrough and RunnableLambda?
RunnablePassthrough and RunnableLambda are utility Runnables that solve two common chain-building problems: passing input data unchanged to a later step, and wrapping arbitrary Python logic as a Runnable step.
RunnablePassthrough simply passes whatever it receives as input directly to its output. It is most useful in RAG chains where you need to forward the original question to the prompt while also fetching documents in parallel:
from langchain_core.runnables import RunnablePassthrough
chain = (
{"context": retriever, "question": RunnablePassthrough()}
| rag_prompt
| llm
| StrOutputParser()
)
# 'question' is passed through unchanged; 'context' is fetched from the retriever
RunnablePassthrough.assign(key=fn) extends this by adding new keys to the dict while keeping existing ones.
RunnableLambda wraps any Python function as a Runnable so it can participate in an LCEL chain:
from langchain_core.runnables import RunnableLambda
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
chain = retriever | RunnableLambda(format_docs) | prompt | llm | StrOutputParser()
# Shorthand: lambda automatically wraps when piped
chain = retriever | (lambda docs: "\n".join(d.page_content for d in docs)) | prompt
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
