jackwener/OpenCLI · error · EmptyResultError

hf datasets

Error message

hf datasets

What it means

EmptyResultError('hf datasets', 'No matching datasets on huggingface.co.') is thrown by the `hf datasets` command when the Hugging Face /api/datasets endpoint returns a successful HTTP response whose parsed body is an empty array (or a non-array body). The library uses it to distinguish 'the API worked but returned nothing' from network or protocol failures so callers can handle empty results explicitly.

Source

Thrown at clis/hf/datasets.js:69

                    'Accept': 'application/json',
                    'User-Agent': 'opencli/1.0 (+https://github.com/jackwener/opencli)',
                },
            });
        } catch (error) {
            throw new CommandExecutionError(`hf datasets request failed: ${error?.message || error}`);
        }
        if (!resp.ok) {
            throw new CommandExecutionError(`hf datasets failed: HTTP ${resp.status}`);
        }
        let data;
        try {
            data = await resp.json();
        } catch (error) {
            throw new CommandExecutionError(`hf datasets returned malformed JSON: ${error?.message || error}`);
        }
        const list = Array.isArray(data) ? data : [];
        if (list.length === 0) {
            throw new EmptyResultError('hf datasets', 'No matching datasets on huggingface.co.');
        }
        return list.slice(0, limit).map((d, i) => {
            const id = d.id || '';
            const slashIdx = id.indexOf('/');
            const author = slashIdx > 0 ? id.slice(0, slashIdx) : '';
            const tags = Array.isArray(d.tags) ? d.tags.filter(t => !t.startsWith('license:')).slice(0, 10).join(', ') : '';
            return {
                rank: i + 1,
                id,
                author,
                downloads: d.downloads ?? 0,
                likes: d.likes ?? 0,
                tags,
                lastModified: d.lastModified ? String(d.lastModified).slice(0, 10) : '',
                url: id ? `https://huggingface.co/datasets/${id}` : '',
            };
        });
    },

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-run with a broader or corrected --search term (check spelling and owner prefix).
  2. Remove --search filters to confirm the API returns data at all.
  3. Change the --sort key (e.g. from 'trending' to 'downloads') in case one ordering yields no rows.
  4. If it persists with no filters, check https://huggingface.co status — the API may be returning empty responses.

Example fix

// before
opencli hf datasets --search 'my-typo-dataset-name'
// after
opencli hf datasets --search 'squad'
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-check: does the search term exist at all?
const r = await fetch('https://huggingface.co/api/datasets?search=' + encodeURIComponent(term) + '&limit=1');
const rows = await r.json();
if (!Array.isArray(rows) || rows.length === 0) console.warn(`No datasets match "${term}" — widen the search before calling hf datasets`);

Type guard

const isNonEmptyArray = (v) => Array.isArray(v) && v.length > 0;

Try / catch

try {
  await run('hf datasets --search ' + term);
} catch (e) {
  if (e.name === 'EmptyResultError') {
    console.warn('No matching datasets; retrying without filters');
    await run('hf datasets');
  } else throw e;
}

Prevention

When it happens

Trigger: Running `hf datasets` (or with --search) when the fetch succeeds (resp.ok) but the parsed JSON array is empty — e.g. a search string matches no dataset, the search term is misspelled or too specific, or the API returns an empty/non-array payload for the given sort/filters.

Common situations: Typo in --search (e.g. 'llamaa' instead of 'llama'); querying an owner prefix like 'someorg/' that has no datasets; a sort/filter combination with zero matching datasets; Hugging Face returning an empty page during API incidents or partial outages.

Related errors


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