liveBook · Manning

LCEL

As explained in AI Agents and Applications

LCEL is LangChain's composition language for building modular and expressive large-language-model pipelines.

LangChain Expression Language (LCEL) connects the steps of an LLM application without a pile of glue code. In a research summarization engine, those steps can include generating search queries, fetching pages, extracting content, summarizing results, and assembling a report. Each step can be built as a small chain, then connected into a larger workflow. section 4.7 section 4.8

An LCEL chain is built from components that follow the Runnable interface. The pipe operator (`|`) passes one component’s output to the next component’s input. Prompt templates, models, and output parsers can therefore form a chain; `invoke()` runs it once, while `stream()` and `batch()` provide other execution modes. Asynchronous versions are also available. section 1.7 section 4.7

Build and improve a selection chain

Start with `selection_prompt | get_llm()` and call `invoke(question)`. The result includes message content and response metadata, which is cumbersome when later steps need just the selected instructions. Extend the chain to `{'user_question': RunnablePassthrough()} | selection_prompt | get_llm() | StrOutputParser() | to_obj`. The passthrough maps and preserves the question, the parser extracts response text, and `to_obj` converts it to a dictionary for subsequent steps. section 4.7.1

Choose composition tools to match the work:

  • Use `RunnableLambda` to wrap a Python callable, such as a function that splits text into chunks, as a chain component. section 3.1.2
  • Use `RunnableParallel` for independent operations on the same input; use `.map()` to run a chain for each URL or search result. section 4.8
  • Keep processing stages in separate chains before assembling the main chain. LCEL also supports fallbacks and can expose execution steps to LangSmith for tracing. section 4.7
Figure 4.5 Architecture of the chain-based research summarization engine. Each step of the process is reimplemented as a mini-chain; all mini-chains are assembled into a master Web Research chain.
figure

The research-engine diagram shows how individual processing chains fit into one master chain. section 4.7

LCEL is useful beyond summarization: a conversational retrieval workflow can connect a retriever, prompt, chat history, and model. Composition makes the flow explicit, but it does not remove the need to test outputs and trace execution when a workflow behaves unexpectedly. section 7.4.4 section 1.2

How AI Agents and Applications covers it

How deep it goes
Works through pipe composition, input mapping, parsing, sub-chains, and parallel execution; introduces fallbacks and tracing without detailing their implementation.
Examples
Uses a question-to-instructions chain, text-chunking function, parallel web-research chains, and a chatbot with memory.
What it assumes
Starts with Runnable and LCEL basics, while examples require Python functions, LangChain imports, and an OpenAI API key.
What you can do after
Build it from scratch with LCEL by composing and testing modular chains, then combining them into a research summarization workflow.
How it connects
Links the Runnable interface with prompt templates, output parsers, parallel processing, retrieval-augmented generation, and LangSmith tracing.

Also explained in

Compare how 2 books explain LCEL →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.