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

Agno框架多智能体工具调用报422模板错误及返回值咨询

Agno(原Phidata)自定义工具返回格式问题解决方案

核心结论

Agno框架的自定义工具必须返回字符串类型,或能被框架隐式转换为字符串的内容。非字符串返回值(如原始字典、未序列化的JSON对象、API响应实例)会导致模板解析失败,触发422错误。

正确返回方式示例

1. 提取关键信息返回可读字符串

如果只需要返回核心结果,直接拼接成自然语言字符串:

def create_community_tool(input_data):
    # 调用GraphQL接口获取响应
    response = graphql_client.execute(create_community_query, variables=input_data)
    # 提取关键字段,返回格式化字符串
    community_data = response["data"]["createCommunity"]
    return f"社区创建完成:ID={community_data['id']},名称={community_data['name']},状态={community_data['status']}"

2. 返回完整序列化后的响应字符串

如果需要返回完整响应结构,用json.dumps()将JSON对象转为格式化字符串:

import json

def create_community_tool(input_data):
    response = graphql_client.execute(create_community_query, variables=input_data)
    # 序列化响应为格式化字符串
    return json.dumps(response, indent=2, ensure_ascii=False)

错误返回方式排查

你之前尝试的几种方式中,以下情况会触发422错误:

  • 直接返回字典{status: success, content: response}:框架无法将字典解析为模板可接受的文本
  • 直接返回GraphQL原始响应对象:若响应是类实例,其默认字符串表示可能不符合模板要求
  • 仅用str(response):如果response是复杂对象,str()的输出可能是不可读的类描述,而非有效内容

额外验证步骤

  1. 单独调用工具函数,打印返回值,确认是纯文本/格式化字符串
  2. 检查智能体配置中的return_direct参数:若设为True,工具返回值会直接作为最终响应,同样要求是字符串

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 22:32:08