Skip to content

ListField validation errors are inconsistent with ListSerializer #7279

Closed

Description

@markopy

Validation errors when using ListSerializer are returned as a list with non-failing entries empty:

[
{},
{"foo": ["error"]},
{},
{"foo": ["another error"]}
]

When using ListField the same information is unexpectedly returned as a dictionary:

{
"1": {"foo": ["error"]},
"3": {"foo": ["another error"]}
}

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.

Activity

  1. sarathak commented on Jul 13, 2021

    @sarathak
    Contributor

    How to replicate it ?

    i tried following

        class TestSerializer(serializers.Serializer):
            foo = serializers.ListField(required=False)
    
        class ObjectListSerializer(serializers.ListSerializer):
            child = TestSerializer()
        input_data = [
            {},
            {"foo": ["error"]},
            {},
            {"foo": ["another error"]}
        ]
        serializer = ObjectListSerializer(data=input_data)
  2. stale commented on Apr 28, 2022

    @stale

    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.

  3. bost-h commented on Nov 18, 2022

    @bost-h

    I agree with the original issue, ListField and ListSerializer errors seem inconsistent to me.

    How to replicate it ?

    Does this help ?

    class NestedTestSerializer(serializers.Serializer):
        nested_serializer_field = serializers.CharField()
    
    
    class TestSerializer(serializers.Serializer):
        list_serializer = NestedTestSerializer(many=True)
        list_field = serializers.ListField(child=serializers.DictField(allow_empty=False))
    
    
    def test_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."]}]
        assert isinstance(serializer.errors["list_serializer"], list)
        # list_field error is: {"0": ["This dictionary may not be empty."]}
        assert isinstance(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:

    • It seems to be the intention of Improve composite field child errors #5655
    • Having a dict of errors address the case where the error isn't for the first item (e.g. {"1": ["Error"]} vs [null, ["Error"]
    • I think it means the clients can be sure list are always list of messages, and there is no need to dive deeper / check the type of the items.
  4. gilman88 commented on Mar 11, 2024

    @gilman88

    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?)

  5. auvipy commented on Mar 11, 2024

    @auvipy
    Collaborator

    a document update and later updating or aligning the errors would be the go. any takers?

  6. gilman88 commented on Apr 2, 2024

    @gilman88

    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.

  7. sevdog commented on Feb 14, 2025

    @sevdog
    Contributor

    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.

  8. stale commented on Apr 26, 2025

    @stale

    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.

  9. stale commented on Jun 27, 2025

    @stale

    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.

  10. ulgens commented on Jun 27, 2025

    @ulgens
    Contributor

    I think this is still relevant and shouldn't be marked as stale.

  11. ulgens commented on Oct 20, 2025

    @ulgens
    Contributor

    @Stale Not completed.

  12. reopened this on Nov 3, 2025
  13. p-r-a-v-i-n commented on Nov 9, 2025

    @p-r-a-v-i-n
    Contributor

    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.

    could @auvipy confirm this direction ?

  14. auvipy commented on Nov 9, 2025

    @auvipy
    Collaborator

    have to check again, but this might be a good starting point #7279 (comment)

  15. added this to the 3.18 milestone on Jul 25, 2026
  16. vnagendra commented on Aug 9, 2026

    @vnagendra

    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

    1. Monkey patching DRF in my app
    2. 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.

  17. adamchainz commented on Oct 2, 2026

    @adamchainz
    Contributor

    I suggest 3.19 allow a global setting which reverts back to the old behavior and that should be the default.

    #10027 added that global setting, LIST_SERIALIZER_ERRORS_AS_DICT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions