Zero to AI Engineer

Module 17 of 54

Module 17: Prompts, Messages & Output Parsing

4 min read739 words
What you'll learn
Use prompt templates with variablesUnderstand why templates beat string-gluingParse model output into structured dataCompose prompt → model → parser

"Reusable prompt templates in, structured data out. This is how you turn a chatty model into a reliable app component."

Level: Intermediate · Time: ~12 min · Prerequisites: Module 16

Learning Objectives

By the end of this module, you will be able to:

  • Use prompt templates with variables
  • Understand why templates beat string-gluing
  • Parse model output into structured data
  • Compose prompt → model → parser

1. Prompt Templates

Hard-coding prompts with string concatenation gets messy fast. LangChain's prompt templates let you write a prompt with placeholders and fill them in at runtime:

python
[object Object], langchain_core.prompts ,[object Object], ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
    (,[object Object],, ,[object Object],),
    (,[object Object],, ,[object Object],),
])
prompt.invoke({,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],})

Same template, endless reuse with different values. Notice the placeholders {role} and {topic} — at call time you pass a dictionary, and LangChain substitutes the values into the right spots. The template also preserves the message structure (system vs. human), so you're not just filling in text, you're filling in a properly-shaped conversation.

Explain like I'm new: A prompt template is a fill-in-the-blank form. You design the form once ("Explain ___ to a ___"), then reuse it for any topic and audience — cleaner and less error-prone than pasting strings together each time.

Common mistake: Building prompts with f-strings and + concatenation scattered through your code. It works for a demo, but when you need to tweak wording you'll hunt through many files, and untrusted user text glued directly into the prompt invites injection. A template centralizes the wording and inserts variables in a controlled way.

2. Why Templates Matter

  • Reusability — one template, many inputs
  • Safety — variables are inserted cleanly (less injection risk)
  • Maintainability — change the prompt in one place
  • Testing — you can test the template systematically

Key idea: Treat prompts as code, not throwaway strings. Templates make prompts versionable, testable, and reusable — the difference between a demo and a maintainable product.

3. Output Parsing: From Text to Data

LLMs return text, but your app usually needs structured data — a list, a JSON object, a number. Output parsers convert the model's text into usable structures:

python
[object Object], langchain_core.output_parsers ,[object Object], JsonOutputParser
chain = prompt | model | JsonOutputParser()
chain.invoke({,[object Object],: ,[object Object],})   ,[object Object],

Modern models also support structured output directly, guaranteeing the shape you asked for.

Real-world use case: Extracting fields from resumes — name, skills, years of experience — as clean JSON your database can store. Without parsing you'd get a paragraph; with it you get structured records your app can actually use.

4. Composing the Pieces

The elegant part: LangChain lets you pipe components together:

python
chain = prompt | model | parser

Read it left to right: fill the prompt → send to model → parse the output. This composability is the heart of building LangChain apps. Because the pipe is just wiring, you can insert, remove, or swap a stage without touching the others — add a step that logs every prompt, or slot in a different parser, and the rest of the chain keeps working.

Real-world use case: A support-ticket triager uses one chain to classify incoming tickets into {"category": ..., "urgency": ...}. The prompt template holds the instructions, the model reads the ticket, and a parser returns a clean dict the routing system can act on — no human re-typing the model's prose into a form.

Common mistake: Asking for JSON in the prompt but not parsing (or validating) it. Models occasionally return slightly malformed output. Use a parser (and handle failures) so a stray character doesn't crash your app.

Hands-On: Try This

Try this: Design a fill-in-the-blank prompt for "summarize {text} in {n} bullet points." List what variables it needs. Then decide what shape you'd want back (a list of strings) — that's your output parser. You've just designed a reusable chain.

✅ Checkpoint

  1. What problem do prompt templates solve?
  2. What does an output parser do?
  3. What does prompt | model | parser mean?

Answers: 1) Reusable, clean prompts with variables instead of string-gluing. 2) Turns the model's text into structured data (e.g., JSON/list). 3) A chain: fill prompt → call model → parse output.

Key Takeaway: Prompt templates turn prompts into reusable fill-in-the-blank forms — treat prompts as code. Output parsers convert the model's text into structured data (lists, JSON) your app can use. LangChain composes these with a pipe (prompt | model | parser), the fundamental pattern for building reliable LLM app components.

Further Learning

Adapted from the LangChain for Beginners curriculum (MIT License).