agentleFS
DevelopersGlobalDevelopersGlobal/ai-agent-skills/skills/ai-output-validation/SKILL.md

ai-output-validation

Skill216 starsChanged 6 months agoMITView on GitHub

Validates, parses, and sanitizes AI-generated outputs before they reach end users or downstream systems. Structured output enforcement, schema validation, and fallback handling.

We split this skill into the rules it gives an agent

12 rules. About 0 are common, 12 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈12

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When AI output feeds into another system, database, or agent

    line 16
  2. Distinctive:

    When building agentic systems that make decisions based on AI output

    line 17
  3. Distinctive:

    When AI generates code, SQL, JSON, or other structured formats

    line 18
  4. Distinctive:

    Use JSON Schema or Pydantic/Zod models to formalize the expected output.

    line 25
  5. Distinctive:

    Use models/APIs that support structured output natively where available (OpenAI structured outputs, Gemini JSON mode, Anthropic tool use).

    line 46
  6. Distinctive:

    After 2 failures: return a structured error, not a crash

    line 63
  7. Distinctive:

    Verify: All AI outputs pass schema validation before use.

    line 69
  8. Distinctive:

    If AI output will be rendered as HTML: sanitize against XSS.

    line 73
  9. Distinctive:

    If AI output will be executed as code: sandbox it and review before execution.

    line 74
  10. Distinctive:

    If AI output will be stored in a database: sanitize against injection.

    line 75
  11. Distinctive:

    Never trust AI output the way you'd trust your own code — it's user-generated content.

    line 76
  12. Distinctive:

    No logging of validation failures

    line 102

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Overview

AI models produce unstructured text by default. In production pipelines, unstructured outputs cause brittle parsing, unexpected behavior, and silent failures. This skill enforces structured output generation and validation at every AI system boundary.

When to Use

  • Any AI pipeline where output is used programmatically (not just displayed to a user)
  • When AI output feeds into another system, database, or agent
  • When building agentic systems that make decisions based on AI output
  • When AI generates code, SQL, JSON, or other structured formats

Process

Step 1: Define Output Schema Before Prompting

  1. Define the exact structure you need BEFORE writing the prompt.
  2. Use JSON Schema or Pydantic/Zod models to formalize the expected output.
  3. Example schema:
    {
      "type": "object",
      "required": ["summary", "action", "confidence"],
      "properties": {
        "summary": {"type": "string", "maxLength": 200},
        "action": {"type": "string", "enum": ["approve", "reject", "review"]},
        "confidence": {"type": "number", "minimum": 0, "maximum": 1}
      }
    }
  4. Design the schema to be minimal — only what you actually need.

Verify: Schema is defined and versioned before any prompt is written.

Step 2: Prompt for Structured Output

  1. Explicitly instruct the model to output in your defined format.
  2. Include the schema or an example in the prompt.
  3. Use models/APIs that support structured output natively where available (OpenAI structured outputs, Gemini JSON mode, Anthropic tool use).
  4. Prompt pattern:
    Respond ONLY with valid JSON matching this schema:
    {schema}
    
    Do not include explanation or markdown. Output raw JSON only.

Verify: Prompt explicitly requests structured output with schema reference.

Step 3: Validate and Parse Output

  1. Parse the output against your schema — never use raw AI output directly.
  2. If parsing fails:
    • Log the raw output and the parse error
    • Retry with a clarification prompt (max 2 retries)
    • After 2 failures: return a structured error, not a crash
  3. Validate semantic constraints beyond the schema:
    • Is the confidence score consistent with the action?
    • Are referenced IDs in the database?
    • Are dates in the valid range?

Verify: All AI outputs pass schema validation before use. Failed validations are logged.

Step 4: Sanitize for Downstream Use

  1. If AI output will be rendered as HTML: sanitize against XSS.
  2. If AI output will be executed as code: sandbox it and review before execution.
  3. If AI output will be stored in a database: sanitize against injection.
  4. Never trust AI output the way you'd trust your own code — it's user-generated content.

Verify: AI output is sanitized appropriate to its destination.

Step 5: Monitor Output Quality

  1. Log the schema validation pass/fail rate.
  2. Sample and review AI outputs regularly for semantic correctness.
  3. Alert on high validation failure rates (>5%).

Verify: Validation metrics are tracked. Alert configured.

Common Rationalizations (and Rebuttals)

ExcuseRebuttal
"The model outputs valid JSON 99% of the time"That 1% causes production incidents. Always validate.
"We display it to users, not parse it"Users act on AI output. Wrong output drives wrong actions.
"Structured output adds latency"Validation is microseconds. Debugging unvalidated output is hours.
"The model is deterministic enough"No LLM is deterministic enough to skip validation.

Red Flags

  • AI output used directly without schema validation
  • JSON.parse() without try/catch around AI output
  • AI-generated SQL or code executed without review
  • No logging of validation failures
  • AI output rendered as HTML without sanitization

Verification

  • Output schema defined and versioned
  • Prompt explicitly requests structured output
  • Schema validation on every AI response
  • Retry logic for validation failures (max 2 retries)
  • Semantic validation beyond schema (constraint checks)
  • Sanitization applied appropriate to output destination
  • Validation failure rate monitored

References

  • hallucination-prevention skill
  • prompt-injection-defense skill
  • security-hardening skill

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.