Lesson 1 of 4
Validate a Reply Before Using It
Estimated time: 10 minutes
Validate a reply before acting on it
A tool reply is data from outside the agent, and a value of the wrong shape can be accepted and passed on before anyone notices. The check belongs at the boundary where the reply is first read.
Check the shape, not the meaning
A shape check asks whether the reply carries the fields the caller expects and whether they are of the right kind. It does not try to judge whether the value is wise, only whether it is understandable.
Treat a missing field as a fault
A default quietly turns a broken reply into a plausible one. When a field the caller needs is absent, the honest outcome is a fault, so the gap is seen instead of guessed.
def valid(reply, fields):
return all(f in reply for f in fields)
Keep the check near the consumer
A check that lives far from where the reply is used drifts out of step with the code that reads it. Put it at the boundary, so a change in one tool is caught where the reply first arrives.