mlflow/mlflow · error

`max_length` must be non-negative, got ${self.max_length}.

Error message

`max_length` must be non-negative, got ${self.max_length}.

What it means

The ResponseLength built-in scorer validates its bounds in a pydantic model_validator at construction time. It throws when `max_length` is provided but is a negative number, because a maximum length cannot be below zero. This is fail-fast input validation so users learn about bad config before any evaluation runs.

Source

Thrown at mlflow/genai/scorers/builtin_scorers.py:3527

    """

    name: str = "response_length"
    min_length: int | None = None
    max_length: int | None = None
    unit: Literal["chars", "words"] = "chars"
    required_columns: set[str] = {"outputs"}
    description: str = "Check whether the output length is within specified bounds."

    @pydantic.model_validator(mode="after")
    def _validate_bounds(self):
        if self.min_length is None and self.max_length is None:
            raise ValueError(
                "ResponseLength requires at least one of `min_length` or `max_length`."
            )
        if self.min_length is not None and self.min_length < 0:
            raise ValueError(f"`min_length` must be non-negative, got {self.min_length}.")
        if self.max_length is not None and self.max_length < 0:
            raise ValueError(f"`max_length` must be non-negative, got {self.max_length}.")
        if (
            self.min_length is not None
            and self.max_length is not None
            and self.min_length > self.max_length
        ):
            raise ValueError(
                f"`min_length` ({self.min_length}) must be <= `max_length` ({self.max_length})."
            )
        return self

    @property
    def feedback_value_type(self) -> Any:
        return Literal["yes", "no"]

    @property
    def instructions(self) -> str:
        bounds = []
        if self.min_length is not None:

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Inspect the value passed as max_length and ensure it is >= 0
  2. If only an upper bound is unwanted, pass max_length=None and rely on min_length alone
  3. Trace where the bound is computed from and fix the source expression/config value

Example fix

// before
ResponseLength(max_length=-1)
// after
ResponseLength(max_length=500)  # or ResponseLength(min_length=10)
Defensive patterns

Strategy: validation

Validate before calling

def safe_max_length(v):
    if v is not None and v < 0:
        raise ValueError(f"max_length must be >= 0, got {v}")
    return v
ResponseLength(max_length=safe_max_length(max_len))

Type guard

def is_valid_max_length(v) -> bool:
    return v is None or (isinstance(v, int) and v >= 0)

Prevention

When it happens

Trigger: Constructing ResponseLength(max_length=<negative int>) directly or via mlflow.genai.evaluate(scorers=[ResponseLength(...)]), e.g. ResponseLength(max_length=-1) or a computed bound that evaluated negative.

Common situations: Computing the bound from a variable/config that defaults to -1 or 0-1 on an empty collection (e.g. max(len(x) for x in []) guarded with -1); sign-flipped min/max variables; YAML/env config parsed into negative numbers.

Related errors


AI-assisted analysis of mlflow/mlflow@6a27f2decc (2026-08-29). Data as JSON: /api/errors/d277bcfcbd3efdd9. Report an issue: GitHub.