AI Hints
Home›LangChain Tutorial›Examples, Chains, Parsers
LangChain · Lesson 17 of 120

Chains with the Pipe

Prompt then model.

⏱ 2 min read▶ Video at 17:21🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

So far, we fill a template, then pass the result to the model, by hand. LangChain lets you connect the steps with the pipe symbol, a vertical bar. The result is called a chain.

Chains with the Pipe
From the video: Chains with the Pipe
  • Fill, then call, by hand
  • Connect with |
  • A chain

prompt | model

Prompt, pipe, model, creates a chain. When we invoke the chain with the template's inputs, it fills the prompt, and passes the messages straight into the model. Run it. One call, two steps. The output of each step becomes the input of the next.

pipe.pypython
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
prompt = ChatPromptTemplate.from_template("Give one fun fact about {topic} in one sentence.")

chain = prompt | model
reply = chain.invoke({"topic": "owls"})
print(type(reply).__name__)
print(reply.text)
Output
AIMessage
Owls cannot move their eyeballs in their sockets, which is why they have to swivel their
entire heads up to 270 degrees to look around!

Data flowing through a chain

Think of a chain as a pipeline. A dictionary of inputs goes in. The prompt template turns it into messages. The model turns the messages into an AI message. And, as we'll see next, a parser can turn that into plain text. Each piece only needs to understand what's handed to it.

  1. {'topic': 'owls'}
  2. Prompt → messages
  3. Model → AIMessage
  4. Parser → text
Data flowing through a chain
From the video: Data flowing through a chain

Summary

The pipe connects steps into a chain, where each step's output feeds the next. Next, parsing the model's reply into plain text.

Key takeaways
  • prompt | model
  • One invoke
  • Output feeds input
Next up: Output Parsers — just the text