Files
myWiki/concepts/structured-output.md

1.5 KiB
Raw Blame History

title, created, updated, type, tags, sources
title created updated type tags sources
结构化输出 (Structured Output) 2026-06-10 2026-06-10 concept
llm
structured-output
pydantic
json-schema
raw/articles/pydantic-three-piece-suite-2026.md

结构化输出 (Structured Output)

让 LLM 生成符合预定义 schema 的 JSON/对象,而非自由格式文本。是从"自然语言回复"到"可编程 API"的关键桥梁。

核心挑战

LLM 的输出是概率性的——同样的 prompt 跑 100 次,结构会漂移字段名变化、多了字段、类型错误、None 值)。

解决方案层次

方案 适用场景 代表工具
单次校验 简单结构化输出 Instructor, Pydantic model_validate
可观测校验 需要监控漂移趋势 [[logfire
类型约束 Agent 多步推理 + tool 调用 [[pydantic-ai

从"事后校验"到"事前约束"

传统LLM 输出 → model_validate → 错了 → 重试

pydantic-aiAgent 定义时类型已写入 tool schema → LLM 按 schema 输出 → 自动校验 → 出错框架层重试

三配置升级(零成本)

model_config = {
    "strict": True,    # 空字符串不会变 0
    "extra": "forbid", # LLM 多塞字段立刻报
    "frozen": True,    # 模块间传递防篡改
}

参考