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.
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.
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.
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.
Key concepts:
- Type hints define the schema.
amount: intmeans "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.
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.
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.
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.
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.
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.
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.
LLM: "Send 1500 to 254712345678"
Agent: Parse string with regex
Risk: parsing breaks, agent crashes
LLM: {"amount": 1500, ...}
Agent: Pydantic validates
Function: Receives MPesaTransaction(amount=1500, ...)
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.
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
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 →