Neural Mastery
← Practice
Validate Tool Arguments Against a Schemaeasy

Validate Tool Arguments Against a Schema

easy Build an Agent 10–15 min

Standard library only · runs in your browser

Models hallucinate arguments: wrong types, missing fields, extra fields they made up. Check them against the tool's JSON Schema before you run anything, and produce error messages clear enough for the model to fix its own call.

Task

Implement validate_arguments(arguments, schema). It returns a list of error strings, and [] means valid.

schema looks like {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}. Supported types are string, number (int or float), integer, boolean, array and object.

Produce errors in this order:

  1. For each name in required order that's missing: missing required argument 'city'
  2. Then for each argument, in the order it appears in arguments:
    • not in properties: unexpected argument 'units'
    • wrong type: argument 'a' should be number, got string

Name the received type in JSON terms: string, boolean, integer, number, array, object or null.

Function Signature

validate_arguments(arguments: dict, schema: dict) -> list[str]

Examples

Example 1: Valid arguments
Input: arguments = {'city': 'Paris'} schema = {'type': 'object', 'properties': {'city': {'type': 'string'}, 'units': {'type': 'string'}}, 'required': ['city']}
Output: []
Example 2: Missing required argument
Input: arguments = {} schema = {'type': 'object', 'properties': {'city': {'type': 'string'}, 'units': {'type': 'string'}}, 'required': ['city']}
Output: ['missing required argument \'city\'']
Example 3: String where a number is required
Input: arguments = {'a': '2', 'b': 3} schema = {'type': 'object', 'properties': {'a': {'type': 'number'}, 'b': {'type': 'number'}}, 'required': ['a', 'b']}
Output: ['argument \'a\' should be number, got string']
Example 4: Hallucinated extra argument
Input: arguments = {'city': 'Lima', 'country': 'PE'} schema = {'type': 'object', 'properties': {'city': {'type': 'string'}, 'units': {'type': 'string'}}, 'required': ['city']}
Output: ['unexpected argument \'country\'']

Constraints

  • Watch out: in Python `True` is an `int`. A boolean is never a valid number or integer.
  • An `int` is a valid `number`, but a float is not a valid `integer`.
  • Error strings must match exactly, because the model reads them in stage 3.
Python3Saved ✓
import json
import math
import re


def validate_arguments(arguments, schema):
"""Return a list of human-readable error strings ([] if the arguments are valid)."""
# Your code here
pass

Case 1: Valid arguments
Input: arguments = {'city': 'Paris'} schema = {'type': 'object', 'properties': {'city': {'type': 'string'}, 'units': {'type': 'string'}}, 'required': ['city']}
Expected: []
Case 2: Missing required argument
Input: arguments = {} schema = {'type': 'object', 'properties': {'city': {'type': 'string'}, 'units': {'type': 'string'}}, 'required': ['city']}
Expected: ['missing required argument \'city\'']
Case 3: String where a number is required
Input: arguments = {'a': '2', 'b': 3} schema = {'type': 'object', 'properties': {'a': {'type': 'number'}, 'b': {'type': 'number'}}, 'required': ['a', 'b']}
Expected: ['argument \'a\' should be number, got string']
Case 4: Hallucinated extra argument
Input: arguments = {'city': 'Lima', 'country': 'PE'} schema = {'type': 'object', 'properties': {'city': {'type': 'string'}, 'units': {'type': 'string'}}, 'required': ['city']}
Expected: ['unexpected argument \'country\'']