A model can call a function with the wrong argument: a word where a number belongs, a missing required field, or a value outside the allowed range. Your code must handle that safely: reject the call, log it, and tell the model what was wrong, so the model can try again with a correct argument. Test this before the agent meets a real user, by sending a bad argument on purpose.
Prerequisites
- A function with a typed schema, including at least one required argument.
- A test harness that can send a tool call with chosen arguments.
- A rule for what happens on a bad call: reject, log, and report back.
Steps
Step 1: Send a bad argument
Send a tool call with a deliberately wrong argument: a string where the schema wants an integer, or a missing required field. Do this in a test, never on real data.
Step 2: Read the failure
Read what your code does: it should reject the call, log the argument and the error, and return an error message to the model. It should never run the function with the bad argument.
Step 3: Fix the handling
If the code runs the bad call, or crashes, fix the validation first. If the model keeps sending the same bad argument, the function’s description or the schema needs clarifying, not the code.
Worked example: Northgate Clinic appointment lookup
The fictional Northgate Clinic tests its lookup_appointment function with a bad date argument.
| Test | Argument sent | Code response | Safe |
|---|---|---|---|
| Missing date | No date field | Rejected: date is required | Yes |
| Wrong type | date: ‘soon’ | Rejected: date must be YYYY-MM-DD | Yes |
| Valid call | date: ‘2026-12-03’ | Ran and returned the appointment | Yes |
The missing date and the wrong-type date are both rejected with a clear message, and the valid call runs, so bad arguments fail safely.
Expected result. Bad arguments are rejected with a clear error, logged, and never executed. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- Validation catches type and format errors, not wrong values; a valid-looking wrong date still runs.
- Test with one bad argument at a time, so you know which check caught it.
- This guide does not cover a model that ignores the error and repeats the bad call.
