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

Google ADK构建Agent时Pydantic验证错误排查求助

解决Google ADK中LlmAgent的ValidationError问题

错误原因分析

从报错信息可明确两个核心问题:

  1. output_schema参数类型错误:output_schema要求传入Pydantic BaseModel的子类,你传入的是JSON字符串(final_output_json),不符合类型要求。
  2. 参数误用与名称错误:
    • 错误地将实际输入数据(JSON字符串)传给了input_schema,该参数用于定义输入结构(需BaseModel子类),实际数据应通过input参数传入。
    • instructions不是LlmAgent的合法参数,导致"Extra inputs are not permitted"错误,正确参数名应为prompt(符合Google ADK的API规范)。

解决方案步骤

  1. 定义Pydantic模型:为输入数据和输出结果分别创建对应的BaseModel子类,明确数据结构。
  2. 修正参数传递:
    • 将实际输入数据传给input参数,而非input_schema。
    • 用prompt替代instructions作为提示词参数。
    • 给output_schema传入定义好的Pydantic模型类,而非JSON字符串。
  3. 避免不必要的JSON序列化:加载输入JSON后直接使用字典或转换为Pydantic实例,无需用json.dumps转为字符串。

修正后代码示例

from pydantic import BaseModel, Field
from typing import List
import json

# 定义输入数据模型,匹配precedence_mitigation结构
class MitigationItem(BaseModel):
    States: str = Field(description="涉及的州")
    Counties: str = Field(description="涉及的县")
    Category: str = Field(description="风险类别")
    Risks: str = Field(description="风险描述")
    Mitigation: str = Field(description="缓解措施")
    Precedence: str = Field(description="参考法规/先例")

class MitigationInput(BaseModel):
    items: List[MitigationItem] = Field(description="风险缓解列表")

# 定义输出模型,匹配final_output_json结构
class AgentOutput(BaseModel):
    passing: bool = Field(description="是否通过检查")
    comments: str = Field(description="检查意见")
    # 可根据实际需求补充其他字段

# 提示词定义
QAAgentPrompt = "Checks the information for accuracy, completeness, and consistency."
ComplianceAgentPrompt = "Reviews the information for adherence to relevant policies, standards, and regulations. Please don't repeat anything that's already mentioned by the QAAgent, i.e. <<{QAAgentOutput}>>"
RecommendationAgentPrompt = "Reviews the document for recommendations."

def json_to_memory(file_path):
    """读取JSON文件并返回字典"""
    with open(file_path, 'r', encoding='utf-8') as file:
        json_data = json.load(file)
    return json_data

# 加载输入数据并转换为Pydantic实例
mitigation_raw_data = json_to_memory(FilePath)
mitigation_input = MitigationInput(items=mitigation_raw_data)

# 创建修正后的Agent实例
QAAgent = LlmAgent(
    name="QAAgent",
    model="gemini-2.0-flash",
    prompt=QAAgentPrompt,
    input=mitigation_input,
    output_schema=AgentOutput,
    output_key="QAAgentOutput"
)

ComplianceAgent = LlmAgent(
    name="ComplianceAgent",
    model="gemini-2.0-flash",
    prompt=ComplianceAgentPrompt,
    input=mitigation_input,
    output_schema=AgentOutput,
    output_key="ComplianceAgentOutput"
)

RecommendationAgent = LlmAgent(
    name="RecommendationAgent",
    model="gemini-2.0-flash",
    prompt=RecommendationAgentPrompt,
    input=mitigation_input,
    output_schema=AgentOutput,
    output_key="RecommendationAgentOutput"
)

root_agent = SequentialAgent(
    name="RootAgent",
    model="gemini-2.0-flash",
    subagents=[QAAgent, ComplianceAgent, RecommendationAgent]
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 21:05:14