Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Tool Validation
Tool validation checks that inputs are correct before executing the tool. Catching bad inputs early prevents wasted API calls, data corruption, and hard-to-debug errors.
8 min•By Priygop Team•Updated 2026
What to Validate
- Required fields: check that all required parameters are present
- Type checks: verify each parameter is the correct type (string, int, bool, list)
- Range checks: numbers within valid ranges, strings within length limits
- Format checks: email addresses, URLs, dates in expected format
- Business rules: an end date must not be before a start date
- Injection prevention: do not allow inputs that could run arbitrary code
Validation Example
Validation Example
# Tool with built-in input validation
from datetime import datetime
def search_orders(
customer_email: str,
start_date: str,
end_date: str,
status: str = "all"
) -> dict:
"""Search customer orders. Returns list of matching orders."""
# Validate email
if "@" not in customer_email or "." not in customer_email.split("@")[1]:
return {"status": "error", "error": "Invalid email address format"}
# Validate dates
try:
start = datetime.strptime(start_date, "%Y-%m-%d")
end = datetime.strptime(end_date, "%Y-%m-%d")
except ValueError:
return {"status": "error", "error": "Dates must be in YYYY-MM-DD format"}
if end < start:
return {"status": "error", "error": "end_date must be after start_date"}
# Validate status
valid_statuses = {"all", "pending", "shipped", "delivered", "cancelled"}
if status not in valid_statuses:
return {"status": "error", "error": f"status must be one of: {valid_statuses}"}
# All valid — proceed
return {
"status": "success",
"customer": customer_email,
"period": f"{start_date} to {end_date}",
"filter": status,
"records": [] # would come from DB in real implementation
}
result = search_orders("user@example.com", "2024-01-01", "2024-12-31")
print(result["status"])Key Takeaways
- Tool validation checks that inputs are correct before executing the tool.
- Required fields: check that all required parameters are present
- Type checks: verify each parameter is the correct type (string, int, bool, list)
- Range checks: numbers within valid ranges, strings within length limits