Implementation:AISecurityLab Hackagent JSON Serialize
| Knowledge Sources | |
|---|---|
| Domains | Tracking, Serialization |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete JSON sanitization utilities for the tracking subsystem: deep-cleaning Pydantic objects to plain dicts and sanitizing for JSON (redacting secrets, handling inf/nan, replacing non-serializable values), provided by the HackAgent tracking package.
Description
deep_clean() recursively converts Pydantic/OpenAI model objects to plain Python structures (using model_dump() or dict()). sanitize_for_json() applies rules recursively: client objects in _client/client keys become type placeholders, sensitive keys (key/token/secret/password) become "***REDACTED***" (except usage count keys), inf/nan become string literals, and non-serializable values become type name placeholders.
Usage
- Tracking payloads: Both tracker.py and step.py use these to sanitize payloads before JSON serialization.
- Single source of truth: All sanitization fixes happen in one place.
Code Reference
Source Location
- Repository: AISecurityLab_Hackagent
- File: hackagent/tracking/serialize.py
Signature
def deep_clean(obj: Any) -> Any: ...
def sanitize_for_json(obj: Any) -> Any: ...
Import
from hackagent.tracking.serialize import deep_clean, sanitize_for_json
Usage Examples
Sanitize a Payload for JSON
from hackagent.tracking.serialize import sanitize_for_json
import json
payload = {"api_key": "secret123", "data": {"score": float("inf")}, "client": httpx.Client()}
safe = sanitize_for_json(payload)
print(json.dumps(safe)) # {"api_key": "***REDACTED***", "data": {"score": "Infinity"}, "client": "<Client>"}