# Chat Models and the Pipe (LCEL) Chain — LangChain / LlamaIndex

Source: https://www.geekswithgeeks.com/en/langchain-llamaindex/c-chain

> Compose prompt, model and parser with the | operator.

## The pipe operator builds a sequence

Every LangChain component that can be invoked is a **Runnable** with the same methods: `invoke`, `batch`, `stream` and async versions. The **`|` operator** (LangChain Expression Language, LCEL) joins runnables into a **`RunnableSequence`**: the output of each step is the input of the next. A typical chain is `prompt | model | parser`. Because the interface is the same, you can swap the real model for a **fake model** in tests, add retries, or stream, without rewriting the chain. A chat model takes messages and returns an `AIMessage`; `StrOutputParser` extracts the text.

## prompt | model | parser, run

I ran this offline in a Python virtual environment with langchain-core 1.6.6, langchain-text-splitters 1.1.2 and llama-index-core 0.14.25. No API key or network call is needed because a fake model or a toy embedding stands in for the real one. A `FakeListChatModel` returns a canned reply, so the chain runs offline. The result is a `RunnableSequence` whose steps are the prompt, the model and the parser.

```python
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.language_models.fake_chat_models import FakeListChatModel

prompt = ChatPromptTemplate.from_template("Translate to French: {text}")
model = FakeListChatModel(responses=["Bonjour le monde"])   # stands in for a real chat model
chain = prompt | model | StrOutputParser()

print(chain.invoke({"text": "Hello world"}))
print(type(chain).__name__)
print([type(step).__name__ for step in chain.steps] if hasattr(chain, "steps") else "")

```

Output:

```
Bonjour le monde
RunnableSequence
['ChatPromptTemplate', 'FakeListChatModel', 'StrOutputParser']
```

## Swapping in a real model (illustrative)

Only the model line changes; the rest of the chain is identical. Requires the provider package and an API key in the environment; not run here.

```python
from langchain_openai import ChatOpenAI          # pip install langchain-openai
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_template("Translate to French: {text}")
model = ChatOpenAI(model="<model-name>", temperature=0)   # reads OPENAI_API_KEY
chain = prompt | model | StrOutputParser()
print(chain.invoke({"text": "Hello world"}))
```

**Quiz:** What does the | operator do in LCEL?

- [ ] Starts a server
- [ ] Performs a bitwise OR on strings
- [ ] Deletes a step
- [x] Connects runnables so each output feeds the next step

*Answer:* Connects runnables so each output feeds the next step. It composes runnables into a RunnableSequence.
