Module 1 cheat sheet: LangChain Basics
Talk to any LLM with one interface, build prompts from templates, turn text into data, join steps with | and give the chat a memory.
Key ideas
- Chat model: messages in,
AIMessage out. invoke · stream · batch · ainvoke. Lab 01 → - Templates have
{blanks}; missing variable → KeyError. Lab 02 → - Parsers turn text into data;
with_structured_output is the best default. Lab 03 → - LCEL:
prompt | llm | StrOutputParser(). Lab 04 → - LLMs are stateless: memory = resend old messages per
session_id. Lab 05 →
Colab + Groq setup
from google.colab import userdata
import os
os.environ['GROQ_API_KEY'] = userdata.get('groq_key')
from langchain_groq import ChatGroq
llm = ChatGroq(model="openai/gpt-oss-20b")Messages
| Class | Role |
|---|
| SystemMessage | rules |
| HumanMessage | question |
| AIMessage | answer / few-shot example |
| ToolMessage | tool result |
Runnables
| Runnable | Does |
|---|
| a | b | output of a → b |
| {"x": chain} | build a dict (parallel) |
| RunnablePassthrough.assign | keep input, add key |
| RunnableParallel | branches at once |
| RunnableLambda | any Python function |
| RunnableBranch | if / else routing |
| .with_fallbacks / .with_retry | production safety |
Parsers
| Parser | Gives |
|---|
| StrOutputParser | str |
| CommaSeparatedList | list |
| JsonOutputParser | dict |
| PydanticOutputParser | validated object |
| with_structured_output | object via tool calling |
Common mistakes
- Using retired Groq Llama 3 models: use
openai/gpt-oss-20b. - User text inside the system message.
- Unescaped
{ } in JSON examples inside templates. - Building an agent when a fixed chain is enough.
- Unlimited chat history: trim it.
Created by Ajeetkumar · EasyLearnSpace