fix: add regression test for shallow copy boundary

Add test documenting that shallow copy isolates top-level key
reassignment but not nested dict mutations (expected behavior).
This commit is contained in:
Ryan Crabbe 2026-02-21 11:22:03 -08:00
parent 5ddb335a31
commit 89ead20293

View file

@ -1643,3 +1643,27 @@ def test_logging_messages_isolated_tool_calls():
# Reassign tool_calls on the original dict
messages[0]["tool_calls"] = [{"id": "call_NEW", "type": "function", "function": {"name": "other", "arguments": "{}"}}]
assert logging_obj.messages[0]["tool_calls"] == original_tool_calls
def test_shallow_copy_nested_metadata_mutation_shared():
"""Top-level key reassignment is isolated; nested dict mutation is shared (expected)."""
original_messages = [
{"role": "user", "content": "test", "metadata": {"key": "value"}}
]
logging_obj = LitellmLogging(
model="gpt-4",
messages=original_messages,
stream=False,
call_type="completion",
start_time=time.time(),
litellm_call_id="12345",
function_id="1245",
)
# Top-level key reassignment IS isolated by shallow copy
original_messages[0]["content"] = "CHANGED"
assert logging_obj.messages[0]["content"] == "test"
# Nested dict mutation is NOT isolated (shared reference) — expected
original_messages[0]["metadata"]["key"] = "modified"
assert logging_obj.messages[0]["metadata"]["key"] == "modified"