Key idea and imports
We use:ChatPromptTemplateto define prompts with placeholdersChatOpenAIas the LLM runnable (swapable per stage)StrOutputParserto extract clean string outputsRunnablePassthroughto 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 pipeline (title → outline → blog → summary)
Below is a compact, readable pipeline that demonstrates the pattern. Note how each mini-chain ends with aStrOutputParser() 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 viaStrOutputParser(). 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 (liketitle, 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
titlestage. - Use a model good at structured responses for the
outlinestage. - Use a high-quality, longer-context model for the
blogstage.
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.