Get started ยท 5 min read
Quickstart
Build a prompt, model and parser pipeline in under ten lines.
A LangChain application is usually a composition of small pieces: a prompt template, a chat model, and something that shapes the output. Each piece is a runnable, and runnables compose with the pipe operator.
Your first chain
python
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain.chat_models import init_chat_model
prompt = ChatPromptTemplate.from_template(
"Explain {topic} to a {audience} in three sentences."
)
model = init_chat_model("gpt-4o-mini", model_provider="openai")
chain = prompt | model | StrOutputParser()
print(chain.invoke({"topic": "vector search", "audience": "product manager"}))Streaming the response
Every runnable supports the same interface, so swapping invoke for stream is all it takes to render tokens as they arrive.
python
for chunk in chain.stream({"topic": "embeddings", "audience": "designer"}):
print(chunk, end="", flush=True)What to read next
- Core concepts, for the vocabulary behind runnables and messages.
- Retrieval augmented generation, to ground answers in your own data.
- Agents and tools, when the model needs to decide what to do next.