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

Agno Agentic Framework中自定义工具返回非字符串类型触发Pydantic ValidationError问题求助

Agno Agentic Framework中自定义工具返回非字符串类型触发Pydantic ValidationError问题求助

我最近在试用Agno这个新的智能体框架,想写个简单的计算器来测试它的功能。下面是我写的代码:

#main.py
def add(x: int, y: int) -> int:
    """
    Use this function to add two numbers.
    Args:
        a (int): The first number.
        b (int): The second number.
    Returns:
        int: The sum of the two numbers.
    """
    print("Adding two numbers...")
    return x + y

def subtract(x: int, y: int) -> int:
    """
    Use this function to subtract two numbers.
    Args:
        a (int): The first number.
        b (int): The second number.
    Returns:
        int: The difference of the two numbers.
    """
    print("Subtracting two numbers...")
    return x - y

def multiply(x: int, y: int) -> int:
    """
    Use this function to multiply two numbers.
    Args:
        a (int): The first number.
        b (int): The second number.
    Returns:
        int: The product of the two numbers.
    """
    print("Multiplying two numbers...")
    return x * y

def generate_random_number() -> int:
    """
    Use this function to generate a random number.
    Returns:
        int: A random number.
    """
    print("Generating a random number...")
    return 42

def divide(x: int, y: int) -> float:
    """
    Use this function to divide two numbers.
    Args:
        a (int): The first number.
        b (int): The second number.
    Returns:
        float: The quotient of the two numbers.
    """
    print("Dividing two numbers...")
    return x / y

calculator_agent = Agent(
    model=OpenRouter(id="google/gemini-2.0-flash-lite-preview-02-05:free"),
    name="Data Agent",
    description=dedent("""
    You are a helpful calulator agent. You can perform basic arithmetic operations like addition, subtraction, multiplication, and division with the help of the tools provided. 
    """),
    instructions=[
        """Analyse the user query and use the tools to perform the required operation."""
    ],
    tools=[add, subtract, multiply, divide, generate_random_number],
    show_tool_calls=True,
    debug_mode=True,
    add_history_to_messages=True,
    markdown=True,
)

response = calculator_agent.run("Add 2 to 5. Take the result and multiply it by 2. Take that output and divide it by a random number")
print(response.content)

运行之后我遇到了下面这个错误:

pydantic_core._pydantic_core.ValidationError: 2 validation errors for Message
content.list[any]
  Input should be a valid list [type=list_type, input_value=7, input_type=int]
    For further information visit https://errors.pydantic.dev/2.11/v/list_type
content.str
  Input should be a valid string [type=string_type, input_value=7, input_type=int]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type

我完全没要求第一步加法的结果是列表或者字符串类型啊,实在搞不懂哪里出问题了。是不是Agno框架要求所有自定义工具都只能返回字符串类型?我只在Agno的工具文档里看到过一个示例,那个示例里的工具确实返回的是字符串类型。

有没有大佬能帮我看看这是怎么回事?

备注:内容来源于stack exchange,提问作者Ashwin Prasad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 20:09:30