AGENT SKILL
structured-output-json-schema
大模型严格结构化输出 JSON Schema 约束设计与 Function Calling 容错修复机制。
- License 许可
- MIT
- 最近更新
- 2026-08-25
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- structured-output-json-schema
- description
- 大模型严格结构化输出 JSON Schema 约束设计与 Function Calling 容错修复机制。
02 资源文件 1
- 参考文档skill.json1.0 KB
03 SKILL.md 正文
大模型严格结构化 JSON 输出设计准则
1. Zod / Pydantic 严格 Schema 约束
定义 Schema 时,必须设置 additionalProperties: false,并将所有字段设为 required:
import { z } from 'zod';
export const DecisionResultSchema = z.object({
status: z.enum(['APPROVED', 'REJECTED', 'NEEDS_REVIEW']),
confidenceScore: z.number().min(0).max(1),
reasoning: z.string().min(10).describe('详细决策依据,不少于 10 字'),
suggestedActions: z.array(z.string()).describe('推荐执行动作清单'),
}).strict();
export type DecisionResult = z.infer<typeof DecisionResultSchema>;
2. 容错与 JSON 修复流
大模型偶尔会输出包含 Markdown 代码块 (json ... ) 或尾随逗号。必须在反序列化层增加清洗容错:
export function safeParseJson<T>(rawText: string, schema: z.ZodType<T>): T {
let cleaned = rawText.trim();
// 去除 markdown 标记
if (cleaned.startsWith('```json')) cleaned = cleaned.replace(/^```json\s*/, '');
if (cleaned.startsWith('```')) cleaned = cleaned.replace(/^```\s*/, '');
if (cleaned.endsWith('```')) cleaned = cleaned.replace(/\s*```$/, '');
const parsed = JSON.parse(cleaned);
return schema.parse(parsed);
}
04 同仓库更多 Skills
web-page-extractor
网页正文智能提纯与清洗工具。抓取网页并剔除 HTML 标签、脚本与样式,输出高信息密度的 Markdown。
★ 1
restful-api-standard
企业级 RESTful API 设计准则
★ 1
fetch-page
抓取网页并提取标题与正文长度,需要网络权限(默认策略为拒绝,须授权)。
★ 1
api-tester-toolkit
API 接口网络测试与耗时诊断工具包。发起 HTTP GET/POST 请求、统计响应耗时与状态码、检查响应数据。
★ 1
semver-bump
语义化版本号升级:major / minor / patch。
★ 1
translate
中英互译演示实现,内置小词典,用于演示翻译类 skill 的检索与调用。
★ 1