liveBook · Manning

JSON Schema

As explained in Build a Multi-Agent System (from Scratch)

JSON Schema is a standard for describing and validating the structure, data types, and constraints of JSON data.

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

How Build a Multi-Agent System (from Scratch) covers it

How deep it goes
Builds schemas manually and from inspected signatures, then validates arguments; identifies Pydantic as a more robust alternative.
Examples
Uses a required numeric x parameter for a Hailstone step and generates schemas from Python function annotations.
What you can do after
Apply it via jsonschema to validate tool-call arguments before invoking wrapped Python functions.
How it connects
Connects JSON Schema with Python type annotations, function signatures, and LLM tool calling.

Also explained in

Compare how 6 books explain JSON Schema →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Multi-Agent System (from Scratch). The section links point to the parts of the book it draws on.