jackwener/OpenCLI · error · ArgumentError

hf models sort must be one of ${SORT_OPTIONS.join(', ')}

Error message

hf models sort must be one of ${SORT_OPTIONS.join(', ')}

What it means

ArgumentError thrown at clis/hf/models.js:31 when the --sort argument, after lowercasing and alias mapping (lastmodified→last_modified, createdat→created_at), is not one of SORT_OPTIONS: downloads, likes, trending, created_at, last_modified. The library validates the sort key client-side before building the https://huggingface.co/api/models request to avoid sending an invalid query to the API.

Source

Thrown at clis/hf/models.js:31

    site: 'hf',
    name: 'models',
    access: 'read',
    description: 'Top Hugging Face models (downloads / likes / trending / freshness).',
    domain: 'huggingface.co',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'sort', type: 'string', default: 'downloads', help: `Sort key: ${SORT_OPTIONS.join(', ')}` },
        { name: 'search', type: 'string', required: false, help: 'Optional name/owner substring filter (e.g. "llama", "mistralai/")' },
        { name: 'pipeline', type: 'string', required: false, help: 'Filter by pipeline tag (e.g. text-generation, image-classification)' },
        { name: 'limit', type: 'int', default: 20, help: 'Max models (max 100; one API page).' },
    ],
    columns: ['rank', 'id', 'author', 'pipelineTag', 'downloads', 'likes', 'tags', 'lastModified', 'url'],
    func: async (args) => {
        const sortRaw = String(args.sort ?? 'downloads').toLowerCase();
        const sort = SORT_ALIAS[sortRaw] ?? sortRaw;
        if (!SORT_OPTIONS.includes(sort)) {
            throw new ArgumentError(`hf models sort must be one of ${SORT_OPTIONS.join(', ')}`);
        }
        const limit = Number(args.limit ?? 20);
        if (!Number.isInteger(limit) || limit <= 0) {
            throw new ArgumentError('hf models limit must be a positive integer');
        }
        if (limit > 100) {
            throw new ArgumentError('hf models limit must be <= 100');
        }

        const url = new URL('https://huggingface.co/api/models');
        url.searchParams.set('sort', sort);
        url.searchParams.set('direction', '-1');
        url.searchParams.set('limit', String(limit));
        url.searchParams.set('full', 'true');
        if (args.search) url.searchParams.set('search', String(args.search));
        if (args.pipeline) url.searchParams.set('pipeline_tag', String(args.pipeline));

        let resp;

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use one of the exact accepted values: downloads, likes, trending, created_at, last_modified.
  2. Use the alias forms 'lastmodified' or 'createdat' (case-insensitive) if more natural.
  3. Check `opencli hf models --help`, which lists the valid sort keys in the --sort help text.
  4. If a desired sort key is missing, it must be added to SORT_OPTIONS in clis/hf/models.js (and mapped in SORT_ALIAS if spelled differently).

Example fix

// before
opencli hf models --sort last-modified
// after
opencli hf models --sort last_modified
Defensive patterns

Strategy: validation

Validate before calling

const SORT_OPTIONS = ['downloads', 'likes', 'trending', 'created_at', 'last_modified'];
const SORT_ALIAS = { lastmodified: 'last_modified', createdat: 'created_at' };
function validateSort(raw) {
  const s = String(raw ?? 'downloads').toLowerCase();
  const norm = SORT_ALIAS[s] ?? s;
  if (!SORT_OPTIONS.includes(norm)) throw new Error(`sort must be one of ${SORT_OPTIONS.join(', ')}`);
  return norm;
}

Try / catch

try {
  await run(`hf models --sort ${sort}`);
} catch (e) {
  if (String(e.message).includes('sort must be one of')) {
    console.error(`Invalid --sort "${sort}"; valid: downloads, likes, trending, created_at, last_modified`);
  } else throw e;
}

Prevention

When it happens

Trigger: Passing `hf models --sort <value>` where value is not in SORT_OPTIONS after normalization — e.g. --sort trending-score, --sort downloads7d, --sort last-modified (hyphenated, not in alias map), or any arbitrary string.

Common situations: Guessing sort keys from Hugging Face web UI names (e.g. 'trendingScore', 'mostDownloads') instead of the CLI's exact keys; using hyphenated variants like 'last-modified' or camelCase 'createdAt' that neither the alias map nor the option list accepts; scripting the CLI with a sort value read from config written for another tool.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/442ba7d5d75e1bfa. Report an issue: GitHub.