sgl-project/sglang · error · ValueError

Length mismatch: {details}

Error message

Length mismatch: {details}

What it means

_check_equal_lengths raises this when parallel lists passed as keyword arguments have differing lengths. It is used by dataclass __post_init__ / _validate_fields to enforce that field lists that are zipped together (names, tensors, etc.) stay in sync, and reports each list's name=length pair.

Source

Thrown at python/sglang/srt/debug_utils/comparator/utils.py:20

import functools
import re
from pathlib import Path
from typing import TYPE_CHECKING, Callable, Generic, Optional, Tuple, TypeVar

import torch
from pydantic import BaseModel, ConfigDict

_T = TypeVar("_T")
_U = TypeVar("_U")


def _check_equal_lengths(**named_lists: list) -> None:
    lengths: dict[str, int] = {name: len(lst) for name, lst in named_lists.items()}
    unique: set[int] = set(lengths.values())
    if len(unique) > 1:
        details: str = ", ".join(f"{name}={length}" for name, length in lengths.items())
        raise ValueError(f"Length mismatch: {details}")


def auto_descend_dir(directory: Path, label: str) -> Path:
    """If directory has no .pt files but exactly one subdirectory does, descend into it.

    Raises ValueError when the layout is ambiguous (>=2 subdirs with .pt)
    or when no .pt data is found at all.
    """
    if any(directory.glob("*.pt")):
        return directory

    candidates: list[Path] = [
        sub for sub in directory.iterdir() if sub.is_dir() and any(sub.glob("*.pt"))
    ]

    if len(candidates) >= 2:
        names: str = ", ".join(sorted(c.name for c in candidates))
        raise ValueError(

View on GitHub (pinned to 0132848349)

Solutions

  1. Audit the reported name=length pairs and fix the source that produced the short/long list
  2. If filtering one list, apply the same mask to all parallel lists
  3. Add an assert len(a)==len(b) at the point the lists are built

Example fix

# before
Record(names=['a','b','c'], tensors=[t0,t1])
# after
Record(names=['a','b','c'], tensors=[t0,t1,t2])
Defensive patterns

Strategy: type-guard

Validate before calling

def assert_parallel_lists(**lists):
    lengths = {k: len(v) for k, v in lists.items()}
    assert len(set(lengths.values())) == 1, lengths

Type guard

def lengths_equal(*lists) -> bool:
    return len({len(l) for l in lists}) <= 1

Try / catch

try:
    rec = Record(names=names, tensors=tensors)
except ValueError as e:
    print(e); raise

Prevention

When it happens

Trigger: Constructing a comparator dataclass with e.g. names=[...3 items...] and tensors=[...2 items...]; any _check_equal_lengths(a=[...], b=[...]) call where lengths differ.

Common situations: Building the record from partially-populated sources, filtering one list but not its sibling, or appending to one list during a loop but not the other.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/1098c1a982bdbb55. Report an issue: GitHub.