Lesson 34.2 · 2 of 6 · 5 min read · Reviewed 24 Nov 2026
Input schemas and required fields
By the end, you can
- Design input schemas that prevent bad calls.
- Use required fields, enums and formats.
A tool’s input_schema is a JSON Schema. Treat it as validation and documentation at once.
{
"type": "object",
"properties": {
"order_id": {"type": "string", "description": "Order ID, e.g. 'ORD-48213'"},
"amount": {"type": "number", "description": "Refund amount in USD, e.g. 40.00"},
"reason": {"type": "string", "enum": ["damaged", "late", "wrong_item", "other"]}
},
"required": ["order_id", "amount", "reason"]
}
Good habits
- Types that match what your code needs: numbers as numbers, not strings.
- Enums for fixed choices.
- Descriptions with examples and units.
- Required for anything the call can’t work without.
- Fewer, clearer parameters. Prefer
order_idto five optional lookup fields.
Validate anyway
Check inputs in your code too. The schema guides Claude, and your code is the last line of defence, especially for actions such as refunds.
Key points
- The input schema is the contract for each call: types, required fields, allowed values.
- Enums and formats stop invalid values before your code runs.
- Describe each parameter, including units, formats and examples.
Validate refund calls against a schema
Check a tool's input against its schema in code before acting on it.
You will have: A validate() function and a report for four calls, with each problem named.
Steps
- Read the schema: types, a pattern, an enum, a minimum and required fields.
- Add the type check, remembering that True counts as an int in Python.
- Add the enum, pattern and minimum checks.
- Run it and read each problem message as the model would.
Done when
- Only the first call is valid.
- The $40 amount fails the number type check.
- The script prints valid: 1 of 4.
Open in the Playground No API key needed.
Starter code (Python)
# Validate tool inputs against a small JSON Schema before your code acts on them.
import re
SCHEMA = {
"type": "object",
"properties": {
"order_id": {"type": "string", "pattern": r"^ORD-\d+$"},
"amount": {"type": "number", "minimum": 0.01},
"reason": {"type": "string", "enum": ["damaged", "late", "wrong_item", "other"]},
},
"required": ["order_id", "amount", "reason"],
}
TYPES = {"string": str, "number": (int, float)}
def validate(args, schema=SCHEMA):
problems = [f"{k}: required" for k in schema["required"] if k not in args]
for key, value in args.items():
rule = schema["properties"].get(key)
if rule is None:
problems.append(f"{key}: not in schema")
continue
# TODO: check the type (TYPES[rule["type"]]); note that True is an int in Python
# TODO: check enum, pattern (re.match) and minimum when the rule has them
return problems
calls = [
{"order_id": "ORD-48213", "amount": 40.0, "reason": "damaged"},
{"order_id": "ORD-48213", "amount": "$40", "reason": "late"},
{"order_id": "48213", "amount": 12.5, "reason": "changed my mind"},
{"order_id": "ORD-1", "amount": 10},
]
ok = 0
for c in calls:
p = validate(c)
ok += not p
print("valid" if not p else "invalid: " + "; ".join(p))
print(f"valid: {ok} of {len(calls)}")
Show a solution
# Validate tool inputs against a small JSON Schema before your code acts on them.
import re
SCHEMA = {
"type": "object",
"properties": {
"order_id": {"type": "string", "pattern": r"^ORD-\d+$"},
"amount": {"type": "number", "minimum": 0.01},
"reason": {"type": "string", "enum": ["damaged", "late", "wrong_item", "other"]},
},
"required": ["order_id", "amount", "reason"],
}
TYPES = {"string": str, "number": (int, float)}
def validate(args, schema=SCHEMA):
problems = [f"{k}: required" for k in schema["required"] if k not in args]
for key, value in args.items():
rule = schema["properties"].get(key)
if rule is None:
problems.append(f"{key}: not in schema")
continue
if isinstance(value, bool) or not isinstance(value, TYPES[rule["type"]]):
problems.append(f"{key}: expected {rule['type']}, got {type(value).__name__}")
continue
if "enum" in rule and value not in rule["enum"]:
problems.append(f"{key}: must be one of {rule['enum']}")
if "pattern" in rule and not re.match(rule["pattern"], value):
problems.append(f"{key}: must match {rule['pattern']}")
if "minimum" in rule and value < rule["minimum"]:
problems.append(f"{key}: must be at least {rule['minimum']}")
return problems
calls = [
{"order_id": "ORD-48213", "amount": 40.0, "reason": "damaged"},
{"order_id": "ORD-48213", "amount": "$40", "reason": "late"},
{"order_id": "48213", "amount": 12.5, "reason": "changed my mind"},
{"order_id": "ORD-1", "amount": 10},
]
ok = 0
for c in calls:
p = validate(c)
ok += not p
print("valid" if not p else "invalid: " + "; ".join(p))
print(f"valid: {ok} of {len(calls)}")