You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用自定义Pydantic类实现LangChain结构化输出时遇验证错误

LangChain结构化输出ValidationError问题解决

错误含义

ValidationError: 1 validation error for AllSections sections field required 表示LLM返回的输出不符合AllSections类的结构要求——Pydantic验证时发现缺少必填的sections字段。

当你用单个HeaderSection作为结构化输出类时,LLM返回单个符合该类的对象即可通过验证;但切换到包含HeaderSection列表的AllSections类后,LLM需要返回一个包含sections键(值为HeaderSection对象列表)的结构,而当前prompt的示例输出是单个HeaderSection实例,没有按照AllSections的结构返回,导致验证失败。

解决方法

1. 修正系统提示的示例输出

将示例输出改为符合AllSections结构的格式,明确告知LLM需要返回包含sections列表的结果:

修改后的system提示:

You are tasked with splitting up a document into sections. These sections all have headers, which is how you will determine where to split all of the data.
Return each section as a HeaderSection object, and wrap all HeaderSection objects in a list under the "sections" field of the AllSections class.
You will be given the input text, and the headers.
Here's an example on how the data will be stored.

EXAMPLE TEXT:
# Bill of Lading and Driver Signature

Item 6

The signature of a Carrier Freight Driver/Sales Representative on any Bill of Lading other than a Carrier’s Bill of Lading will act only to acknowledge the receipt of freight as described on the document. This signature will not acknowledge agreement to any terms and conditions of carriage and/or liability conditions that may also appear on the document. Unless there is a written agreement, separate from the Bill of Lading, signed by shipper and Carrier, then the Carrier Freight Bill of Lading Terms and Conditions will apply.

EXAMPLE OUTPUT:
{"sections": [{"header": "Bill of Lading and Driver Signature Item 6", "text": "The signature of a Carrier Freight Driver/Sales Representative on any Bill of Lading other than a Carrier’s Bill of Lading will act only to acknowledge the receipt of freight as described on the document. This signature will not acknowledge agreement to any terms and conditions of carriage and/or liability conditions that may also appear on the document. Unless there is a written agreement, separate from the Bill of Lading, signed by shipper and Carrier, then the Carrier Freight Bill of Lading Terms and Conditions will apply."}]}

NOTE: The item number may not always be present.

2. 明确提示LLM返回结构

在prompt中进一步强调必须返回包含sections字段的结构,避免LLM误解输出要求。

3. 验证结构化输出配置

确认with_structured_output的使用正确,对于Anthropic模型,LangChain的with_structured_output会自动生成格式要求,若仍有问题,可尝试显式指定响应格式:

structured_json_output = llm.with_structured_output(AllSections, method="json_mode")

修改后的完整代码片段

from langchain.pydantic_v1 import BaseModel, Field
from typing import List

class HeaderSection(BaseModel):
    """Class to save a section header and text from the section"""
    header: str = Field(description="Header of a section from the document.")
    text: str = Field(description="Text under the associated header.")

class AllSections(BaseModel):   
    sections: List[HeaderSection]

from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate

llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
structured_json_output = llm.with_structured_output(AllSections)

system = """You are tasked with splitting up a document into sections. These sections all have headers, which is how you will determine where to split all of the data.
Return each section as a HeaderSection object, and wrap all HeaderSection objects in a list under the "sections" field of the AllSections class.
You will be given the input text, and the headers.
Here's an example on how the data will be stored.

EXAMPLE TEXT:
# Bill of Lading and Driver Signature

Item 6

The signature of a Carrier Freight Driver/Sales Representative on any Bill of Lading other than a Carrier’s Bill of Lading will act only to acknowledge the receipt of freight as described on the document. This signature will not acknowledge agreement to any terms and conditions of carriage and/or liability conditions that may also appear on the document. Unless there is a written agreement, separate from the Bill of Lading, signed by shipper and Carrier, then the Carrier Freight Bill of Lading Terms and Conditions will apply.

EXAMPLE OUTPUT:
{"sections": [{"header": "Bill of Lading and Driver Signature Item 6", "text": "The signature of a Carrier Freight Driver/Sales Representative on any Bill of Lading other than a Carrier’s Bill of Lading will act only to acknowledge the receipt of freight as described on the document. This signature will not acknowledge agreement to any terms and conditions of carriage and/or liability conditions that may also appear on the document. Unless there is a written agreement, separate from the Bill of Lading, signed by shipper and Carrier, then the Carrier Freight Bill of Lading Terms and Conditions will apply."}]}

NOTE: The item number may not always be present.
"""

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system),
        ("human", "Headers: {headers}\n\nText: {text}")
    ]
)

chunking_chain = prompt | structured_json_output
output = chunking_chain.invoke({"headers": llama_parse_headers_and_items, "text": llama_data_md[0].text})

内容的提问来源于stack exchange,提问作者Alex McGraw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 11:00:13