Prompts & Output Parsing
Master LangChain Prompts and Output Parsing — PromptTemplate, FewShotPromptTemplate, ChatPromptTemplate, MessagesPlaceholder, StrOutputParser, JsonOutputParser, PydanticOutputParser with full code examples.
Prompts are the primary way you communicate with LLMs — getting them right is the difference between mediocre and exceptional outputs. This module covers every prompt type in LangChain's prompt toolkit, from basic templates to few-shot examples and dynamic chat prompts. Then we cover Output Parsers, which transform raw LLM text into structured Python objects (JSON, lists, Pydantic models) that your application can use directly.
Prompt Templates & Few-Shot Learning
Why this matters
PromptTemplate turns strings into reusable, parameterized inputs — the first abstraction over raw prompts.
PromptTemplate parameterizes prompts with variables; FewShotPromptTemplate adds in-context examples.
from langchain_core.prompts import PromptTemplate
template = PromptTemplate.from_template("Translate {text} to {language}.")
prompt = template.invoke({"text": "Hello", "language": "French"})
print(prompt.to_string())- Validate variables before invoke — missing keys raise clear errors.
- Keep templates in version control; treat prompts as code.
Common mistakes
- Hard-coding API keys in source instead of environment variables.
- Passing raw strings where ChatPromptTemplate expects message tuples.
- Skipping text splitting before embedding large PDFs (context overflow).
Interview checkpoints
- Q: Explain prompt templates in LangChain. A: One-sentence definition + one API name.
- Q: Common bug? A: Keys, message format, or missing split/embed step.
Practice
- Basic: Sketch a minimal prompt templates snippet.
- Intermediate: Run a notebook cell demonstrating Prompt Templates.
- Advanced: Break Prompt Templates intentionally and interpret the error.
Recap
- You can explain prompt templates clearly.
- You know one mistake to avoid.
- You see how this connects to the next lesson.
Next: Output Parsers
Chat Prompts & Output Parsers
Why this matters
Output parsers convert messy LLM text into JSON or Pydantic objects your app can consume reliably.
ChatPromptTemplate builds role-based message lists. Output parsers structure LLM responses.
StrOutputParser— plain text from chat models.JsonOutputParser/ Pydantic — typed objects for downstream code.MessagesPlaceholder— inject conversation history dynamically.
Common mistakes
- Hard-coding API keys in source instead of environment variables.
- Passing raw strings where ChatPromptTemplate expects message tuples.
- Skipping text splitting before embedding large PDFs (context overflow).
Interview checkpoints
- Q: Explain output parsers in LangChain. A: One-sentence definition + one API name.
- Q: Common bug? A: Keys, message format, or missing split/embed step.
Practice
- Basic: Sketch a minimal output parsers snippet.
- Intermediate: Run a notebook cell demonstrating Output Parsers.
- Advanced: Break Output Parsers intentionally and interpret the error.
Recap
- You can explain output parsers clearly.
- You know one mistake to avoid.
- You see how this connects to the next lesson.
Next: Chains & LCEL
