deepset-ai/haystack · error · ValueError

The value of parameter <sort_order> must be 'ascending' or '

Error message

The value of parameter <sort_order> must be 'ascending' or 'descending', but is currently set to '{sort_order}'.
Change the <sort_order> value to 'ascending' or 'descending' when initializing the MetaFieldRanker.

What it means

MetaFieldRanker._validate_params rejects any sort_order other than 'ascending' or 'descending'. Sort order determines whether the meta field is ranked smallest-first or largest-first; unknown values raise ValueError in both __init__ and run.

Source

Thrown at haystack/components/rankers/meta_field.py:139

            raise ValueError(f"top_k must be > 0, but got {top_k}")

        if weight < 0 or weight > 1:
            raise ValueError(
                f"Parameter <weight> must be in range [0,1] but is currently set to '{weight}'.\n'0' disables sorting "
                "by a meta field, '0.5' assigns equal weight to the previous relevance scores and the meta field, and "
                "'1' ranks by the meta field only.\nChange the <weight> parameter to a value in range 0 to 1 when "
                "initializing the MetaFieldRanker."
            )

        if ranking_mode not in ["reciprocal_rank_fusion", "linear_score"]:
            raise ValueError(
                "The value of parameter <ranking_mode> must be 'reciprocal_rank_fusion' or 'linear_score', but is "
                f"currently set to '{ranking_mode}'.\nChange the <ranking_mode> value to 'reciprocal_rank_fusion' or "
                "'linear_score' when initializing the MetaFieldRanker."
            )

        if sort_order not in ["ascending", "descending"]:
            raise ValueError(
                "The value of parameter <sort_order> must be 'ascending' or 'descending', "
                f"but is currently set to '{sort_order}'.\n"
                "Change the <sort_order> value to 'ascending' or 'descending' when initializing the "
                "MetaFieldRanker."
            )

        if missing_meta not in ["drop", "top", "bottom"]:
            raise ValueError(
                "The value of parameter <missing_meta> must be 'drop', 'top', or 'bottom', "
                f"but is currently set to '{missing_meta}'.\n"
                "Change the <missing_meta> value to 'drop', 'top', or 'bottom' when initializing the "
                "MetaFieldRanker."
            )

        if meta_value_type not in ["float", "int", "date", None]:
            raise ValueError(
                "The value of parameter <meta_value_type> must be 'float', 'int', 'date' or None but is "
                f"currently set to '{meta_value_type}'.\n"

View on GitHub (pinned to e318778c9b)

Solutions

  1. Use exactly 'ascending' or 'descending' (lowercase, full word).
  2. Normalize shorthand at the call site: map 'asc'->'ascending', 'desc'->'descending'.
  3. Add a config-level choice validation before constructing the ranker.

Example fix

// before
ranker = MetaFieldRanker(sort_order="asc")
// after
ranker = MetaFieldRanker(sort_order="ascending")
Defensive patterns

Strategy: validation

Validate before calling

VALID_SORT_ORDERS = ("ascending", "descending")
if sort_order not in VALID_SORT_ORDERS:
    raise ValueError(f"sort_order must be one of {VALID_SORT_ORDERS}, got {sort_order!r}")

Type guard

def is_valid_sort_order(v) -> bool:
    return v in ("ascending", "descending")

Try / catch

try:
    ranker = MetaFieldRanker(sort_order=order)
except ValueError:
    order = {"asc": "ascending", "desc": "descending"}.get(order, "descending")
    ranker = MetaFieldRanker(sort_order=order)

Prevention

When it happens

Trigger: MetaFieldRanker(sort_order='asc'), 'Ascending', 'desc', or any other string; the literal check is case-sensitive and requires the full words.

Common situations: Using SQL-style abbreviations ('asc'/'desc') in configs; UI code passing arbitrary sort strings; locale/case differences when the value is loaded from a file.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/89fac87da64b8b1b. Report an issue: GitHub.