nodejs/node · error · FilterArgumentError
You can only sort by either "key" or "value"
Error message
You can only sort by either "key" or "value"
What it means
The dictsort filter sorts the items of a mapping. Its `by` argument selects the sort key: 'key' (pos 0) or 'value' (pos 1). Any other string is meaningless to the filter and is rejected with FilterArgumentError.
Source
Thrown at tools/inspector_protocol/jinja2/filters.py:230
{% for item in mydict|dictsort %}
sort the dict by key, case insensitive
{% for item in mydict|dictsort(reverse=true) %}
sort the dict by key, case insensitive, reverse order
{% for item in mydict|dictsort(true) %}
sort the dict by key, case sensitive
{% for item in mydict|dictsort(false, 'value') %}
sort the dict by value, case insensitive
"""
if by == 'key':
pos = 0
elif by == 'value':
pos = 1
else:
raise FilterArgumentError(
'You can only sort by either "key" or "value"'
)
def sort_func(item):
value = item[pos]
if not case_sensitive:
value = ignore_case(value)
return value
return sorted(value.items(), key=sort_func, reverse=reverse)
@environmentfilter
def do_sort(
environment, value, reverse=False, case_sensitive=False, attribute=None
):View on GitHub (pinned to 1b2de5e052)
Solutions
- Use by='key' (the default) or by='value' in the dictsort call.
- If you need to sort by a nested field, first map items to that field then sort, instead of passing the field name to dictsort.
Example fix
{# before #}
{{ mydict|dictsort(false, 'name') }}
{# after #}
{{ mydict|dictsort(false, 'value') }} Defensive patterns
Strategy: validation
Validate before calling
ALLOWED = ('key', 'value')
if by not in ALLOWED:
raise ValueError("dictsort `by` must be one of %r" % (ALLOWED,)) Type guard
def is_dictsort_by(value: str) -> bool:
return value in ('key', 'value') Try / catch
from jinja2.exceptions import FilterArgumentError
try:
out = env.call_filter('dictsort', value, args=(False, by))
except FilterArgumentError:
by = 'key'
out = env.call_filter('dictsort', value, args=(False, by)) Prevention
- Whitelist the `by` argument in any code that builds dictsort dynamically.
- Keep dictsort and attribute-based filters (map/selectattr) distinct in templates.
When it happens
Trigger: Invoking {{ mydict|dictsort(case_sensitive, by) }} where `by` is anything other than the literal strings 'key' or 'value' (e.g. 'name', 'field', 'attribute').
Common situations: Confusing dictsort with other filters (attr/map) that take an attribute name; copy-pasting a sort field name from another part of the template.
Related errors
- can't handle positional and keyword arguments at the same ti
- method must be common, ceil or floor
- Unexpected keyword argument %r
- map requires a filter argument
- Missing parameter for attribute name
AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13).
Data as JSON: /api/errors/f0045810eb3cd198.
Report an issue: GitHub.