‹ 首页

ai-output-validation

@developersglobal · 收录于 1 周前

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

适合你,如果需要在AI输出进入生产前做结构化校验

/ 下载安装
ai-output-validation.skill双击,或拖进 Claude 桌面版 / Cowork,即完成安装↓ .skill↓ .zip
用别的 agent?下载 .zip 解压,把文件夹放进它的技能目录
Claude Code~/.claude/skills/(项目级 .claude/skills/)
Codex CLI~/.codex/skills/
Cursor自动读取上面两处目录
其他工具见其文档的「skills」目录;两个下载是同一份文件,只是名字不同
/ 通过 npx 安装 校验哈希
npx oh-my-skill add developersglobal/ai-agent-skills/ai-output-validation
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- developersglobal/ai-agent-skills/ai-output-validation
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify developersglobal/ai-agent-skills/ai-output-validation
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
64GitHub stars
~990上下文体积 · 单文件
镜像托管

怎么用

技能原文 SKILL.md作者撰写 · MIT · f47f948
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: ```json { "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:
  3. Log the raw output and the parse error
  4. Retry with a clarification prompt (max 2 retries)
  5. After 2 failures: return a structured error, not a crash
  6. Validate semantic constraints beyond the schema:
  7. Is the confidence score consistent with the action?
  8. Are referenced IDs in the database?
  9. 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)

| Excuse | Rebuttal | |--------|----------| | "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](../hallucination-prevention/SKILL.md)
  • [prompt-injection-defense skill](../prompt-injection-defense/SKILL.md)
  • [security-hardening skill](../security-hardening/SKILL.md)
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。