Large language models are powerful, but they can also be unpredictable.
When you expect a brief summary, omit fields in the JSON output, or change the format entirely from one request to another.
When you’re building an AI application that depends on structured responses, these small mistakes can lead to big failures.
That’s where pedantic comes in.
pydantic Lets you define the exact data format for both inputs and outputs of your AI system. By using it to validate model responses, you can catch inconsistencies, automatically correct some of them, and make your entire LLM workflow much more reliable.
This article goes over how you can use pedantics to predict the output of your language model, even when the model itself isn’t.
What we will cover:
Problem with unexpected LLM output
Imagine you’re building an AI app that generates summaries of product reviews. You tell the model to return a structured JSON with two fields: summary and sentiment.
Your prompt looks like this:
“Summarize this review and return JSON keys with ‘summary’ and ’emotion’.”
Most of the time, it works. But sometimes, the model adds extra text around the JSON, forgets a key, or outputs the wrong type.
For example, {"summary": "Good build quality", "sentiment": "positive"} is perfect But sometimes you get it Sure, here you go! {"summary": "Too expensive but works well"} or {"summary": "Nice camera", "sentiment": 5}.
You can try to fix this with string parsing, but it gets faster. Instead, you can specify a strict schema using pydentic and ensure that only valid responses are accepted.
What is pedantic?
pydantic is a Python library that lets you define a data model using simple classes. It automatically validates data types and structures when you create a model instance.
If something is missing or incorrect, Pedantic raises an error, helping you identify problems early.
A basic example looks like this:
from pydantic import BaseModel
class ReviewSummary(BaseModel):
summary: str
sentiment: str
data = {"summary": "Nice screen", "sentiment": "positive"}
result = ReviewSummary(**data)
print(result)
If you try to pass an integer where a string is expected, pedantic raises an explicit validation error. This is the exact procedure we can use to correct the LLM output.
Validating model responses
Let’s integrate this idea with a real LLM reaction. Let’s say you are using Openai’s API. You can ask the model to return structured data and then validate it using pedantic, like this:
import json
from pydantic import BaseModel, ValidationError
from openai import OpenAI
client = OpenAI()
class ReviewSummary(BaseModel):
summary: str
sentiment: str
prompt = "Summarize this review and return JSON with keys: summary, sentiment.\n\nReview: The phone is fast but battery drains quickly."
response = client.responses.create(
model="gpt-4o-mini",
input=prompt
)
raw_text = response.output_text
try:
parsed = json.loads(raw_text)
validated = ReviewSummary(**parsed)
print(validated)
except (json.JSONDecodeError, ValidationError) as e:
print("Validation failed:", e)
Here, the model response goes through two stages. First, it is parsed from text to JSON. Then Pydentik checks if it matches the expected schema. If something is missing, it throws an error. You can catch it and decide to handle it.
How pydantic AI makes apps more secure
LL.M. are likely. Even with perfect cues, you can never guarantee that they will follow your structure every time.
The use of the pedantic adds a neural layer on top of this uncertainty. It acts as a contract between your app and the model. Each response must adhere to this agreement. If it doesn’t, your system can immediately detect it, reject it, or retry with a clear signal.
This is especially important for production-grade AI apps where unexpected responses can break user flows, crash APIs, or corrupt data in databases.
By validating the output, you get three major benefits: predictable data formats, implicit error handling, and safer downstream processing.
Using pydantic to implement an AI response framework
You can also use pedantic in more complex workflows. Let’s say your model generates structured responses for a chatbot that requires several fields: an answer, a confidence score, and suggested follow-up questions.
from typing import List
from pydantic import BaseModel, Field
class ChatResponse(BaseModel):
answer: str
confidence: float = Field(ge=0, le=1)
follow_ups: List(str)
Now your model must return something like:
{
"answer": "You can enable dark mode in settings.",
"confidence": 0.92,
"follow_ups": ("How to change wallpaper?", "Can I set auto dark mode?")
}
If the model outputs invalid data, such as a missing key or a negative trust score, Pedantik immediately flags it.
You can then log the error, retry with a system message, or replace missing data with defaults.
Adding pedantic validation to the LLM framework
Frameworks like Lingchen And work easily with FastiPedantic.
In LangChain, you can define tool or agent schemas using pyidentical classes to ensure all interactions between models and tools.
For example:
from langchain.tools import StructuredTool
tool = StructuredTool.from_function(
func=lambda x: x * 2,
args_schema=PydanticModel,
description="Doubles the input number"
)
In FastPy, each endpoint can accept and return a PID model. This makes it perfect for AI APIs where model responses are automatically validated before being sent to clients.
Improving LLM reliability through feedback
When you start validating the output, you’ll quickly notice patterns of how your LLM fails. Sometimes it involves additional commentary, sometimes it confuses key names.
Instead of manually fine-tuning them each time, you can feed this information back into your indicator or fine-tuning data.
For example, if the model continues to write sentiments Instead sentimentadd a repair directive to your system prompt. Over time, validation errors will decrease, and the model will learn to conform to your structure more consistently.
Real world use cases
Developers use pydantic authentication in many AI systems.
In AI chatbots, this ensures consistent message formatting and trust scores. In an abstract system, it verifies that each abstract contains key fields such as title, head, or keywords. In AI-driven APIs, it acts as a buffer that prevents unsolicited data from spreading downstream.
This is particularly useful in Retrieval Related Generation (RAG) pipelines, where structured outputs such as document scores or entities are critical to maintaining correct context.
The result
The pedantic LLM brings structure to the chaos of the output. It turns unpredictable text generation into predictable, schema-checked data. By validating the model’s response, you make your AI workflow reliable, debuggable, and safe for production.
The combination of LLM flexibility and pedantic rigorous typing is powerful. You get the creativity of the language model with data validation controls.
When every output follows a schema, your AI becomes not only intelligent, but reliable.
Hope you enjoyed this article. Sign up for my free newsletter turingtalks.ai For more tutorials on AI. You can too Visit my website.