mixle.utils.serialization module¶
Safe JSON serialization helpers for mixle distribution and estimator objects.
The legacy load_models(eval_string) path reconstructed models by executing
their repr strings. This module instead serializes distribution state as a
small tagged JSON value graph and reconstructs only classes registered from
mixle’s own distribution modules.
- exception SerializationError[source]
Bases:
ValueErrorRaised when an object cannot be serialized or decoded safely.
- deserialization_is_trusted()[source]
Whether the current context has opted into code-executing deserialization.
Checked by
mixle.models._neural_serialbefore unpickling an embedded torch module. Not needed for the ordinary registry-based path (from_serializable()on a payload with no embedded module blob), which never executes code regardless of this flag.- Return type:
- trusted_deserialization()[source]
Permit code-executing deserialization (an embedded torch module) for this
withblock.Only enter this around an artifact whose SOURCE you trust: within the block, decoding a NeuralLeaf-family object (or anything else that persists live code/objects via pickle) executes that pickle’s
__reduce__/__setstate__arbitrarily, exactly likepickle.loadon an untrusted file. Nested/re-entrant use is safe (the gate stays open until the outermost block exits).The authority is deliberately NOT inherited by child work.
contextvarsare copied into child asyncio tasks and into an explicitly copied context on another thread, so ambient propagation would let a task spawned inside this block keep decoding trusted artifacts for its whole lifetime – including after the parent block exited._TrustScopetherefore pins the owning thread and task, anddeserialization_is_trusted()reportsFalseanywhere but that one synchronous decode context. Open the block again in the child if it genuinely needs the authority.- Return type:
Iterator[None]
- register_serializable_class(cls, type_id=None)[source]
Register a class that may be reconstructed from serialized state.
Deserialization never imports a class named in the payload. The class must already be present in this registry, which is populated from mixle package modules by
ensure_pysp_serialization_registry.
- register_serializable_callable(fn, callable_id=None)[source]
Register a callable that may appear inside a serialized distribution.
This is intentionally explicit. Arbitrary lambdas or local functions cannot be made safe by JSON alone; callers that need SelectDistribution-like routing should register a stable process-local callable id.
- serializable_class_ids()[source]
Return the registered class ids, primarily for diagnostics/tests.
- serializable_schema_records(profile='full')[source]
Return the declared per-type schema records for one generation profile.
baseexcludes torch-backedmixle.modelstypes.fullincludes every type available after the optional serialization-module discovery. A generator must separately attest that the profile’s dependency inventory is installed before treating this record as complete.
- ensure_pysp_serialization_registry()[source]
Populate the closed registry of mixle classes that can be decoded.
- Return type:
None
- to_serializable(value)[source]
Convert a mixle model/value to a JSON-compatible tagged value.
- from_serializable(payload)[source]
Decode a value produced by
to_serializable.
- to_json(value, **kwargs)[source]
Serialize a mixle model/value to strict JSON.
- from_json(text)[source]
Deserialize a value produced by
to_json.
- rebuild_through_init(instance, state, *, parameters, label)[source]
Rebuild
instancefrom a serialized parameter state by re-running its own__init__.The counterpart of a
__pysp_getstate__that writes constructor parameters only: derived caches are recomputed rather than persisted, so an artifact does not depend on how the caches were laid out when it was written, and every constructor invariant re-runs on load. Families whose state carried derived caches, or whose constructor keyword differs from the attribute name, could not be read back at all –to_jsonproduced textfrom_jsonrefused, andModel.deployfell back to a code-executing pickle for them (P05-F17, P09-F05, P10-F05).parametersnames the constructor keywords in the order they are written; each must be present instate, and nothing else may be.