Artifact formats and safe loading
Quick answer
Model serialization stores the information needed to reconstruct predictions: learned parameters, architecture or operator graph, preprocessing, output contract and version metadata. Choose a format that the receiving runtime explicitly supports. Examples use standard-library Python; optional ONNX execution is not tested. ByteMind execution is unverified.
Round trip numerical weights
import jsontext=json.dumps({"version":1,"weights":[2,3],"bias":1},sort_keys=True)a=json.loads(text)print(sum(w*x for w,x in zip(a["weights"],[4,5]))+a["bias"])Expected output (locally tested): 24. A plain data representation reconstructs a linear prediction.
How it works
A Python pickle or joblib file can execute code during loading; never deserialize an unknown artifact. Even a restricted weights loader is not a substitute for trusted provenance, resource limits and isolation. Framework weight formats often require a separately defined architecture; graph formats describe computation and may include weights. A small linear model can instead use JSON containing a version, coefficients and intercept. Validate exact keys, types, lengths, numeric finiteness and allowed operators before interpreting it. A digest compared to a trusted manifest detects a changed byte sequence, but an attacker who can change both artifact and manifest can replace both. Use authenticated release metadata, least privilege, atomic promotion and rollback. Never treat JSON as intrinsically trustworthy or use eval to interpret fields. The local lab admits only one explicit linear operator and bounded values; it never loads executable checkpoints.
Detect a modified artifact
import hashliboriginal=b'model-v1'changed=b'model-v2'print(hashlib.sha256(original).digest()==hashlib.sha256(changed).digest())Expected output (locally tested): False. The stored digest must come from a trusted source.
Common mistakes
Do not infer trust from a file extension or assume a matching hash proves authorization. Do not silently accept a newer incompatible version.
Practice and next steps
Change one coefficient in the artifact and verify the digest check fails; then use an updated digest with a disallowed operator and verify schema rejection.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Artifact formats and safe loading — knowledge check
Apply artifact formats and safe loading to small examples and debugging scenarios.
What is unsafe to load from unknown sources?
What does a trusted digest comparison check?
What may weights alone omit?
How should an unknown operator be handled?
What should approve a release?