0
Phase 6

Tool Validation

~2 min read

Concept & How It Works

    Why Does It Exist?

    LLMs produce malformed JSON, wrong types, and dangerous inputs. Validation is the last gate before a tool touches production systems.

    Real-World Analogy

    Tool validation is airport security — even if you have a ticket (tool call), your bag (arguments) gets scanned before boarding.
    Loading diagram...

    Visual Workflows

    What is Tool Validation?

    Loading diagram...

    Example

    Scenario

    The LLM calls delete_file(path='/etc/passwd'). Validation rejects: path outside allowed /workspace directory.

    Solution

    In Tool Calling & Function Calling, apply Tool Validation to this scenario: The LLM calls delete_file(path='/etc/passwd'). Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

    Practice Task

    Do this before moving to the next module — reading alone is not enough.

    Open the Code Walkthrough below and run it locally. Change one parameter related to Tool Validation (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

    Code Walkthrough

    Highlighted lines show where Tool Validation happens in the code.

    Tool Validation
    1def validate_and_run(tool_name, args, registry):  # define a reusable function2    schema = registry.get_schema(tool_name)  # key line for Tool Validation3    errors = jsonschema.validate(args, schema)4    if errors:5        return {"error": f"Invalid args: {errors}"}  # return the result6    if tool_name == "delete_file" and not args["path"].startswith("/workspace/"):  # key line for Tool Validation7        return {"error": "Path not allowed"}  # return the result8    return registry.execute(tool_name, args)  # return the result

    Commands to Remember

    Commands to Remember

    • client.chat.completions.create(..., tools=[...]) # pass tool schemas to API
    • json.loads(response.choices[0].message.tool_calls[0].function.arguments) # parse tool args
    • pip install pydantic # validate tool inputs with schemas

    Common Mistakes

    • Treating Tool Validation as a black box without evaluation
    • Ignoring cost and latency in production
    • Skipping error handling for tool validation

    Cheat Sheet

    Quick recap — the most important points from this module.

    Cheat Sheet

    quick ref
    • Tool Validation
    • JSON Schema
    • Sandboxing
    • Allowlist