Skip to content

Improvement: sort_models_by_id only sorts by the **first** character of the id #3817

Description

@ivyw960

backend/utils/model_name_utils.py:50-65:

def sort_models_by_id(model_list: List[dict]) -> List[dict]:
    if isinstance(model_list, list):
        model_list.sort(
            key=lambda m: str((m.get("id") if isinstance(m, dict) else m) or "")[:1].lower(),
            reverse=False
        )
    return model_list

[:1] keeps only the leading character. Anything else is sorted in an effectively arbitrary order (Python's sort is stable, so the order falls back to insertion order). For a list like:

[{"id": "qwen-7b"}, {"id": "qwen-72b"}, {"id": "qwen-1.5"}, {"id": "deepseek-r1"}]

…you get deepseek-r1, qwen-7b, qwen-72b, qwen-1.5 — every q* model collapses to a single equivalence class and their relative order is whatever the caller passed in.

Per the docstring ("Sort model list by the first letter of id"), the current behaviour technically matches the spec, but the spec itself is what the user/UX really wants challenged: in the model-selection UI in the frontend, this means models with similar prefixes are randomly shuffled.

Suggested improvement

Sort by the entire id with a natural sort to keep numeric suffixes in human order:

import re

def _natural_key(s: str):
    return [int(t) if t.isdigit() else t for t in re.split(r'(\d+)', s.lower())]

def sort_models_by_id(model_list):
    if isinstance(model_list, list):
        model_list.sort(key=lambda m: _natural_key(
            str((m.get("id") if isinstance(m, dict) else m) or "")
        ))
    return model_list

This gives deepseek-r1, qwen-1.5, qwen-7b, qwen-72b — predictable for users.

Category: H (improvement). Severity: Low.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions