Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AISecurityLab Hackagent JSON Serialize

From Leeroopedia
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

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>"}

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment