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

部署ADK Python Agent至Google Agent Engine时遇Pydantic警告的解决咨询

解决ADK Python Agent部署时的Pydantic Schema生成警告

问题描述

部署Google ADK的auto-insurance-agent示例到Agent Engine时,终端出现如下警告:

WARNING:vertexai.agent_engines:failed to generate schema for async_add_session_to_memory: async_add_session_to_memory is not fully defined; you should define Session, then call async_add_session_to_memory.model_rebuild().

For further information visit https://errors.pydantic.dev/2.11/u/class-not-fully-defined

原因分析

这是Pydantic v2的类型注解前向引用问题:async_add_session_to_memory函数的参数使用了Session类,但该类要么未在函数之前定义,要么存在循环引用导致Pydantic无法自动解析类型,进而无法生成对应的schema。

解决方案

步骤1:定位相关代码文件

找到项目中定义async_add_session_to_memory函数的文件(通常是memory.py或核心agent模块文件)。

步骤2:调整代码定义顺序或添加model_rebuild调用

根据Session类的定义位置,选择以下两种方式之一:

  • 方式一:调整类定义顺序
    如果Session类和async_add_session_to_memory函数在同一个文件中,将Session类的定义移到函数之前:

    from pydantic import BaseModel
    
    # 先定义Session类
    class Session(BaseModel):
        session_id: str
        user_input: str
        agent_response: str
        timestamp: float
    
    # 再定义async_add_session_to_memory函数
    async def async_add_session_to_memory(session: Session) -> None:
        # 原函数逻辑,比如写入内存或数据库
        pass
    
  • 方式二:调用model_rebuild处理前向引用
    如果Session类是跨文件引用或存在循环引用,在函数定义完成后添加model_rebuild()调用:

    from pydantic import BaseModel
    from typing import TYPE_CHECKING
    
    if TYPE_CHECKING:
        from .models import Session  # 仅类型检查时导入
    
    async def async_add_session_to_memory(session: "Session") -> None:
        # 原函数逻辑
        pass
    
    # 调用model_rebuild解析类型并生成schema
    async_add_session_to_memory.model_rebuild()
    

步骤3:重新部署验证

修改完成后,重新执行部署命令,确认警告信息不再出现。

补充说明

该警告通常不会立即影响agent的运行,但修复后可以避免后续schema相关的潜在问题,同时符合Pydantic v2的最佳实践。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 10:42:36