neuralqx.experimental.utils.struct.io module¶
Public serialization APIs for Struct and registered pytree types.
The implementation lives in neuralqx.utils.struct._internals.io.
This module intentionally exposes a compact, stable public surface while hiding
wire-format and storage details behind internal helpers.
- Supported use cases:
Convert runtime values into Python-inspection views (
to_python()).Build in-memory portable payloads (
to_state_dict()).Restore from in-memory payloads (
from_state_dict()).Persist payloads to directory or
.zipbundles (export()).Load persisted bundles back into objects (
load()).
- Struct I/O uses a two-part representation:
manifest: JSON-friendly structural metadata and object graph encoding.
arrays: compressed array blobs stored separately for efficiency.
- to_python(value, *, include_opaque=True)¶
Convert a possibly nested value into Python-native inspection form.
- This helper recursively converts:
Struct objects into
dictform,registered pytrees into explicit structural views,
JAX/NumPy arrays into NumPy arrays.
- Parameters:
- Return type:
- Returns:
A Python-friendly representation preserving overall structure.
Notes
to_pythonis designed for introspection/debugging, not for persistence. For portable storage useto_state_dict()orexport().
- to_state_dict(obj)¶
Encode an object graph into a portable state-dict payload.
- The returned mapping contains:
version: schema version integer.manifest: JSON-serialisable structural manifest.arrays: array metadata (shape/dtype).array_data: in-memory NumPy arrays.
- Parameters:
obj (
Any) – Root object to encode. May be a Struct, a registered pytree class, or nested built-in containers of supported types.- Return type:
- Returns:
In-memory transport format suitable for
from_state_dict().- Raises:
SerializationError – if unsupported values are encountered and no adapter is registered.
- from_state_dict(payload, *, expected_cls=None)¶
Reconstruct an object graph from
to_state_dict()output.- Parameters:
- Return type:
- Returns:
Reconstructed object graph.
- Raises:
SerializationError – If payload schema/version is invalid, required arrays are missing, class resolution fails, adapters are unavailable, or type guard check fails.
- export(obj, path, *, overwrite=False)¶
Export object state to a directory bundle or
.ziparchive.- Directory/zip output always includes:
manifest.jsonstructural payloadarrays.npzcompressed array buffer
- load(path, *, expected_cls=None)¶
Load a previously exported state bundle.
- Parameters:
- Return type:
- Returns:
Decoded object graph.
- Raises:
FileNotFoundError – If
pathdoes not exist.SerializationError – If bundle files are malformed, schema checks fail, or type resolution cannot be completed.