LangChain Architecture and LCEL
LangChain components, LCEL pipe syntax, chains, runnable interfaces, streaming chains.
LangChain Architecture and LCEL is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is LangChain?
LangChain is a framework for building LLM applications by composing reusable pieces: prompts, models, parsers, retrievers, and memory. Instead of hand-wiring API calls, you build chains that pass data from one component to the next.
pip install langchain langchain-openaiThe Core Building Blocks
Three components appear in almost every chain: a PromptTemplate (formats input into a prompt), a chat model (the LLM), and an output parser (cleans the result). LangChain connects them into a pipeline.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParserBuilding a Prompt Template
A prompt template holds placeholders filled at runtime. Use {variable} syntax. When you invoke the chain with a dict, those keys are substituted in.
prompt = ChatPromptTemplate.from_template(
"Explain {topic} to a {audience} in two sentences."
)LCEL: The Pipe Syntax
LCEL (LangChain Expression Language) composes components with the pipe operator |. The output of each step flows into the next, reading left to right like a Unix pipeline.
model = ChatOpenAI(model="gpt-4o")
chain = prompt | model | StrOutputParser()Why StrOutputParser?
A chat model returns a message object, not a plain string. StrOutputParser() extracts the .content text so the chain yields a clean string. Without it you would get a structured message back.
Invoking the Chain
Run the chain with chain.invoke(), passing a dict that fills the template variables. The result is the parsed string output.
result = chain.invoke({"topic": "vectors", "audience": "beginner"})
print(result)Streaming a Chain
For real-time output, call chain.stream() instead of invoke. It returns a generator that yields output chunks as the model produces them, just like raw API streaming.
for chunk in chain.stream({"topic": "RAG", "audience": "engineer"}):
print(chunk, end="", flush=True)Batching Multiple Inputs
Process many inputs efficiently with chain.batch(). Pass a list of input dicts and LangChain runs them concurrently, returning a list of results in the same order.
inputs = [
{"topic": "loops", "audience": "kid"},
{"topic": "recursion", "audience": "kid"}
]
results = chain.batch(inputs)RunnablePassthrough
RunnablePassthrough forwards its input unchanged. It is essential when a chain must keep the original input alongside a transformed value, common in RAG where you pass both the question and retrieved context.
from langchain_core.runnables import RunnablePassthrough
chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt | model | StrOutputParser()
)Composing with Dictionaries
When a step is a dict, each value runs in parallel and the results form the input to the next step. This builds the multi-input prompts that power retrieval pipelines, mapping each key to a runnable or a passthrough.
parallel = {
"context": retriever,
"question": RunnablePassthrough()
}
# parallel feeds {"context": ..., "question": ...} downstreamWhy LCEL Matters
LCEL chains get streaming, batching, and async for free, are easy to read, and snap together like Lego. The same pipe syntax scales from a one-line prompt to a full RAG system.
Quick Check
Test your LCEL understanding.
Recap: Architecture and LCEL
You composed LangChain components with the pipe operator: prompt | model | StrOutputParser(). You ran chains with invoke, streamed with stream, and processed many inputs with batch. RunnablePassthrough and dict composition let you build multi-input pipelines for RAG.
Frequently asked questions
Is the “LangChain Architecture and LCEL” lesson free?
Yes — the full text of “LangChain Architecture and LCEL” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “LangChain Architecture and LCEL”?
LangChain components, LCEL pipe syntax, chains, runnable interfaces, streaming chains. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “LangChain Architecture and LCEL” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- LangChain Architecture and LCEL
- Document Loading, Splitting, and Embedding
- Vector Stores: Chroma and FAISS
- Building a RAG Q&A System End-to-End