Validate LLM outputs against schemas with automatic retry and JSON extraction.
Project description
llm-output-guard
A production-ready Python package that validates LLM outputs against schemas, with automatic JSON extraction and retry logic.
Features
- Schema-agnostic — works with Pydantic v2/v1, JSON Schema, and plain Python dicts
- Automatic JSON extraction — strips markdown fences and prose wrappers from LLM responses
- Configurable retry — fixed, exponential, or linear back-off with jitter
- Zero hard dependencies — the core works with nothing installed; extras add integrations
- Integrations — LangChain, FastAPI, OpenAI SDK
- CLI — validate JSON files against schemas from the terminal
- Fully typed —
py.typedmarker, completemypy --strictcoverage
The Problem It Solves
Getting structured data out of an LLM reliably requires a surprising amount of boilerplate — JSON extraction, schema validation, retry logic with error feedback, and prompt engineering. Here's what that looks like today versus with this package.
Before — ~100 lines of manual plumbing
import json
import re
import time
from typing import Any, Dict, Optional
class LLMJobValidator:
def __init__(self, model="gpt-4"):
self.model = model
self.max_retries = 3
def generate_job(self, description: str) -> Optional[Dict]:
prompt = self._build_prompt(description)
for attempt in range(self.max_retries):
response = self._call_llm(prompt)
data = self._extract_json(response)
if not data:
continue
errors = self._validate_job_data(data)
if not errors:
return data
prompt = self._build_retry_prompt(prompt, errors, response)
time.sleep(1 * (attempt + 1))
return None
def _build_prompt(self, description):
return f"""
Create a job posting for: {description}
Output must be JSON with:
- title (string): Job title
- salary (integer): Annual salary in USD
- skills (array of strings): Required skills
- is_remote (boolean): Whether remote work is possible
Example:
{{
"title": "Software Engineer",
"salary": 100000,
"skills": ["Python", "SQL"],
"is_remote": true
}}
"""
def _extract_json(self, text):
try:
return json.loads(text)
except Exception:
pass
match = re.search(r'```(?:json)?\s*([\s\S]*?)\s*```', text)
if match:
try:
return json.loads(match.group(1))
except Exception:
pass
match = re.search(r'\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}', text)
if match:
try:
return json.loads(match.group(0))
except Exception:
pass
return None
def _validate_job_data(self, data):
errors = {}
if 'title' not in data:
errors['title'] = 'Missing title'
elif not isinstance(data['title'], str):
errors['title'] = 'Title must be string'
if 'salary' not in data:
errors['salary'] = 'Missing salary'
elif not isinstance(data['salary'], (int, float)):
errors['salary'] = 'Salary must be number'
elif data['salary'] <= 0:
errors['salary'] = 'Salary must be positive'
if 'skills' not in data:
errors['skills'] = 'Missing skills'
elif not isinstance(data['skills'], list):
errors['skills'] = 'Skills must be array'
if 'is_remote' not in data:
errors['is_remote'] = 'Missing is_remote'
elif not isinstance(data['is_remote'], bool):
errors['is_remote'] = 'is_remote must be boolean'
return errors
def _build_retry_prompt(self, original_prompt, errors, bad_response):
error_text = "\n".join([f"- {k}: {v}" for k, v in errors.items()])
return f"""
{original_prompt}
Your previous response had these errors:
{error_text}
Bad response: {bad_response}
Please fix these errors and provide valid JSON.
"""
def _call_llm(self, prompt):
# your real API call here
...
After — just the schema:
from pydantic import BaseModel, Field
from llm_output_guard.integrations.openai import GuardedOpenAI
class JobPost(BaseModel):
title: str = Field(description="Job title")
salary: int = Field(description="Annual salary in USD", gt=0)
skills: list[str] = Field(description="Required skills")
is_remote: bool = Field(description="Whether remote work is possible")
# That's ALL you write. Everything else is handled automatically:
# JSON extraction · schema validation · retry with error feedback · prompt engineering
guard = GuardedOpenAI(schema=JobPost, model="gpt-4o-mini", max_retries=3)
result = guard.guard("Create a software engineer job posting.")
if result.success:
job: JobPost = result.data # fully typed Pydantic instance
print(f"{job.title} — ${job.salary:,} — remote: {job.is_remote}")
Installation
# Core (no dependencies)
pip install llm-output-guard
# With Pydantic v2
pip install "llm-output-guard[pydantic]"
# With JSON Schema validation
pip install "llm-output-guard[jsonschema]"
# With OpenAI integration
pip install "llm-output-guard[openai]"
# With LangChain integration
pip install "llm-output-guard[langchain]"
# Everything
pip install "llm-output-guard[all]"
Quick Start
JSON Schema
from llm_output_guard import Validator
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
},
"required": ["name", "age"],
}
def my_llm(prompt: str) -> str:
return '{"name": "Alice", "age": 30}'
validator = Validator(schema=schema, llm_callable=my_llm, max_retries=2)
result = validator.guard("Who is Alice?")
print(result.success) # True
print(result.data) # {'name': 'Alice', 'age': 30}
print(result.attempts) # 1
Pydantic Model
from pydantic import BaseModel
from llm_output_guard import Validator
class Person(BaseModel):
name: str
age: int
validator = Validator(schema=Person, llm_callable=my_llm)
result = validator.guard("Describe Alice.")
person: Person = result.data # fully typed Pydantic instance
print(person.name) # Alice
Validate an Existing String
result = Validator(schema=Person).validate_output('{"name": "Bob", "age": 25}')
print(result.success) # True
Raise on Failure
from llm_output_guard.core.exceptions import MaxRetriesExceededError
validator = Validator(schema=schema, llm_callable=my_llm, raise_on_failure=True)
try:
result = validator.guard("…")
except MaxRetriesExceededError as e:
print(f"Failed after {e.attempts} attempts.")
Integrations
OpenAI
from llm_output_guard.integrations.openai import GuardedOpenAI
guard = GuardedOpenAI(schema=Person, model="gpt-4o-mini", max_retries=3)
result = guard.guard("Tell me about Alice.")
LangChain
from langchain_openai import ChatOpenAI
from llm_output_guard.integrations.langchain import GuardedLLM
guarded = GuardedLLM(llm=ChatOpenAI(), schema=Person, max_retries=3)
result = guarded.invoke("Tell me about Alice.")
FastAPI
from fastapi import FastAPI
from llm_output_guard import Validator
app = FastAPI()
validator = Validator(schema=Person, llm_callable=my_llm)
@app.post("/person")
async def get_person(prompt: str):
result = validator.guard(prompt)
result.raise_for_status()
return result.data
CLI
# Validate a JSON file against a JSON Schema file
llm-guard validate output.json schema.json
# Print the JSON Schema of a Pydantic model
llm-guard schema mypackage.models.Person
GuardResult
Every .guard() or .validate_output() call returns a GuardResult:
| Attribute | Type | Description |
|---|---|---|
success |
bool |
True when validation passed |
data |
Any |
Validated output (Pydantic instance or dict) |
raw_output |
str |
Raw string from the LLM |
errors |
list[dict] |
Validation error dicts (empty on success) |
attempts |
int |
Number of LLM calls made |
schema_type |
str |
"pydantic" / "json_schema" / "dict" |
Retry Strategies
| Strategy | Description |
|---|---|
fixed |
Constant delay between retries |
exponential |
Exponential back-off with optional jitter (default) |
linear |
Linearly increasing delay |
Validator(schema=schema, llm_callable=llm, retry_strategy="exponential", retry_delay=1.0)
Development
git clone https://github.com/Ujjwal-Bajpayee/llm-output-guard
cd llm-output-guard
pip install -e ".[dev]"
pytest --cov=llm_output_guard
Contributing
See CONTRIBUTING.md.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_output_guard-0.1.0.tar.gz.
File metadata
- Download URL: llm_output_guard-0.1.0.tar.gz
- Upload date:
- Size: 26.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98f4a10d5674a8a327ba3b7a930df1aa3e63a2616615dc318c51e78682948d97
|
|
| MD5 |
850f0b2e1cbe2cbdc9dd10f049e4acd2
|
|
| BLAKE2b-256 |
da275a3ee6e39016d6a0ffcbf28d0ab13b7860d08120ab701bcfcafbecea7327
|
File details
Details for the file llm_output_guard-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llm_output_guard-0.1.0-py3-none-any.whl
- Upload date:
- Size: 27.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42d3b52adbbada76de1cc54f23933536c27f8b333a900f49800a829ed923e2b7
|
|
| MD5 |
74dc9297cadd43fa7dad314ff2d4468a
|
|
| BLAKE2b-256 |
2cb06c35faa145d4d964e5d78e97837723c5df8f56d52e1a3b0eb2eb25f08ed1
|