You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ListField validation errors are inconsistent with ListSerializer #7279
The current ListField error handling was implemented in #5655 along with DictField, which explains this behavior but I think it would be more obvious for ListField to behave like ListSerializer and return a list instead.
Reacted by Nat Gordon, YanyangChen, Haakon Thor Brunstad, Hugo Bost and Devid
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I agree with the original issue, ListField and ListSerializer errors seem inconsistent to me.
How to replicate it ?
Does this help ?
classNestedTestSerializer(serializers.Serializer):
nested_serializer_field=serializers.CharField()
classTestSerializer(serializers.Serializer):
list_serializer=NestedTestSerializer(many=True)
list_field=serializers.ListField(child=serializers.DictField(allow_empty=False))
deftest_errors_inconsistencies():
serializer=TestSerializer(
data={
"list_serializer": [{"nested_serializer_field": ""}],
"list_field": [{}],
}
)
serializer.is_valid()
# list_serializer error is: [{"nested_serializer_field": ["This field may not be blank."]}]assertisinstance(serializer.errors["list_serializer"], list)
# list_field error is: {"0": ["This dictionary may not be empty."]}assertisinstance(serializer.errors["list_field"], dict)
As you can see, both list_serializer and list_field expects a list, but in case of validation errors ListField returns a dict of errors, and ListSerializer returns a list of errors.
It is caused by ListField.run_child_validation which uses errors = OrderedDict() but ListSerializer.to_internal_value has errors = [].
It is debatable if the correct error type is a list or a dict. On our side, we have chosen to use a dict in all cases because:
I am also in the camp that returning a dict of errors is more likely to be useful when actually handling these errors and/or debugging, I also think it's more likely to be the behavior that developers are expecting.
However in the event that consensus cannot be reached I would propose that the documentation be updated to make this difference clear between ListSerializer (which you get by default with the many=True flag) and ListField. Especially since passing a many=True flag is strongly encouraged whenever you are dealing with nested serializers. I'm sure there are other important differences as well but I haven't dug into them (maybe the create/update behaviour?)
I don't really have enough time, also I don't really agree with the style the documentation is written in so I would inevitably fall down a deep rabbit hole trying to update them.
This is certainly be a breaking change with the current behaviour. However, IMHO, it is worth it for the sake of simplicity and uniformity.
For the API user the ListField and ListSerializer should behave the same because it is possible to mimic ListSerializer by using ListField(child=Serializer()) or by using ListField(child=DictField()). These should be just internal choices and should not have any effect at the output level, because they all undergo the same use-case: validate a list of JSON elements.
Before proceeding with the implementation a decision should be done regarding which output format will be kept and which will be removed.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Before proceeding with the implementation a decision should be done regarding which output format will be kept and which will be removed.
We should keep the Dictionary Format and remove the List Format for consistency.
The dictionary is better because it's cleaner and more efficient, it only lists the items that actually have errors, instead of cluttering the response with empty entries for every valid item.
This was probably a good idea when the discussion started. However the implementation turned out to be a one way transform where if someone wants to migrate to 3.18, they must rewrite all the Frontend contracts to follow this. For a mature product like DRF, we should've planned for a backward compatible setting. Now the options are
Monkey patching DRF in my app
Overriding every call to DRF with a custom serializer manually where we are using a ListSerializer with many=True
In order to help people not discover this public API breaking change accidentally like we did - I suggest 3.19 allow a global setting which reverts back to the old behavior and that should be the default.
Activity
sarathak commentedon Jul 13, 2021
How to replicate it ?
i tried following
stale commentedon Apr 28, 2022
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
bost-h commentedon Nov 18, 2022
I agree with the original issue,
ListFieldandListSerializererrors seem inconsistent to me.Does this help ?
As you can see, both
list_serializerandlist_fieldexpects a list, but in case of validation errorsListFieldreturns a dict of errors, andListSerializerreturns a list of errors.It is caused by
ListField.run_child_validationwhich useserrors = OrderedDict()butListSerializer.to_internal_valuehaserrors = [].It is debatable if the correct error type is a
listor adict. On our side, we have chosen to use adictin all cases because:{"1": ["Error"]}vs[null, ["Error"]gilman88 commentedon Mar 11, 2024
I am also in the camp that returning a dict of errors is more likely to be useful when actually handling these errors and/or debugging, I also think it's more likely to be the behavior that developers are expecting.
However in the event that consensus cannot be reached I would propose that the documentation be updated to make this difference clear between ListSerializer (which you get by default with the many=True flag) and ListField. Especially since passing a many=True flag is strongly encouraged whenever you are dealing with nested serializers. I'm sure there are other important differences as well but I haven't dug into them (maybe the create/update behaviour?)
auvipy commentedon Mar 11, 2024
a document update and later updating or aligning the errors would be the go. any takers?
gilman88 commentedon Apr 2, 2024
I don't really have enough time, also I don't really agree with the style the documentation is written in so I would inevitably fall down a deep rabbit hole trying to update them.
sevdog commentedon Feb 14, 2025
This is certainly be a breaking change with the current behaviour. However, IMHO, it is worth it for the sake of simplicity and uniformity.
For the API user the
ListFieldandListSerializershould behave the same because it is possible to mimicListSerializerby usingListField(child=Serializer())or by usingListField(child=DictField()). These should be just internal choices and should not have any effect at the output level, because they all undergo the same use-case: validate a list of JSON elements.Before proceeding with the implementation a decision should be done regarding which output format will be kept and which will be removed.
stale commentedon Apr 26, 2025
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
stale commentedon Jun 27, 2025
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
ulgens commentedon Jun 27, 2025
I think this is still relevant and shouldn't be marked as stale.
ulgens commentedon Oct 20, 2025
@Stale Not completed.
p-r-a-v-i-n commentedon Nov 9, 2025
We should keep the
Dictionary Formatand remove theList Formatfor consistency.The dictionary is better because it's cleaner and more efficient, it only lists the items that actually have errors, instead of cluttering the response with empty entries for every valid item.
could @auvipy confirm this direction ?
auvipy commentedon Nov 9, 2025
have to check again, but this might be a good starting point #7279 (comment)
vnagendra commentedon Aug 9, 2026
This was probably a good idea when the discussion started. However the implementation turned out to be a one way transform where if someone wants to migrate to 3.18, they must rewrite all the Frontend contracts to follow this. For a mature product like DRF, we should've planned for a backward compatible setting. Now the options are
many=TrueIn order to help people not discover this public API breaking change accidentally like we did - I suggest 3.19 allow a global setting which reverts back to the old behavior and that should be the default.
adamchainz commentedon Oct 2, 2026
#10027 added that global setting,
LIST_SERIALIZER_ERRORS_AS_DICT.