Invalid Tool Arguments
Invalid tool arguments are decision failures — the agent chose to call a tool but passed wrong or missing parameters. These must be caught before the tool executes.
8 min•By Priygop Team•Updated 2026
Preventing Invalid Arguments
- Validate all required parameters before calling the tool — check for None, empty strings, wrong types
- Use schema validation (Pydantic, jsonschema) to catch structural errors automatically
- Check argument values against business rules (e.g., date ranges, positive numbers)
- If an argument is missing, check whether it can be inferred from context before escalating
- Log every invalid argument with the exact values received — essential for debugging agent decisions
Pydantic Argument Validation
Pydantic Argument Validation
from pydantic import BaseModel, EmailStr, field_validator
from typing import Optional
import json
# Define expected tool arguments with automatic validation
class SearchOrdersArgs(BaseModel):
customer_email: str
start_date: str
end_date: str
status: Optional[str] = "all"
@field_validator("customer_email")
@classmethod
def validate_email(cls, v):
if "@" not in v or "." not in v.split("@")[-1]:
raise ValueError(f"Invalid email format: {v}")
return v.lower()
@field_validator("status")
@classmethod
def validate_status(cls, v):
allowed = {"all", "pending", "shipped", "delivered", "cancelled"}
if v not in allowed:
raise ValueError(f"status must be one of: {allowed}")
return v
def validate_tool_args(tool_name: str, raw_args: dict) -> dict:
"""Validate tool arguments before execution."""
validators = {
"search_orders": SearchOrdersArgs,
}
validator_cls = validators.get(tool_name)
if not validator_cls:
return {"valid": True, "args": raw_args}
try:
validated = validator_cls(**raw_args)
return {"valid": True, "args": validated.model_dump()}
except Exception as e:
return {"valid": False, "error": str(e), "error_type": "validation"}
# Test validation
good_args = {"customer_email": "User@Example.COM",
"start_date": "2024-01-01", "end_date": "2024-12-31"}
bad_args = {"customer_email": "not-an-email",
"start_date": "2024-01-01", "end_date": "2024-12-31"}
print("Good args:", validate_tool_args("search_orders", good_args))
print("Bad args: ", validate_tool_args("search_orders", bad_args))Key Takeaways
- Invalid tool arguments are decision failures — the agent chose to call a tool but passed wrong or missing parameters.
- Validate all required parameters before calling the tool — check for None, empty strings, wrong types
- Use schema validation (Pydantic, jsonschema) to catch structural errors automatically
- Check argument values against business rules (e.g., date ranges, positive numbers)