Pydantic Models for OCSF
ocsf-pydantic provides Pydantic v2 models for the Open Cybersecurity Schema Framework (OCSF). It enables type-safe Python representations of OCSF schemas, facilitating event parsing, validation, and generation in cybersecurity applications. The current version is 0.0.6, and its release cadence is irregular, typically aligned with updates to the OCSF specification or bug fixes.
Common errors
-
ModuleNotFoundError: No module named 'pydantic.v1'
cause You have Pydantic V1 installed, but `ocsf-pydantic` requires Pydantic V2.fixUpgrade Pydantic to version 2: `pip install --upgrade 'pydantic>=2,<3'`. -
pydantic_core._pydantic_core.ValidationError: 1 validation error for FileActivity\nactivity_id\n Field required
cause A required field for the OCSF event model was omitted or set to `None` where not allowed.fixRefer to the OCSF specification for the event type you are using and ensure all required fields are provided with valid data. For `FileActivity`, `activity_id` is mandatory. -
AttributeError: 'FileActivity' object has no attribute 'non_existent_field'
cause You are trying to access a field that is not part of the OCSF schema for the specific event or object model, or the field name is misspelled.fixConsult the OCSF specification or the generated `ocsf-pydantic` model's attributes (e.g., using `dir(event_object)` or inspecting the class definition) to confirm valid field names.
Warnings
- breaking This library is pre-1.0, and its API is subject to change without strict adherence to semantic versioning. Updates to the underlying OCSF specification can also lead to breaking changes in model structure.
- gotcha `ocsf-pydantic` strictly requires Pydantic V2 (>=2.0.0). It is incompatible with Pydantic V1.
- gotcha Many common OCSF event fields (e.g., `type_id`, `class_name`, `severity`, `category_name`) are automatically set by the specific event models (e.g., `FileActivity`) based on the OCSF specification. Attempting to manually override these can lead to unexpected behavior or validation errors.
Install
-
pip install ocsf-pydantic
Imports
- FileActivity
from ocsf_pydantic.events import FileActivity
from ocsf_pydantic.events.file_activity import FileActivity
- User
from ocsf_pydantic.objects import User
from ocsf_pydantic.objects.user import User
Quickstart
from datetime import datetime, timezone
from ocsf_pydantic.events.file_activity import FileActivity
from ocsf_pydantic.objects.file import File
from ocsf_pydantic.objects.user import User
# Create an OCSF FileActivity event
file_activity_event = FileActivity(
time=datetime.now(timezone.utc),
correlation_uid="example-correlation-id-456",
activity_id=1, # Represents FileActivityId.CREATE
file=File(
name="report.pdf",
path="/home/user/documents/report.pdf",
size=10240,
hash_md5="d41d8cd98f00b204e9800998ecf8427e"
),
user=User(name="analyst_user", uid="U007"),
message="New report generated by analyst_user"
)
# Print the event as JSON
print(file_activity_event.model_dump_json(indent=2))
# Access a specific field
print(f"\nEvent Type Name: {file_activity_event.activity_name}")
print(f"File Name: {file_activity_event.file.name}")