#2847·smolagents

SafeSerializer corrupts/crashes on a plain dict whose own `__type__` key collides with an internal type tag

Author: VANDRANKICreated Sep 25, 2026Updated Sep 25, 2026

SafeSerializer corrupts or crashes on a plain dict whose own __type__ key collides with an internal type tag

What happens

SafeSerializer.to_json_safe()/from_json_safe() in src/smolagents/serialization.py use a "__type__" key inside a dict to mark values that need special decoding (bytes, tuple, set, dataclass, PIL.Image, datetime, ...). This is used for send_variables() to a remote executor (E2B/Docker/Modal/Blaxel) and for capturing a tool's final answer via _patch_final_answer_with_exception.

The problem: nothing stops an ordinary user dict from itself containing a key literally named "__type__". If that dict passes through SafeSerializer.dumps()/.loads(), the round trip either raises an unrelated, confusing exception, or silently returns a completely different object than what was sent - depending only on what string happens to be under that key.

to_json_safe() does not know anything is wrong, because a plain dict with string keys is serialized as-is:

python
# obj_type is dict (fast path)
if obj_type is dict:
    if all(type(k) is str for k in obj):
        return {k: SafeSerializer.to_json_safe(v) for k, v in obj.items()}
    ...

from_json_safe() then treats the resulting "__type__" key as its own marker, with no way to tell a real marker from a coincidental one:

python
if isinstance(obj, dict):
    if "__type__" in obj:
        obj_type = obj["__type__"]
        if obj_type == "bytes":
            return base64.b64decode(obj["data"])
        ...
        elif obj_type == "dataclass":
            return {
                "__dataclass__": obj["class_name"],
                "__module__": obj["module"],
                **{k: SafeSerializer.from_json_safe(v) for k, v in obj["data"].items()},
            }
    return {k: SafeSerializer.from_json_safe(v) for k, v in obj.items()}

Repro

Ran directly against the current src/smolagents/serialization.py (main branch, verified unchanged just before filing):

python
from serialization import SafeSerializer

original = {"__type__": "bytes", "data": "hello world this is plain text, not base64 bytes"}
serialized = SafeSerializer.dumps(original, allow_pickle=False)
restored = SafeSerializer.loads(serialized, allow_pickle=False)

Actual output:

Serialized: safe:{"__type__": "bytes", "data": "hello world this is plain text, not base64 bytes"}
binascii.Error: Incorrect padding

A second case shows silent corruption instead of a crash:

python
original2 = {"__type__": "dataclass", "class_name": "whatever", "module": "whatever", "data": {"x": 1}}
serialized2 = SafeSerializer.dumps(original2, allow_pickle=False)
restored2 = SafeSerializer.loads(serialized2, allow_pickle=False)

Actual:

Original:  {'__type__': 'dataclass', 'class_name': 'whatever', 'module': 'whatever', 'data': {'x': 1}}
Restored:  {'__dataclass__': 'whatever', '__module__': 'whatever', 'x': 1}

restored2 != original2 - the dict's shape and keys are silently rewritten with no error and no warning.

Neither original nor original2 is a contrived or malformed value. Both are ordinary dicts that could plausibly come from a tool's return value, an API response, or an ontology-like structure the agent's code builds and passes to a remote executor via send_variables().

Where this bites

RemotePythonExecutor.send_variables() (src/smolagents/remote_executors.py) calls SafeSerializer.dumps(variables, allow_pickle=self.allow_pickle) on whatever variables the agent sends to E2B/Docker/Modal/Blaxel. Any variable dict containing a "__type__" key is silently exposed to this collision the moment it crosses that boundary.

Expected vs actual

  • Expected: a plain dict round-trips through dumps()/loads() unchanged, regardless of what its keys are named, or to_json_safe() refuses to serialize an ambiguous shape up front with a clear error.
  • Actual: the round trip either raises an unrelated binascii.Error/KeyError-style exception from deep inside the "known type" decoding branch, or silently returns a structurally different object, depending only on the string value under the user's own "__type__" key.

Suggested direction

Reserve the marker with something a legitimate JSON-safe user dict cannot produce (e.g. wrap every plain dict in its own explicit envelope, or use a marker key unlikely enough to collide and validate the expected companion keys/types before trusting obj_type), so from_json_safe cannot mistake ordinary data for one of its own type tags.

Environment

Verified against src/smolagents/serialization.py on main just before filing (fetched via the GitHub API, confirmed byte-identical to the copy used for the repro above). Python 3.13, Windows - the bug is pure base64/json logic, not platform-specific.