Repository navigation
Open
Labels
Description
Please checkout the F.A.Q page before creating a bug ticket to make sure it is not already addressed.
Describe the bug
I cannot get the compare function to work properly on lists that are not 1st-level.
To Reproduce
Define the two following objects. They are identical except that:
- the
versionattribute went0.0.0->0.0.1 - in the second object has a number field removed in the
numberfield_setlist, and a new one added.UUID4s are used to track which one should be correlated with which, much likeids. in the example in the docs.
self_json= b'{"stringfield_set":[],"numberfield_set":[{"uuid":"fa0c87e8-01f5-43f5-8e63-24886f72ffd0","name":"field_1","address":1,"documentation":"","first_bit_offset":2,"size_in_bits":3,"is_signed":false,"is_lsb_left":false,"value_multiply_by":1.0,"value_divide_by":1.0,"value_increase_by":0.0,"value_unit":""},{"uuid":"332e5cfe-886d-41cc-b4d9-0fc1296b3ea0","name":"field_2","address":5,"documentation":"","first_bit_offset":5,"size_in_bits":6,"is_signed":false,"is_lsb_left":false,"value_multiply_by":1.0,"value_divide_by":1.0,"value_increase_by":0.0,"value_unit":""}],"enumfield_set":[],"version":"0.0.0","version_date":null,"version_commit":null,"ros_link":null,"documentation":"","definition":42,"information":[],"users":[]}'
other_json=b'{"stringfield_set":[],"numberfield_set":[{"uuid":"fa0c87e8-01f5-43f5-8e63-24886f72ffd0","name":"field_1","address":1,"documentation":"","first_bit_offset":2,"size_in_bits":3,"is_signed":false,"is_lsb_left":false,"value_multiply_by":1.0,"value_divide_by":1.0,"value_increase_by":0.0,"value_unit":""},{"uuid":"056429c5-812b-4f49-9aae-1b52bd40aacd","name":"field_3","address":7,"documentation":"","first_bit_offset":8,"size_in_bits":9,"is_signed":false,"is_lsb_left":false,"value_multiply_by":1.0,"value_divide_by":1.0,"value_increase_by":0.0,"value_unit":""}],"enumfield_set":[],"version":"0.0.1","version_date":null,"version_commit":null,"ros_link":null,"documentation":"","definition":42,"information":[],"users":[]}'make the iterable compare func
def field_diff_function(x, y, level=None):
try:
return x["uuid"] == y["uuid"]
except Exception:
raise CannotCompare() from Nonerun the compare:
diff_native = DeepDiff(
json.loads(self_json),
json.loads(other_json),
iterable_compare_func=field_diff_function,
ignore_order=True,
)the following diff is the result:
{
"values_changed": {
"root['numberfield_set'][1]['uuid']": {
"new_value": "056429c5-812b-4f49-9aae-1b52bd40aacd",
"old_value": "332e5cfe-886d-41cc-b4d9-0fc1296b3ea0"
},
"root['numberfield_set'][1]['name']": {
"new_value": "field_3",
"old_value": "field_2"
},
"root['numberfield_set'][1]['address']": {
"new_value": 7,
"old_value": 5
},
"root['numberfield_set'][1]['first_bit_offset']": {
"new_value": 8,
"old_value": 5
},
"root['numberfield_set'][1]['size_in_bits']": {
"new_value": 9,
"old_value": 6
},
"root['version']": {
"new_value": "0.0.1",
"old_value": "0.0.0"
}
}
}Expected behavior
As the fields should be matched using the uuid attribute, it should show that one has been added, and the other has been removed, and not that they changed.
OS, DeepDiff version and Python version (please complete the following information):
- Python Version: 3.9.10
- deepdiff version: 5.8.0
Additional context
I'm definitely not ruling out there's a problem somewhere between the chair and the keyboard
Reacted by Sep Dehpour
Activity
jvacek commentedon Apr 29, 2022
Upon further inspection, slapping a
print()statement inside the compare function shows that the function is actually never run.LizardBlizzard commentedon Jun 25, 2022
The
iterable_compare_funcfunction is not called when you setignore_orderparameter toTruefrom what I knowhavardthom commentedon Jan 17, 2023
Is this a bug or intended behaviour? It is not mentioned in the docs atleast.
I'm doing diffs on nested dicts with list of dicts which have
idkeys whereiterable_compare_funchelps comparing correct dicts to eachother, however the performance is not very good. Settingignore_order=Truehelps performance alot but wrong dicts are compared sometimes sinceiterable_compare_funcis not used.Omar-Abdul-Azeez commentedon Mar 3, 2023
I'm having the same issue and was about to create an issue...
I also done some more testing and debugging...
I think there are actually 2 bugs in this:
This is exclusive to
ignore_order=True. There's alen > 1check that prevents the fuction from being called.only if there are two or more different items in both lists does the function actually get called. ie. If there's 1 addition and 1 removal it doesn't call the function to check whether they are pairs or not.
I'm almost certain this is incorrect behaviour. I edited it locally and got this fixed (you can mimic by just introducing another element in both lists with differences). Instead of getting
key 'id' changed from a to b, I'm now gettingdict {'id': a} changed to dict {'id': b}ie. instead of considering k, v changes, the entire dicts are considered changes.https://github.com/seperman/deepdiff/blob/8ab1c8dbf19bb87177c10029a518051d6622532a/deepdiff/diff.py#L1094
Irrelevent of
ignore_order. The result of the custom compare function is ignored for nested dictsI've tested this on a list of dicts, inside each of these dicts is a key whose value is also a list of dicts. each dict has an id key. The example below sets
ignore_order=Truebut it works for both cases, just make sure the order is correct.I use this as compare function. Except the prints, taken straight from the docs.
I found out that it gets called 8 times. 4 times comparing the 2 top level dicts to each other. 4 times comparing the 2 nested dicts to each other (the ones in the dict with the same
id)the prints were as expected for both the top level and nested dicts. but the results are different...
'id': '1010'and'id': '1012'are not pairs and appeared initerable_item_addedanditerable_item_removedaccordingly. but nested dicts were paired with eachother and entered invalues_changedDESPITE the function returningFalsefor all pairing checks between them...Omar-Abdul-Azeez commentedon Mar 3, 2023
I did some more debugging and I found out that the dicts are reported as

iterable_item_removedand not asvalues_changedwhich led me to think the problem is in the this line
https://github.com/seperman/deepdiff/blob/8ab1c8dbf19bb87177c10029a518051d6622532a/deepdiff/diff.py#L309
up to this point all differences found are under
removedoraddedbut once this function gets called it changes tochangedfor some reason... taking a quick look inside that function there's a suspiciousmutual_add_removes_to_become_value_changes()call that makes me believe it doesn't respect the custom comparing function... I took a quick peek inside it and it seems to do exactly that. I think I need a break...https://github.com/seperman/deepdiff/blob/8ab1c8dbf19bb87177c10029a518051d6622532a/deepdiff/diff.py#L1551
seperman commentedon Nov 19, 2023
Hi @jvacek @Omar-Abdul-Azeez @havardthom @LizardBlizzard
Somehow this ticket was lost among other tickets I was paying attention to.
@Omar-Abdul-Azeez Thank you for diving into it already. I am going to take a look soon.
pascalstindt commentedon Jan 9, 2024
I think I'm running into the same issue. I'm using a custom
iterable_compare_funcfunction to match onid.All the results BUT ONE are correctly reported either as
iterable_item_addedoriterable_item_removed. The single incorrect one is reported asvalues_changedwith clearly a non matchingid.@seperman and @Omar-Abdul-Azeez have you been able to fix this? Thanks a lot.
amyasnikov commentedon Jul 5, 2024
Running into the same issue even on the latest (7.0.1) version.
@seperman it would be very nice if you could get back to this issue
cdiproperzio1 commentedon Sep 12, 2024
Having the same issue. Thanks!
bongi23 commentedon Feb 10, 2025
Similar issue here. I am comparing dicts with a field that contains list of dicts. Dicts in these lists have an "_id" field that I would like to use to compare items.
Example
I would like to get a value_change event for root[addresses][0][AddressName] but the comparator that returns True for a match on the _id filed does not get called.
I have both ignore_order and ignore_repetition set to True
dtorres-sf commentedon Apr 18, 2025
This issue can now be closed. This is fixed in deepdiff 8+, I believe with this PR:
#473
Tested the following:
Got this result:
Omar-Abdul-Azeez commentedon Jun 8, 2025
while the example in the original post may be fixed (I haven't tested), the one in my comment isn't. reposting for ease.
actually checking this example now yields no prints(???) and the result being value changes for root[0] (id:1010 with id:73) and for root[1] (id:73 with id:1012) which is clearly wrong.
and it is still due to this function not respecting the custom comparison.
https://github.com/seperman/deepdiff/blob/ecc823a3fdb9c094b7392ca40f859eab8cc8572a/deepdiff/diff.py#L1754
before the function is called there are additions for id: 73, 1012 and removals for id: 1010, and 73. I'm not sure if 73 should be paired beforehand or this function pairs them and finds out that the change is in a nested dict inside rather than the top level so I can't tell if it will even find out the difference correctly or not.
seperman commentedon Nov 16, 2026
Thanks everyone for the detailed reports, and sorry this sat for so long. Earlier PRs (#333, #454, #473) fixed parts of this, but the latest example in this thread still fails on
dev(5e7b5ef) and on the released 9.1.0. I traced it to two separate causes.Reproduction
Without
ignore_order: the top level is now correct (73is matched,1010removed,1012added). But the nested items are reported as changed even thoughcompare_funcreturnedFalsefor every nested pair:With
ignore_order=True:compare_funcis never called (0 calls), and records with different ids are paired by position:Cause 1: removed and added at the same index are merged into
values_changed_get_matching_pairshandles the nested list correctly:compare_funcrejects every pair, so each item is reported as removed or added. But when results are built,DeepDiff._get_view_results(diff.py:2243) runsTreeResult.mutual_add_removes_to_become_value_changes()(model.py:59) on the whole tree wheneverreport_repetitionis off. That step turns any removed and added pair at the same path into avalues_changed, overriding the compare function's decision.Cause 2: with
ignore_order, pairing is skipped when most items changed_diff_iterable_with_deephash(diff.py:1738) only pairs items when the changed fraction,(added + removed) / (len(t1) + len(t2) + 1), is at mostcutoff_intersection_for_pairs(default0.7). In this example no item is identical, so the fraction is 4/5 = 0.8. Pairing is skipped, andcompare_funcwith it, so everything is added or removed. Cause 1 then merges those by position into the wrongvalues_changed.Confirmation: passing
cutoff_intersection_for_pairs=1makescompare_funcget called and the top level comes out right. The nested items are then still merged by cause 1.Workaround until this is fixed: with
ignore_order=True, passcutoff_intersection_for_pairs=1when usingiterable_compare_func. The nestedvalues_changedfrom cause 1 remains.Candidate fixes (tried on a scratch copy of
dev, not merged)cutoff_intersection_for_pairsshortcut wheniterable_compare_funcis set. The compare function is an explicit instruction for how to pair items, and here the shortcut exists only to avoid the cost of pairing. Trade-off: large, mostly changed lists now pay O(n·m) compare calls.iterable_compare_funcis set, fixes this example in both modes, and the existing test suite passes. But it's too broad. Withignore_order=Trueit also changes lists the compare function doesn't handle (it raisesCannotCompare), e.g.["a", "b"]→["x", "y"]becomes added/removed instead ofvalues_changed. While applying aDeltabuilt from the result, it also logsUnable to get the item at root[0]['g'][0], although the round-trip still succeeds. The right fix is narrower: mark the levels that the compare function explicitly rejected, and have the merge skip only those.Two smaller things found while tracing:
ignore_orderpath, the compare function is called ascompare_func(t2_item, t1_item)with nolevel(distance.py:116). The in-order path callscompare_func(t1_item, t2_item, level). A compare function that is not symmetric, or that relies onlevel, behaves differently between the two modes.ignore_order, nested paths use the parent's t2 index (root[0]['g']); with it, the t1 index (root[1]['g']).Related: #665 (another
iterable_compare_funcmatching bug: an unmatched t2 item is dropped when it equals a matched one), #664 (planned key-based list matching, which builds on this code), and #290, #414, #489 (ignore_ordertogether withiterable_compare_func).