JJoeven

Reference/Python

Python: JSON, types, and dicts

json.loads/dumps, dict key sets, and the action object {tool, args} used everywhere on Joeven.

JSON is the wire format of agents. Python's json module is stdlib.

Loads and dumps

CallUse
json.loads(s)string → object
json.dumps(obj)object → string
json.dumps(obj, sort_keys=True, separators=(",", ":"))canonical string for hashes
json.dumps(obj, indent=2)debug only (not for digests)

Allowed JSON types: dict, list, str, int, float, bool, None. Not allowed: set, tuple (becomes list), custom classes.

python
import json
json.loads(class="tok-s">'{"tool":"geocode","args":{"city":"Paris"}}')
json.dumps({class="tok-s">"b": 1, class="tok-s">"a": 2}, sort_keys=True)

Action object

Require exact keys:

set(obj.keys()) == {"tool", "args"}

CheckFail closed
not a dictparse error
extra keysparse error (injection hideout)
tool not strparse error
args not dictparse error

Dict patterns

PatternMeaning
d.get(k)missing → None
d.get(k, default)missing → default
k in dmembership
dict(d)shallow copy
{**d, k: v}copy with override
Tip:Never eval JSON. Never json.loads on a value you already parsed into a dict.

Errors

json.JSONDecodeError is a ValueError. Catch both in parse retries. Do not catch Exception around tool bodies or you will hide bugs.