15_structured_output

command
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 4 Imported by: 0

README

结构化输出 (Structured Output)

展示 JSON Schema 生成、验证和手动构建,用于约束 LLM 输出格式。

功能

  • Schema 生成:从 Go 结构体自动生成 JSON Schema(支持 jsonschema tag)
  • Schema 验证:验证 JSON 数据是否符合 Schema(必填字段、枚举、范围等)
  • 手动构建:使用 Builder API 编程式构建复杂 Schema(嵌套对象、数组)

前置条件

  • Go 1.24+
  • 无需 API Key

运行

cd examples/15_structured_output
go run main.go

代码说明

structured.NewSchemaGenerator 通过反射从 Go 类型生成 Schema;structured.NewValidator 验证 JSON 数据;structured.NewObjectSchema 等 Builder 方法支持链式构建 Schema。

Documentation

Overview

示例 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 .

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL