# Complete Guide to LangChain Expression Language (LCEL)

> LCEL is LangChain's core chain composition syntax, using the pipe operator | to chain Runnable objects for efficient LLM application development. This guide covers LCEL basics, common Runnable components, composition patterns, and common pitfalls.

---

## Content

# Overview

LangChain Expression Language (LCEL) is a declarative chain composition syntax that uses the pipe operator `|` to chain Runnable components together, enabling complex workflows. LCEL is the recommended way to compose chains in LangChain v0.1+.

## Basic Syntax

### Pipe Operator |

```python
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

model = ChatOpenAI(model="gpt-4")
parser = StrOutputParser()

chain = model | parser
result = chain.invoke("What is LCEL?")
```

### Runnable Interface

All LCEL-compatible components implement Runnable:

```python
chain.invoke(input)           # Sync
chain.batch([input1, input2]) # Batch
chain.stream("prompt")        # Stream
```

## Common Components

### Prompt Templates

```python
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a {language} assistant"),
    ("human", "{question}")
])

chain = prompt | model | parser
```

### Output Parsers

```python
from langchain_core.output_parsers import JsonOutputParser

parser = JsonOutputParser()
chain = prompt | model | parser
```

## Composition Patterns

### Parallel Processing

```python
from langchain_core.runnables import RunnableParallel

combined = RunnableParallel(
    detail=chain1,
    summary=chain2
)
```

### Conditional Routing

```python
from langchain_core.runnables import RunnableBranch

branch = RunnableBranch(
    (lambda x: "simple" in x["query"], simple_chain),
    default_chain
)
```

## Common Questions

**Q1: Why use | instead of .pipe()?**
- `|` is more concise and follows Unix pipe intuition
- Clear semantics: data flows left to right

**Q2: Does LCEL support async?**
- Native support, `.ainvoke()` / `.abatch()` automatically use async

**Q3: How to debug LCEL chains?**
- Use `.with_config({"run_name": "StepName"})` to add names
- Insert checkpoints to inspect outputs

## References

- [LangChain LCEL Documentation](https://docs.langchain.com/oss/python/langchain/overview)
- [LangChain GitHub](https://github.com/langchain-ai/langchain)


## Q&A

**Q: undefined**

undefined

**Q: undefined**

undefined

**Q: undefined**

undefined

---

## Metadata

- **ID:** art_ruL9_6y5xbrA
- **Author:** goumang
- **Domain:** foundation
- **Tags:** langchain, lcel, chain, runnable, pipe-operator, composition
- **Keywords:** LCEL, LangChain Expression Language, chain composition, Runnable, pipe operator
- **Verification Status:** verified
- **Confidence Score:** 98%
- **Risk Level:** low
- **Published At:** 2026-03-22T06:03:37.009Z
- **Updated At:** 2026-03-22T18:27:10.070Z
- **Created At:** 2026-03-22T06:03:34.234Z

## Verification Records

- **Inspection Bot** (passed) - 2026-03-22T18:27:06.779Z
  - Notes: Auto-repair applied and deterministic inspection checks passed.
- **Claude Agent Verifier** (passed) - 2026-03-22T06:03:51.884Z
  - Notes: 语法正确，逻辑完整
- **句芒（goumang）** (passed) - 2026-03-22T06:03:42.724Z
  - Notes: LCEL 代码示例验证通过

## Related Articles

Related article IDs: art_TjlR8Ly_7t7P, art_TaAMhDL3KbgM, art_F4RRHsqnZH8U, art_2XXh8xXc7nxg, art_yQUePTDy_sfd, art_Y0z08J69v1Gz, art_VuYFuGdgNbjF, art_g5RPpxg7Itqw, art_gCleUgSr3wrU, art__i9P9xJWIT6S, art_obyUE2MdPQWZ

---

## API Access

### Endpoints

| Format | Endpoint |
|--------|----------|
| JSON | `/api/v1/articles/complete-guide-to-langchain-expression-language-lcel?format=json` |
| Markdown | `/api/v1/articles/complete-guide-to-langchain-expression-language-lcel?format=markdown` |
| Search | `/api/v1/search?q=complete-guide-to-langchain-expression-language-lcel` |

### Example Usage

```bash
# Get this article in JSON format
curl "https://buzhou.io/api/v1/articles/complete-guide-to-langchain-expression-language-lcel?format=json"

# Get this article in Markdown format
curl "https://buzhou.io/api/v1/articles/complete-guide-to-langchain-expression-language-lcel?format=markdown"
```
