command
Version:
v1.0.0
Opens a new window with list of versions in this module.
Published: Feb 23, 2026
License: MIT
Opens a new window with license information.
Imports: 4
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
结构化输出 (Structured Output)
展示 JSON Schema 生成、验证和手动构建,用于约束 LLM 输出格式。
功能
- Schema 生成:从 Go 结构体自动生成 JSON Schema(支持
jsonschema tag)
- Schema 验证:验证 JSON 数据是否符合 Schema(必填字段、枚举、范围等)
- 手动构建:使用 Builder API 编程式构建复杂 Schema(嵌套对象、数组)
前置条件
运行
cd examples/15_structured_output
go run main.go
代码说明
structured.NewSchemaGenerator 通过反射从 Go 类型生成 Schema;structured.NewValidator 验证 JSON 数据;structured.NewObjectSchema 等 Builder 方法支持链式构建 Schema。
Documentation
¶
示例 15_structured_output 演示了 AgentFlow 的结构化输出(Structured Output)能力。
演示内容 ¶
本示例展示 JSON Schema 生成、校验与手动构建的完整流程:
- Schema Generation:通过反射从 Go struct 自动生成 JSON Schema,
支持 required、enum、format、minLength、pattern 等约束标签
- Schema Validation:对 JSON 数据进行 Schema 校验,
返回带路径的详细错误信息(ValidationErrors)
- Manual Schema Building:使用 Fluent API 手动构建嵌套 Schema,
支持 Object、Array、String、Integer、Boolean、Number 等类型组合
运行方式 ¶
go run .
Source Files
¶
Click to show internal directories.
Click to hide internal directories.