Skip to main content
Article
langchainlcelai-pipelinechain-compositiondeclarative-aistreamingpython

Build Composable AI Pipelines with LangChain Expression Language (LCEL)

Use LangChain Expression Language (LCEL) to declaratively build AI applications. Chain prompts, models, and parsers together with a simple pipe `|` operator to create robust, streamable, and parallelizable AI workflows.

beginner30 min5 steps
The play
  1. Create a Basic LCEL Chain
    First, install the necessary packages and set your OpenAI API key. Then, define a prompt template, a model, and chain them together using the `|` operator. This is the fundamental syntax of the LangChain Expression Language (LCEL).
  2. Add an Output Parser
    The model returns an AIMessage object. To get a clean string output, pipe the model's result into an output parser. The `StrOutputParser` is a common choice for converting chat message outputs into strings.
  3. Stream the Response
    One of LCEL's key features is native streaming. Instead of waiting for the full response, use the `.stream()` method on any chain to receive tokens as they are generated by the model. This is crucial for building responsive, real-time applications.
  4. Execute in Parallel with RunnableParallel
    LCEL makes it easy to run parts of your chain in parallel. Use `RunnableParallel` to define a dictionary where each value is a runnable that will be executed concurrently. This is useful for fetching data or preparing multiple inputs for a final prompt.
  5. Add Fallbacks for Robustness
    Create more resilient applications by adding fallbacks. The `.with_fallbacks()` method allows you to specify a list of alternative chains to try if the primary one fails. This is useful for handling API errors or rate limits.
Starter code
import os
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
from langchain_openai import ChatOpenAI

# 1. Set your OpenAI API Key as an environment variable
# Ensure you have the key before running:
# export OPENAI_API_KEY='your-api-key'
if 'OPENAI_API_KEY' not in os.environ:
    print("Error: OPENAI_API_KEY environment variable not set.")
    exit()

# 2. Define the components of your chain
prompt_template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant who provides concise answers."),
    ("user", "Tell me a short story about a {character} who discovers a {object}.")
])

model = ChatOpenAI(model="gpt-3.5-turbo")

output_parser = StrOutputParser()

# 3. Build the chain using LangChain Expression Language (LCEL)
# This chain demonstrates passing through context and parallel execution.
# 'story' is generated by the main prompt, while 'character' is passed through.
runnable_parallel = RunnableParallel(
    story=(prompt_template | model | output_parser),
    character=RunnablePassthrough()
)

# 4. Invoke the chain
print("--- Invoking Chain ---")
input_data = {"character": "curious robot", "object": "glowing cube"}
result = runnable_parallel.invoke(input_data)
print(f"Character: {result['character']['character']}")
print(f"Story: {result['story']}")

# 5. Stream the response from one part of the parallel chain
print("\n--- Streaming Story ---")
story_chain = prompt_template | model | output_parser
for chunk in story_chain.stream(input_data):
    print(chunk, end="", flush=True)
print()
Build Composable AI Pipelines with LangChain Expression Language (LCEL) — Action Pack