admin管理员组文章数量:1355825
I was trying out the new agentic framework agno and wanted to test it out by writing a simple calculator.
#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)
The error I am getting is
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 /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 /2.11/v/string_type
But I am not stating anywhere that the result of the first addition needs to be a list neither for it to be a String.
Can someone please tell me what I am doing wrong ? Should all custom tools only return types of string ?
There was only one example at : at it does seem to have str as the return type
I was trying out the new agentic framework agno and wanted to test it out by writing a simple calculator.
#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)
The error I am getting is
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
But I am not stating anywhere that the result of the first addition needs to be a list neither for it to be a String.
Can someone please tell me what I am doing wrong ? Should all custom tools only return types of string ?
There was only one example at : https://docs.agno/agents/tools at it does seem to have str as the return type
Share Improve this question asked Mar 28 at 19:39 Ashwin PrasadAshwin Prasad 1581 silver badge11 bronze badges1 Answer
Reset to default 0Potential Fix
Try modifying your function return types to always return str
instead of int
or float
.Perhaps the Agno agent framework likely expects all tool outputs to be in str
format to be processed correctly.
Here's the code which I tested multiple times and it works:
import random
from agno.agent import Agent
from agno.models.ollama import Ollama
def add(x: int, y: int) -> str:
"""
Use this function to add two numbers.
Args:
x (int): The first number.
y (int): The second number.
Returns:
str: The sum of the two numbers.
"""
print("Adding two numbers...")
return str(x + y)
def subtract(x: int, y: int) -> str:
"""
Use this function to subtract two numbers.
Args:
x (int): The first number.
y (int): The second number.
Returns:
str: The difference of the two numbers.
"""
print("Subtracting two numbers...")
return str(x - y)
def multiply(x: int, y: int) -> str:
"""
Use this function to multiply two numbers.
Args:
x (int): The first number.
y (int): The second number.
Returns:
str: The product of the two numbers.
"""
print("Multiplying two numbers...")
return str(x * y)
def generate_random_number() -> str:
"""
Use this function to generate a random number.
Returns:
str: A random number.
"""
print("Generating a random number...")
num = random.randint(1, 10)
return str(num)
def divide(x: int, y: int) -> str:
"""
Use this function to divide two numbers.
Args:
x (int): The first number.
y (int): The second number.
Returns:
str: The quotient of the two numbers.
"""
print("Dividing two numbers...")
return str(x / y)
calculator_agent = Agent(
model=Ollama(id="llama3.2"),
name="Data Agent",
description=("""
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)
output:
DEBUG ************************************************************************** METRICS ***************************************************************************
DEBUG * Tokens: input=345, output=113, total=458
DEBUG * Time: 2.2229s
DEBUG * Tokens per second: 50.8336 tokens/s
DEBUG * Additional metrics: {'total_duration': 2220019200, 'load_duration': 19071400, 'prompt_eval_duration': 19087100, 'eval_duration': 2177474700}
DEBUG ************************************************************************** METRICS ***************************************************************************
DEBUG --------------------------------------------------------------------- Ollama Response End ----------------------------------------------------------------------
DEBUG Added 6 Messages to AgentMemory
DEBUG Added AgentRun to AgentMemory
DEBUG Logging Agent Run
DEBUG ***************************************************** Agent Run End: bd02ea9c-cd30-4059-b26c-d1ba6e05349c ******************************************************
Here's the correct solution:
### Step 1: Add 2 and 5
The result of adding 2 and 5 is 7.
### Step 2: Multiply by 2
Multiplying 7 by 2 gives us 14.
### Step 3: Divide by a random number
Let's generate a random integer between 1 and 10 (inclusive) to serve as the divisor.
Random Number: 8
Now, dividing 14 by 8 gives us:
14 / 8 = 1.75
本文标签: pythonAgno Agentic Frameworkpydantic code ValidationErrorStack Overflow
版权声明:本文标题:python - Agno Agentic Framework - pydantic code ValidationError - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744016769a2576471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论