A comprehensive Pydantic-based validation schema for Agent-Based Models (ABM) defined in JSON format.
This project provides a robust schema for defining agent-based models in JSON, with full validation support through Pydantic. The schema enables:
- JSON-based ABM definitions that can be validated and type-checked
- LLM-friendly format for AI-assisted model generation
- Backend pipeline support for converting validated JSON to executable Mesa Python code
abm_json_round2/
βββ README.md # This file
βββ abm_json_schema.py # Main Pydantic schema definition
βββ abm_json_schema.json # JSON schema reference (draft)
βββ sir_example.json # Complete SIR epidemiological model
βββ test_sir_schema.py # SIR example validation script
βββ requirements.txt # Python dependencies
βββ pyproject.toml # Project configuration
βββ tests/ # Test suite directory
βββ README.md # Test documentation
βββ test_validator.py # Main test runner
βββ test_*.json # Test cases
βββ *.md # Feature documentation
# Install dependencies
pip install -r requirements.txtfrom abm_json_schema import ABMJsonSchema
import json
# Load and validate an ABM model
with open('sir_example.json') as f:
model = ABMJsonSchema(**json.load(f))
print("β
Model is valid!")cd tests
python3 test_validator.py- Function call validation (existence, argument counts, types)
- Source reference validation (all references must exist)
- Optional parameter support with proper ordering
- Unused resource detection (functions, variables)
- Scheduler reference validation
- Support for global functions, variables, and behaviors
- Agent-based and environment-based components
- Multiple agent types per model
- Customizable initialization and scheduling
- Data analytics tracking
- Functions can have optional parameters with defaults
- Proper validation of parameter ordering
- Flexible function calls (min to max arguments)
- Function parameters don't have
source_name(determined at call site) - Separation of function signatures from data flow
- Reusable function definitions
- Global Functions - Reusable helper functions
- Global Variables - Model-wide state
- Environment - The world/context
- Environment Attributes
- Environment Behaviors
- Agents - Active entities
- Agent Attributes
- Agent Behaviors
- Scheduler - Execution order
- Initialization order
- Step-by-step schedule
- Termination Criteria - When to stop
- Data Analytics - What to track
The schema uses a source naming convention for clarity:
globalVariable.name- Global variablesglobalFunction.name- Global functionsenvironment.environmentAttribute.name- Environment attributesenvironment.environmentBehavior.name- Environment behaviorsagent.AgentType.agentAttribute.name- Agent attributesagent.AgentType.agentBehavior.name- Agent behaviors
See sir_example.json for a complete epidemiological SIR (Susceptible-Infected-Recovered) model demonstrating:
- Network-based agent interactions
- State transitions (S β I β R)
- Global variable tracking
- Termination conditions
- Deterministic initialization
- Bidirectional network connections
Run the SIR example:
python3 test_sir_schema.pyβ Function Calls
{
"function": "randomInt",
"args": [1, 10] // Must match function signature
}β Optional Parameters
{
"functionInputs": [{
"name": "distance",
"type": "float",
"optional": true,
"defaultValue": 1.0 // Required for optional params
}]
}β Source References
{
"source_name": "globalVariable.population" // Must exist
}β Resource Usage
- Warns if functions are defined but never called
- Warns if variables are defined but never used
- README.md (this file) - Main project documentation
- tests/README.md - Test suite documentation
- tests/TEST_VALIDATOR_README.md - Detailed test case descriptions and validation rules
- Python 3.8+
- Pydantic 2.0+
Create a JSON file following the schema structure (see sir_example.json).
from abm_json_schema import ABMJsonSchema
import json
with open('my_model.json') as f:
model = ABMJsonSchema(**json.load(f))The validator provides detailed error messages:
- Invalid function calls
- Missing source references
- Unused resources
- Parameter ordering issues
- Argument count mismatches
The validated JSON can be used as input for a backend pipeline that generates executable Mesa Python code.
- Validation First - Catch errors before execution
- LLM-Friendly - Structured format for AI-assisted generation
- Explicit Over Implicit - Clear naming and references
- Reusable Components - Functions and behaviors are truly reusable
- Type Safety - Strong type checking throughout
{
"functionInputs": [
{
"name": "param1",
"description": "Required parameter",
"type": "integer"
},
{
"name": "param2",
"description": "Optional parameter",
"type": "float",
"optional": true,
"defaultValue": 1.0
}
]
}{
"initialValue": {
"function": "functionName",
"args": [10, 2.5]
}
}{
"args": [
"globalVariable.count", // Reference a variable
"agent.Person.age", // Reference an agent attribute
5 // Or use a literal
]
}The test suite includes 17 comprehensive tests organized into 3 categories:
- β Basic tests (6) - Core validation functionality including library compatibility
- β Reference validation (4) - Source names, scheduler references, formatting
- β Advanced tests (7) - Optional parameters, edge cases, type checking
- β SIR example validation - Complete epidemiological model
- β Combined multi-error test - 15+ error types detected simultaneously
Run all tests:
cd tests && python3 test_validator.pyExpected output: 17/17 tests passed
- Mesa code generation pipeline
- Additional visualization support
- Network topology validation
- Spatial grid validation
- Parameter sweep support
When making changes:
- Update the schema in
abm_json_schema.py - Update test cases as needed
- Run the full test suite
- Update documentation
[Your License Here]
[Your Name/Team]
Built with Pydantic for robust validation and type safety.