Langgraph踩坑记录

1. 当你用到返回格式需要是json_object时

planner = llm.with_structured_output(Sections)
只要你开了 response_format: json_object你的对话消息里 必须出现 "json" 这个单词(大小写都行)不然直接报错 400!

你现在的 System Prompt 是:

ini 复制代码
SystemMessage(content="Generate a plan for the report.")

里面完全没有 json → 所以报错!


怎么修?

只需要在你的 SystemMessage 里加一个 json 单词就行!

修复后代码(只改一行)

less 复制代码
report_sections = planner.invoke(
    [
        # 👇 就加个 "JSON" 在里面,立刻不报错
        SystemMessage(content="Generate a plan for the report in valid JSON."),
        HumanMessage(content=f"Here is the report topic: {state['topic']}"),
    ]
)

或者中文版本:

ini 复制代码
SystemMessage(content="生成报告计划,**必须输出 JSON 格式**。")

只要出现 json/JSON 这个词,错误立刻消失!


为什么 LangGraph 官方示例不报错?

因为官方用的是 OpenAI没有这个奇葩限制

Qwen / 阿里通义 自己加的规则:

  • json_object必须提示词里带 json
  • 不然直接拦死

你现在完整正确、不会报错的代码

python 复制代码
def orchestrator(state: State):
    """Orchestrator that generates a plan for the report"""

    report_sections = planner.invoke(
        [
            # ✅ 修复:加了 JSON 单词
            SystemMessage(content="Generate a plan for the report. Output response in JSON format."),
            HumanMessage(content=f"Here is the report topic: {state['topic']}"),
        ]
    )

    return {"sections": report_sections.sections}

2.pydantic 字段类型校验未通过

发生异常: ValidationError 7 validation errors for Sections sections.0.name Field required type=missing, input_value={'section_number': 1, 'ti...ectives of the Report'}, input_type=dict] For further information visit errors.pydantic.dev/2.13/v/miss... sections.1.name Field required type=missing, input_value={'section_number': 2, 'ti...al Training Paradigms'}, input_type=dict] For further information visit errors.pydantic.dev/2.13/v/miss... sections.2.name Field required type=missing, input_value={'section_number': 3, 'ti...wer-Law Relationships'}, input_type=dict] For further information visit errors.pydantic.dev/2.13/v/miss... sections.3.name Field required type=missing, input_value={'section_number': 4, 'ti...e Resource Allocation'}, input_type=dict] For further information visit errors.pydantic.dev/2.13/v/miss...

swift 复制代码
sections.0.name  Field required

意思是:你的 Sections 模型要求每个章节必须有 name 字段,但 Qwen 输出的是 title /section_number,没有 name!


1. 先看你现在的 Schema(错误根源)

python 复制代码
class Section(BaseModel):
    name: str  # 👈 你要求必须有 name
    section_number: int

class Sections(BaseModel):
    sections: list[Section]

Qwen 输出的是:

bash 复制代码
{
  "section_number": 1,
  "title": "Introduction"  # 👈 它输出 title,不是 name!
}

→ 字段对不上 → ValidationError


2. 一秒修复(二选一,都能立刻好)

方案 A:把 Schema 改成 title(推荐,最稳)

python 复制代码
class Section(BaseModel):
    title: str                # 👈 改成 title
    section_number: int

class Sections(BaseModel):
    sections: list[Section]

方案 B:让模型输出 name(改 Prompt)

ini 复制代码
SystemMessage(content="""
Generate a plan for the report. Output response in JSON format.
Each section MUST have a field called "name", not "title"!
""")
相关推荐
艾醒(AiXing-w)7 小时前
LangChain 1.0 入门(三):稳定性双核心——重试机制+速率限速器参数详解与实战
开发语言·langchain·php
未若君雅裁8 小时前
从函数到工具,LangChain Tools 与 Tool Calling 完整入门
langchain
未若君雅裁8 小时前
工具描述决定调用质量,LangChain 工具 Schema 与参数校验
langchain
艾醒(AiXing-w)10 小时前
LangChain 1.0 入门(二):LangChain 全模型标准化接入最佳实践(小白参数详解版)
前端·javascript·langchain
invicinble14 小时前
langchain --rage了解(周末版本)
langchain
qy2016skq14 小时前
OpenClaw 源码解读——入门与破局9 双插件协同:Quota Guard 负责“停“,Model Router 负责“绕“
langchain·prompt·aigc·embedding·ai编程·llama·agi
这就是佬们吗15 小时前
治幻觉,先治检索:RAG 系统防幻觉的完整工程指南
python·langchain·embedding
爱奥尼欧15 小时前
14.输出解析器-Pydantic与JSON
人工智能·学习·langchain·json
qy2016skq15 小时前
OpenClaw 源码解读——入门与破局8 从“报错不切换“到“秒级自动切换“:模型路由插件在 2026.4.14 上的五次迭代实录
langchain·prompt·aigc·embedding·ai编程·ai-native
AI_小站1 天前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain