A team is building a LangChain pipeline that must: (1) accept a user question, (2) retrieve relevant document chunks, (3) format them into a prompt, and (4) stream tokens to the UI in real time. Using LangChain Expression Language (LCEL), which approach correctly enables streaming on the composed chain?
Show answer & explanation
Correct answer: B
WHY B is correct: In LCEL, the pipe operator (|) composes Runnable objects into a pipeline. The chain is defined as retriever | prompt | llm | StrOutputParser() and streaming is activated by calling .stream(input) on the composed chain object. The LLM node automatically yields tokens when .stream() is called at the chain level.
WHY NOT A: .stream() is an invocation method, not a pipe-operator argument. Writing llm.stream() inline with | produces a syntax error — the pipe operator expects Runnable objects, not coroutine call results.
WHY NOT C: LLMChain is the legacy pre-LCEL class. It does not accept retriever= as a constructor parameter and does not support stream=True as a run() keyword argument in the standard API.
WHY NOT D: Manually chaining .invoke(), .format(), and .predict() produces sequential blocking synchronous calls. It does not compose a streaming pipeline and provides no real-time token delivery.