Loading Python for AI Engineering...
Building resilient validation pipelines that catch Pydantic ValidationError exceptions, format the exact schema error message into a repair prompt, and re-query the model to achieve 99.9% extraction reliability.
{
"query": "vector index latency",
"confidence": 0.94,
"is_urgent": true
}“A self-correcting loop is a compiler telling a programmer: "Syntax error on line 4: missing comma" instead of silently crashing the whole operating system.”
Retrying the exact same prompt repeatedly without telling the LLM what validation error it produced, resulting in identical failures.
from pydantic import BaseModel, ValidationError
class UserProfile(BaseModel):
age: int
email: str
def parse_with_repair(raw_output: str, model_client, retries=2):
for attempt in range(retries):
try:
return UserProfile.model_validate_json(raw_output)
except ValidationError as e:
repair_prompt = f"Fix this JSON: {raw_output}. Errors: {e.errors()}"
raw_output = model_client(repair_prompt)
raise RuntimeError("Failed to extract valid schema after retries")