Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.2k
merge() method to merge two instances of the same model#3416
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
First, my motivating use-case. I have two sources of a model instance: a default that an application provides, and an override which gets parsed from a file (if it exists). As a library writer, to merge these two instances, I will have to implement some recursive merger that works on nested dictionaries so that my users don't have to manually implement merging in their application. Python's
This would work great for my use-case. For inspiration, some related Python merging libraries that I found:
|
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 3 comments 5 replies
-
I have a similar use case here, but TBH, I don't see the need for a |
BetaWas this translation helpful?Give feedback.
All reactions
👎 1
-
that would just overwrite the fields with the latest model. merge might mean addition of int fields, and things like that. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
Anaddition of int fields is well supported by python built-in functionality (e.g. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I think they meant literal addition of int values, e.g. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Oh,that kind of addition! OK, didn't think of that - I thought ofadding model fields with integer type. Your code sample is not a merge, though; in fact, it's not even valid python - missing quotes for starters, but more importantly,
Also, why would anyone try to add numbers in model instance field value - and expect the result to be the sum ? Anyway, recipe for merging model instances: Turn them into dicts using |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I was presenting it to communicate the idea, not as a literal Python example.
Here's some valid Python thatdoes behave exactly in this way, to motivate: >>>fromcollectionsimportCounter>>>Counter({"a":5})+Counter({"a":6})Counter({'a':11}) |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Here's an example that's working well for my use-case: fromtypingimportTypeVarfrompydanticimportBaseModelfromdeepmergeimportalways_mergerT=TypeVar("T",bound=BaseModel)defmerge_pydantic_models(base:T,nxt:T)->T:"""Merge two Pydantic model instances. The attributes of 'base' and 'nxt' that weren't explicitly set are dumped into dicts using '.model_dump(exclude_unset=True)', which are then merged using 'deepmerge', and the merged result is turned into a model instance using '.model_validate'. For attributes set on both 'base' and 'nxt', the value from 'nxt' will be used in the output result. """base_dict=base.model_dump(exclude_unset=True)nxt_dict=nxt.model_dump(exclude_unset=True)merged_dict=always_merger.merge(base_dict,nxt_dict)returnbase.model_validate(merged_dict) |
BetaWas this translation helpful?Give feedback.
All reactions
-
I've come across use cases like this many times, but I think it should stay outside the scope of Pydantic. Generally speaking, there is no "one correct way" to merge fields of a model. Lists are probably fine, sure, but most everything else will conflict. |
BetaWas this translation helpful?Give feedback.