Architecture
PyMLB StatsAPI uses a config-driven, schema-based architecture where all MLB API endpoints are defined as JSON schemas rather than hardcoded Python classes.
Core Design Pattern
Schema-Driven API Wrapper
The library uses JSON schemas to define MLB API endpoints, enabling:
Dynamic endpoint discovery
Self-documenting API methods
Easy updates when MLB API changes
Automatic parameter validation
Key Components
Schema System
Located in pymlb_statsapi/resources/schemas/:
endpoint-model.json: Master configuration mapping endpoint names to paths and method namesschemas/statsapi/stats_api_1_0/*.json: Individual endpoint schemas with full parameter definitions
Each JSON schema defines:
Available operations (methods)
Parameters (path vs query)
Validation rules
Documentation
Model Layer
Base Models (model/factory.py):
DynamicEndpoint: Base class for all endpoint modelsEndpointMethod: Represents individual API operationsAPIResponse: Response wrapper with metadata
Dynamic Factory (model/factory.py):
Generates endpoint classes and methods from schemas at runtime
Creates clean function signatures with proper parameter handling
Handles method overloading
Registry (model/registry.py):
Central
apisingleton that loads all endpointsProvides discovery API for exploring available methods
Clean API Pattern
The “Clean API” intelligently routes parameters to path or query parameters based on each method’s schema configuration. Developers simply pass parameters as keyword arguments:
# Library automatically determines parameter types from schema
response = api.Game.liveGameV1(game_pk="747175", timecode="20241027_000000")
# Resolves to: /api/v1/game/747175/feed/live?timecode=20241027_000000
Data Flow
User calls
api.Schedule.schedule(sportId=1, date="2025-06-01")Registry returns
ScheduleModelinstance (loaded from schedule.json schema).schedule()method looks up configuration in endpoint-model.jsonReturns
APIResponsewith:Endpoint model
API definition from schema
Operation with parameter rules
Resolved URL:
https://statsapi.mlb.com/api/v1/schedule?sportId=1&date=2025-06-01
Response includes
.json(),.save_json(),.get_uri()methods
Configuration
The architecture is controlled by:
endpoint-model.json: Endpoint to path mappingsIndividual schema files: Operation definitions and parameter rules
Environment variables:
PYMLB_STATSAPI__BASE_FILE_PATH, etc.
This design enables the library to adapt to MLB API changes by simply updating JSON schemas, with no code modifications required.