ML Foundations
Copy nested sample metadata without sharing the edited list
A new dictionary can still share nested lists with the original record. Copy the mutable field that a transformation will edit.
Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
A copied record is not necessarily an isolated record
A data transformation may begin with `record.copy()` to avoid changing the input sample. That creates a new outer dictionary, but it is a shallow copy: references to objects inside it are retained. If a value is a mutable list, editing that list through the copy can also change what the original record sees. Python documents this difference between shallow and deep copying. [1, 2]
This matters when a training-data transform adds a label or modifies an annotation list. Replacing a top-level scalar field and appending to a nested list are different operations. The former changes an entry in the new dictionary; the latter can act on an object still shared by both records.
Copy the field the transformation will mutate
The first half of the example copies the dictionary and appends `dog` through the copied record. The original label list then contains both labels. The second half starts again from a fresh original, copies the outer dictionary, and separately copies its `labels` list before appending. The original remains `['cat']`; the transformed copy becomes `['cat', 'dog']`. [1, 2]
The identity check is intentionally about the nested list, not just the dictionary. It confirms that these two list containers are distinct. Their elements are strings in this fixture, so the example does not require another level of mutable-object copying.
Executed with Python 3.13.5; bounded synthetic example. Exact stdout and source-byte SHA-256 are preserved in private evidence.
original = {"sample_id": 7, "labels": ["cat"]}
shallow = original.copy()
shallow["labels"].append("dog")
print("after shallow edit:", original["labels"])
original = {"sample_id": 7, "labels": ["cat"]}
isolated = original.copy()
isolated["labels"] = original["labels"].copy()
isolated["labels"].append("dog")
print("original after isolated edit:", original["labels"])
print("edited copy:", isolated["labels"])
print("same nested list:", original["labels"] is isolated["labels"])
Recorded output
after shallow edit: ['cat', 'dog'] original after isolated edit: ['cat'] edited copy: ['cat', 'dog'] same nested list: False
Make the ownership boundary explicit
For this record shape, explicitly copying the one mutable field documents the intended ownership boundary. Keep tests that inspect the original after the transformation, rather than only asserting the transformed result. A transformation can produce a plausible output while still changing an input that later code expects to reuse.
This targeted approach is not a general deep copy. If the labels themselves contain dictionaries or mutable objects, a list copy retains references to those elements. Python's copy module describes recursive copying and also notes that copying everything can duplicate more than intended. Choose the boundary based on the actual data model rather than treating either shallow or deep copying as an automatic universal fix. [1]