InterviewPrepKit

Home / Coding / Agent Coding / Tools & Function Calling / Validating Tool Arguments

Validating Tool Arguments

medium 00:00
Solving tips
  • The model produces arguments as untrusted text; validate them before you ever call the tool.
  • Check three things in order: no missing required args, no unknown args, and every value has the right type.
  • Return a machine-usable result — (ok, error) — so the loop can feed a clear error back to the model instead of crashing.

Before an agent runs a tool, it has to trust the arguments the model produced — but those arguments arrive as untrusted, model-generated data. A hallucinated field, a missing required parameter, or a string where an integer belongs will make the underlying function crash or, worse, do the wrong thing silently. The guard that sits between the model’s proposed call and the real function is one of the most common things interviewers ask you to build in an agent round.

Task

Complete validate_tool_args(schema, required, args):

  • schema maps each valid argument name to its expected Python type.
  • required is the set of argument names that must be present.
  • args is the dict the model produced.

Check for problems in this exact order and return the first one you find:

  1. Missing required arg — a name in required is absent from args.
  2. Unknown arg — a key in args is not present in schema.
  3. Wrong type — a value is not an instance of the type declared in schema.

Return a tuple (ok, error_message): (True, "") when everything passes, otherwise (False, "<reason>") describing the first failure.

Example

schema = {"city": str, "days": int}
required = {"city"}

validate_tool_args(schema, required, {"city": "Paris", "days": 3})
# -> (True, "")

validate_tool_args(schema, required, {"days": 3})
# -> (False, "missing required arg: city")

validate_tool_args(schema, required, {"city": "Paris", "unit": "C"})
# -> (False, "unknown arg: unit")

validate_tool_args(schema, required, {"city": "Paris", "days": "3"})
# -> (False, "type error: days expected int, got str")

Constraints

  • Enforce the ordering: a missing required arg is reported before an unknown arg, which is reported before a type error.
  • args may contain extra keys, be missing keys, or hold wrong-typed values — never assume it is well-formed.
  • Do not call the tool; only validate. No network or LLM calls.
  • Watch the bool/int subtype trap: in Python isinstance(True, int) is True, so a declared-int field will accept True unless you guard it. Handle it however you justify in the interview, but be ready to explain the choice.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug