Skip to main content
This lesson demonstrates a practical, end-to-end LCEL content-generation pipeline that composes multiple mini-chains into a single workflow. The pipeline produces an impactful title, a detailed outline, a 200-word blog post, and a short social-media style summary by chaining prompt → LLM → parser → passthrough for each stage.

Key idea and imports

We use:
  • ChatPromptTemplate to define prompts with placeholders
  • ChatOpenAI as the LLM runnable (swapable per stage)
  • StrOutputParser to extract clean string outputs
  • RunnablePassthrough to attach parsed strings to keys in the execution context so downstream mini-chains can reference them
This pattern composes small, focused runnables into a chain-of-chains. Each stage returns a parsed string that is then attached to the execution context under a key like title, outline, or blog, allowing subsequent prompts to reference those values via placeholders (for example, "{title}").
Example Python imports and the pipeline wiring:

Example pipeline (title → outline → blog → summary)

Below is a compact, readable pipeline that demonstrates the pattern. Note how each mini-chain ends with a StrOutputParser() followed by attaching the parsed value to a new key with RunnablePassthrough().

Pipeline overview (quick reference)

Notes on placeholders and MDX safety

  • When mentioning prompt placeholders in prose, always wrap them in backticks so MDX does not interpret curly braces as JavaScript: "{input}", "{title}", "{outline}", "{blog}".

How RunnablePassthrough connects stages

Each mini-chain parses the LLM output into a string via StrOutputParser(). Using | {"title": RunnablePassthrough()} attaches that parsed string under the title key in the chain’s execution context. Downstream chat prompts can then reference "{title}" to receive the exact string produced by the previous stage. This produces a chain-of-chains effect: the output from one mini-chain becomes the input to the next via context keys.

Invoking the pipeline

Call the top-level chained runnable with a single input dictionary. Intermediate values (like title, outline, blog) will be attached to the chain execution context while the final return value is the summary.

Execution considerations

  • This is a multi-stage pipeline: each mini-chain triggers a separate LLM call and parsing step. Expect longer runtimes than a single LLM request.
  • For rapid iteration you can execute each mini-chain independently to validate or refine a stage without running the entire pipeline.

Extending the pipeline: different LLMs per stage

A common pattern is to use specialized models per task:
  • Use a fast, cheap model tuned for short creative outputs for the title stage.
  • Use a model good at structured responses for the outline stage.
  • Use a high-quality, longer-context model for the blog stage.
To swap models, replace ChatOpenAI() with another LLM runnable at the stage you want to change. The LCEL wiring (prompts → parser → passthrough) remains identical.
Using different LLMs per mini-chain is a powerful approach: you can optimize cost and quality by selecting the best model for each subtask (e.g., catchy titles, structured outlines, long-form writing, and concise summarization).

What we achieved

  • Demonstrated how LCEL composes small runnable units (prompt → LLM → parser → attached key) into a larger, maintainable workflow.
  • Built a reusable pattern for content generation, multi-stage processing, and orchestrating different LLMs for specialized subtasks.
In a future lesson we will explore memory, retrieval, and building more complex chains and integrations—stay tuned.

Watch Video