Pydantic models for the Open Cybersecurity Schema Framework (OCSF).
pip install pydantic-ocsfPackage versions follow semantic versioning and are independent of OCSF schema versions.
- This package (v2.x) uses OCSF schema version 1.7.0
- Patch versions fix bugs or update type stubs
- Minor versions add features
- Major versions make breaking changes
from ocsf.v1_7_0.objects import File
from ocsf.v1_7_0.events import FileActivity
# Create a file object
file = File(
name="document.pdf",
type_id=1,
size=1024000,
)
# Serialize to JSON
json_str = file.model_dump_json(indent=2)
print(json_str)
# Parse from JSON
parsed = File.model_validate_json(json_str)
# Create an event
event = FileActivity(
class_uid=1001,
category_uid=1,
activity_id=1,
file=file,
metadata={"version": "1.7.0"},
)BREAKING CHANGE in v2.0.0: OCSF models are now organized into namespaces to resolve name collisions.
Models must be imported from their respective namespaces:
# Import objects (User, File, Account, etc.)
from ocsf.v1_7_0.objects import User, Account, File
# Import events (FileActivity, ApiActivity, etc.)
from ocsf.v1_7_0.events import FileActivity, ApiActivityOld style imports NO LONGER WORK:
# ❌ NO LONGER WORKS
from ocsf.v1_7_0 import User, FileActivity
# ✅ Use namespace imports instead
from ocsf.v1_7_0.objects import User
from ocsf.v1_7_0.events import FileActivitySome names exist in both namespaces. Import them explicitly with aliases:
from ocsf.v1_7_0.objects import Finding as FindingObject
from ocsf.v1_7_0.events import Finding as FindingEvent
from ocsf.v1_7_0.objects import Application as AppObject
from ocsf.v1_7_0.events import Application as AppEvent
# These are different classes
assert FindingObject is not FindingEventFor convenience, namespace modules are also accessible from the top level:
from ocsf import objects, events
user = objects.User(name="Alice", uid="123")
activity = events.FileActivity(class_uid=1001, category_uid=1)This package uses OCSF schema version 1.7.0 (latest stable release).
The package includes 8 OCSF versions (v1.0.0 through v1.7.0), each organized into objects and events namespaces:
- 169 objects: User, File, Process, Account, etc.
- 84 events: FileActivity, ApiActivity, ProcessActivity, etc.
If a newer OCSF schema version is released before this package is updated, you can manually use it by downloading the schema and placing it in the appropriate location.
Download the schema JSON from the OCSF Schema Repository:
# Example: Download schema version 1.8.0
VERSION="1.8.0"
curl -o "src/ocsf/schemas/v${VERSION//./_}.json" \
"https://raw.githubusercontent.com/ocsf/ocsf-schema/v${VERSION}/exports/schema.json"Generate type stubs for the new schema:
python3 scripts/regenerate_stubs.py# Import from the new version's namespaces
from ocsf.v1_8_0.objects import File
from ocsf.v1_8_0.events import FileActivityNote: The package maintainers test and validate each bundled schema version. Manually added schemas may have edge cases that aren't fully supported.
You can use custom or extended OCSF schemas with this package's JIT (Just-In-Time) model generation system.
The simplest approach for custom schemas is to fork this repository:
- Fork the repository
- Replace
src/ocsf/schemas/v1_7_0.jsonwith your custom schema - Run
python3 scripts/regenerate_stubs.py - Install from your fork:
pip install git+https://github.com/yourname/pydantic-ocsf.git
For advanced use cases, you can use the internal model factory with your own schema:
from pathlib import Path
import json
from ocsf._model_factory import ModelFactory
from ocsf._version_module import OCSFVersionModule
import sys
# Load your custom schema
with open("path/to/your/custom_schema.json") as f:
custom_schema = json.load(f)
# Create a version module with your schema
custom_module = OCSFVersionModule("ocsf.custom", "custom")
custom_module.schema = custom_schema
custom_module.factory = ModelFactory(custom_schema, "custom")
# Register it so imports work
sys.modules["ocsf.custom"] = custom_module
# Now you can use it with namespaces
from ocsf.custom.objects import File
from ocsf.custom.events import FileActivityYour custom schema must follow the OCSF schema structure:
- Top-level keys:
objects,events,dictionary - Each object/event must have
attributesand optionalextends - Attributes must specify
type(e.g.,string_t,integer_t) and optionallyrequirement(requiredoroptional)
See the OCSF Schema Documentation for details on the schema format.
Maintainers and contributors can add new OCSF schema versions to the released library by following these steps:
Edit scripts/download_schemas.py and add the new version:
VERSIONS = [
"1.7.0",
"1.8.0", # Add new version here
]Run the download script to fetch the new schema:
# First, ensure the schema exists in the .schema_cache directory
# (You may need to manually download it first)
python3 scripts/download_schemas.pyThis will:
- Copy the schema from
.schema_cache/tosrc/ocsf/schemas/ - Compute and store the checksum in
checksums.json - Validate that the schema has the expected structure
Generate type stub files for the new version:
python3 scripts/regenerate_stubs.pyThis creates src/ocsf/v1_8_0/__init__.pyi with type hints for all models.
Create tests for the new version:
# tests/test_v1_8_0.py
def test_import_v1_8_0():
"""Test that v1.8.0 imports work."""
from ocsf.v1_8_0.objects import File
from ocsf.v1_8_0.events import FileActivity
assert FileActivity is not None
assert File is not None
def test_v1_8_0_basic_usage():
"""Test basic model creation with v1.8.0."""
from ocsf.v1_8_0.objects import File
file = File(name="test.txt", type_id=1)
assert file.name == "test.txt"Run all tests to ensure nothing broke:
pytest tests/ -vUpdate the README.md and CLAUDE.md:
## OCSF Schema Versions
This package supports OCSF schema versions:
- v1.7.0 (stable)
- v1.8.0 (latest)Ensure all quality checks pass:
just check
# Or manually:
ruff format src/ tests/ scripts/
ruff check src/ tests/ scripts/
mypy src/ocsf/ --ignore-missing-imports
pytest tests/ -v --cov=ocsf-
Commit your changes with a descriptive message:
git add . git commit -m "Add support for OCSF schema v1.8.0"
-
Push to your fork and create a pull request
-
Include in your PR description:
- OCSF schema version added
- Link to the OCSF schema release notes
- Any breaking changes or schema differences
- Test coverage for the new version
Schemas are typically downloaded from:
- Official releases:
https://github.com/ocsf/ocsf-schema/releases - Schema exports:
https://raw.githubusercontent.com/ocsf/ocsf-schema/v{VERSION}/exports/schema.json
When adding a new schema version, consider the package versioning:
- Patch version (2.0.x → 2.0.y): Bug fixes, type stub updates for existing versions
- Minor version (2.0.x → 2.1.0): Adding new schema versions (backward compatible)
- Major version (2.x → 3.0): Removing old schema versions or breaking API changes
Note: v2.0.0 introduced namespace organization (breaking change from v1.x)
Apache 2.0