JSON Schema describes a tool’s parameters in a format compatible with LLM tool-calling APIs and SDKs. A parameter schema can define an object’s properties and required fields. section 2.1.2
A tool integration can use schemas to:
- Declare parameters manually, such as a required numeric x for the Hailstone tool. section 2.1.2
- Generate a schema by inspecting a Python function’s annotations and signature, mapping types and identifying parameters without defaults as required. section 2.2.1
- Validate tool-call arguments against the schema before delegating to the wrapped function. section 2.2.1
Listing 2.6 Helper for turning a function signature into a JSON Schema
# llm_agents_from_scratch/tools/simple_function.py
import inspect
from typing import Any, Callable, get_type_hints
def function_signature_to_json_schema(func: Callable) -> dict[str, Any]:
"""Turn a function signature into a JSON schema.
Inspects the signature of the function and maps types to the
appropriate JSON schema type.
Args:
func (Callable): The function for which to get the JSON schema.
Returns:
dict[str, Any]: The JSON schema
"""
sig = inspect.signature(func) #A
type_hints = get_type_hints(func)
python_to_json_schema_type = { #B
str: "string",
int: "number",
float: "number",
dict: "object",
list: "array",
type(None): "null",
bool: "boolean",
tuple: "array",
bytes: "string",
set: "array",
}
properties = {}
required = []
for param in sig.parameters.values(): #C
# skip args and kwargs
if param.kind in (param.VAR_POSITIONAL, param.VAR_KEYWORD):
continue
annotation = type_hints.get(param.name, param.annotation)
if annotation in python_to_json_schema_type:
this_params_json_schema = {
"type": python_to_json_schema_type[annotation],
}
else:
# fallback schema, that accepts everything
this_params_json_schema = {}
properties[param.name] = this_params_json_schema
# check if param is required
if param.default == inspect._empty: #D
required.append(param.name)
return { #E
"type": "object",
"properties": properties,
"required": required,
}See the helper that converts a Python function signature into a JSON Schema. section 2.2.1





