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
More Related questions...