Validate tool arguments before execution
In this article (5 sections)
A syntactically valid tool call can still request an empty identifier, impossible date or unauthorized resource. Treat model arguments as untrusted input.
Reject before calling the tool
The agent controls lab checks two authored calls.
from agent_cases import argument_validation_case
result = argument_validation_case()
assert result["accepted_indices"] == [0]
assert result["rows"][1]["valid"] is False
assert result["invalid_executed"] is Falserefund-v2 with an ISO date passes; empty ID and tomorrow fail. No external tool runs.
Validate four layers
- 1Parse against a strict schema with required fields and bounded lengths.
- 2Enforce domain rules such as valid date ranges, units and mutually exclusive fields.
- 3Check authenticated identity, resource scope and action authorization.
- 4Attach an idempotency key and approval reference for consequential writes.
Resolve opaque identifiers in application code rather than letting the model invent them. Never place credentials inside arguments. Return structured errors the workflow can route, while avoiding sensitive diagnostic leakage.
Test adversarial and accidental inputs
Include unknown fields, path traversal, huge arrays, encoded content, cross-tenant IDs, stale approval and duplicate calls. A schema-constrained model output helps format but does not replace application validation.
The Generative & Agentic AI course applies the same contract discipline to agent tools and MCP integrations.
Exercise
Define a tool schema and ten domain/authorization rules. Generate 30 invalid calls, prove none reaches the adapter and preserve reason codes without echoing secrets.
Continue learning
This article is part of the Agent workflows and state sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Build a tool-calling loop with explicit termination.
- Continue with Separate an agent's proposal from an approved action.
Reference: JSON Schema validation.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Generative & Agentic AI programme — 3 months. Add practical GenAI, retrieval and agent-building skills to your existing toolkit.
Explore Generative & Agentic AI