# Practices for Embedding AI Agents in Software
# Verified Structured Output
🎯 The Hook
Feeding raw LLM output into downstream systems? Broken JSON, invalid values, missing fields -- things fail silently until they become production incidents. Two-layer verification stops this.
🔥 The Problem
LLM output is inherently ambiguous natural language. When downstream code expects strict types and fields, parse failures, type errors, and invalid values silently corrupt processing. Even when the format is correct, business rule violations like future dates in the past or nonexistent product IDs slip through syntax checks undetected.
💡 The Pattern
Enforce structure in two layers. Layer 1 (schema validation) uses response_format or function_calling to constrain output to a JSON Schema. Layer 2 (semantic validation) applies deterministic code to verify business rules -- date ranges, ID existence, permission checks. On failure, error details are fed back into context for self-correction retry (1-3 attempts). If retries exhaust the budget, fail-fast explicitly rather than passing bad data downstream. Temperature is set to 0.0-0.3 for high schema compliance.
✅ When to Use
Use when:
- LLM output is consumed by downstream code, APIs, or databases
- A target schema (types, required fields, value ranges) can be defined
- Invalid output causes real business impact in downstream processing
Don't use when:
- Output is free-form text (summaries, conversations) with no structural requirements
- The output schema changes so frequently that maintenance cost exceeds benefit
- Invalid output can simply be discarded with no consequence
⚠️ Pitfalls
- Appending full error messages on each retry inflates token consumption. Pass only the latest error or a summary
- Schemas with too many fields (roughly 15+) reduce LLM compliance rates. Consider splitting into multiple calls
- response_format is provider-specific. Manage schema definitions in a portable format like Pydantic or JSON Schema
🔧 Implementation Approach
- Layer 1 (schema validation) uses response_format or function_calling to enforce JSON Schema compliance, with Pydantic models for type and value range constraints
- Layer 2 (semantic validation) applies deterministic code to verify domain constraints such as date ranges, ID existence, and permissions
- On validation failure, feed error details back into context for self-correction retry, with a budget of 1-3 attempts before fail-fast
- Set LLM temperature to 0.0-0.3 to maximize structured output compliance rates
- Manage schema definitions in a provider-agnostic format (Pydantic, JSON Schema) to ensure portability across LLM providers
#
AIAgents# #
SoftwareArchitecture#