Your First Chain
A minimal chain: a prompt template feeds a chat model, whose output feeds a parser. The | operator wires them together.
from langchain.chat_models import init_chat_model
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template(
"Explain {topic} in one sentence for a beginner."
)
model = init_chat_model("gpt-4o-mini", model_provider="openai")
parser = StrOutputParser()
chain = prompt | model | parser
result = chain.invoke({"topic": "vector embeddings"})
print(result)
What's happening
prompttakes a dict ({"topic": "..."}) and fills the template, producing aPromptValue.modeltakes thatPromptValue, calls the LLM, and returns anAIMessage.parsertakes theAIMessageand extracts just the string content.
| doesn't call anything — it builds a RunnableSequence object. Nothing runs until .invoke(...) is called on the composed chain.
Every piece in this chain implements the same Runnable interface — that's why | works uniformly regardless of what's on either side of it. That interface, and the rest of what a Runnable can do beyond invoke, is covered next.
See also
- installation.md, keys-and-config.md — getting to the point this snippet assumes.
- Core Primitives → Runnables & LCEL (next section) — the full
Runnablecontract this chain relies on.