The problem: free-form text is fragile

You ask an LLM to extract transaction details from an M-Pesa SMS. The model returns a string like: "Sent ₹15,500 to 254712345678, ref MP202603081547". Parse it yourself with regex? That's fragile. The model might change the format. What if the reference number is missing? Your regex breaks.

📱
Real-world scenario
Arjun (Hyderabad): parsing SMS receipts with regex is pain

Arjun's fintech startup receives M-Pesa transaction SMS from users in Kenya. Every SMS is a paragraph: "Confirmed. You have sent KSh1500 to 254712345678 John Doe on 8/3/26 at 5:30PM. Ref ID: MP202603081547."

He writes a regex: r'sent (\d+).*to (\d+).*ref.*(\w+)'. It works 90% of the time. But sometimes the amount has commas, or "ref" is missing, or the sender name has special characters. His parsing breaks. He spends more time fixing regex than building features.

Then he learns about JSON mode. Instead of fighting regex, he tells the LLM: "Extract this SMS as JSON with fields amount, recipient_phone, reference. Always return valid JSON." Now his code is 10 lines and reliable.

This is why agents need structured outputs. Tools expect typed inputs. Python functions expect integers, strings, booleans—not "maybe a number in a paragraph somewhere".

Ollama's format parameter: the simplest path

Ollama supports a format="json" parameter that constrains the model's output to valid JSON. It's not magic—the model still predicts tokens—but it enforces structure at token-generation time.

Prompt + format="json"
→
[Ollama enforces JSON]
→
{"amount": 1500, "phone": "254..."} [guaranteed valid]

Why this works:

  • Fast to implement. One parameter. No schema definition yet.
  • Reliable output. Ollama blocks any token that violates JSON syntax.
  • No hidden cost. The model still runs locally; you're just constraining output.
  • Easy fallback. If JSON mode fails (rare), you can parse the response anyway.
json_mode.py Using Ollama's format parameter
Expected output
{"amount": 1500, "recipient_phone": "254712345678", "reference": "MP202603081547"}
Key insight:
format="json" tells Ollama "I want JSON back" but doesn't define what fields should be in it. The model has to infer from your prompt. For more control, you'll add Pydantic next.

Pydantic refresher: BaseModel and validation

Pydantic is a Python library for data validation using type hints. A Pydantic model is a class that validates and transforms data at creation time.

pydantic_intro.py A simple Pydantic model

Key concepts:

  • Type hints define the schema. amount: int means "amount must be an integer, coerce if possible, error if impossible".
  • Field() adds metadata. Use Field(description="...") to document fields for the LLM.
  • Validation runs at init. Pydantic checks types when you create an instance.
  • model_json_schema() exports the schema. This is what you send to the LLM to tell it what structure you want.
pydantic_schema.py Exporting the schema for the LLM

The power combo: Pydantic schema + Ollama format="json"

Send your Pydantic model's JSON schema to the LLM. Ollama enforces JSON structure. Pydantic validates the response. This is how typed data flows from the model into your Python functions.

structured_extraction.py Extract M-Pesa data with guaranteed types
Expected output
Amount: 1500 KSh Recipient: 254712345678 Reference: MP202603081547
Why this matters for agents:
Your agent's tools (functions) accept typed inputs: def send_payment(txn: MPesaTransaction). You can't pass a string. Structured outputs + Pydantic guarantee the LLM gives you the right type.

Worked example 1: Extract M-Pesa SMS fields

You receive an M-Pesa SMS. Extract amount, recipient, and reference into a structured Transaction object. Use that object to log the payment or file a report.

mpesa_extractor.py Full extraction pipeline

Worked example 2: Classify a SouthBank support email

SouthBank India receives a customer email: "My card was declined at Flipkart yesterday. Urgent!" Extract the category, priority, and sentiment. Use the Ticket object to route it to the right team.

support_ticket.py Email classification into structured Ticket
Expected output
Category: technical, Priority: high Route to: Tech support

Handling validation errors: retry and fallback

Sometimes the LLM's JSON doesn't match your schema. The amount field is "15k" instead of 15. Pydantic will raise an error. Handle it gracefully: retry with a clearer prompt, or fall back to parsing free-form text.

retry_pattern.py Retry logic for structured extraction

When JSON mode fails: prompt + regex fallback

Rarely, the model refuses or Ollama can't enforce JSON (old versions, low memory). Have a fallback: ask the model to output structured text, then parse with regex.

text_fallback.py Fallback to regex parsing
Note:
This fallback is less reliable than JSON mode, so use it only if JSON mode fails. But it's there if you need it.

Why this unlocks agents: typed function calls

An agent is a loop: LLM decides what to do → calls a function with typed inputs → executes → returns result → repeat. Without structured outputs, the LLM can't safely call functions.

Without Structured Outputs
Fragile, manual parsing

LLM: "Send 1500 to 254712345678"
Agent: Parse string with regex
Risk: parsing breaks, agent crashes

With Structured Outputs
Typed, validated inputs

LLM: {"amount": 1500, ...}
Agent: Pydantic validates
Function: Receives MPesaTransaction(amount=1500, ...)

agent_example.py An agent that uses structured outputs

How this connects to LLM agents

Every major LLM (GPT-4, Claude, Gemini) supports structured outputs or function calling. It's the same pattern: schema → LLM → validated response → function call. By learning this now with Ollama, you're learning the universal agent pattern. By Lesson 10, you'll have agents calling 10+ tools with 100% reliability.

Function Calling Tool Use Reliability Multi-Agent Systems
Mini Challenge: Extract customer email intent

Task: Define a Pydantic model for customer support emails with fields: issue_type (fraud/technical/billing), urgency (low/high), and summary (one-line). Call your LLM with format="json" on this email: "My card isn't working and I need to pay rent today—it's urgent!"

Hint 1: Define the model using BaseModel and Field().

Hint 2: Get the schema with model_json_schema().

Hint 3: Pass schema and format="json" to your chat() function.

Test your knowledge

Question 1 of 5
What does Ollama's format="json" parameter do?
Question 2 of 5
In Pydantic, what does the type hint in a BaseModel field do?
Question 3 of 5
Why is structured output critical for LLM agents?
Question 4 of 5
How do you export a Pydantic model's schema for the LLM?
Question 5 of 5
What's the best way to handle Pydantic ValidationError from the LLM response?
0
/5
Excellent! You understand structured outputs and agent patterns.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You've locked in typed data.
Now build agents that execute reliably.

Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Agentic Engineer →