MiniMax-AI/skills · error · ValueError

Cannot decode {file_path}. Tried encodings: {encodings}. Las

Error message

Cannot decode {file_path}. Tried encodings: {encodings}. Last error: {last_error}

What it means

For CSV/TSV the loader tries utf-8-sig, gbk, utf-8, latin-1 in order and, if all raise, gives up with ValueError listing the attempts and the last error. Subtlety: latin-1 maps every byte to a character and never raises UnicodeDecodeError, so a pure encoding failure is nearly impossible to reach — the real trigger is a non-encoding parser error (empty file, malformed CSV, embedded null bytes) caught by the broad 'Exception' in the except tuple.

Source

Thrown at skills/minimax-xlsx/scripts/xlsx_reader.py:72

        if isinstance(result, dict):
            return result
        else:
            return {sheet_name_filter: result}

    elif suffix in (".csv", ".tsv"):
        sep = "\t" if suffix == ".tsv" else ","
        encodings = ["utf-8-sig", "gbk", "utf-8", "latin-1"]
        last_error = None
        for enc in encodings:
            try:
                import pandas as pd
                df = pd.read_csv(file_path, sep=sep, encoding=enc)
                df._reader_encoding = enc  # attach metadata (non-standard, for reporting)
                return {path.stem: df}
            except (UnicodeDecodeError, Exception) as e:
                last_error = e
                continue
        raise ValueError(
            f"Cannot decode {file_path}. Tried encodings: {encodings}. "
            f"Last error: {last_error}"
        )

    elif suffix == ".xls":
        raise ValueError(
            ".xls is a legacy binary format not supported by this tool. "
            "Please open the file in Excel and save as .xlsx, then retry."
        )

    else:
        raise ValueError(
            f"Unsupported file format: {suffix}. "
            "Supported formats: .xlsx, .xlsm, .csv, .tsv"
        )


# ---------------------------------------------------------------------------

View on GitHub (pinned to 60aaae52bb)

Solutions

  1. Confirm the file is non-empty and actually CSV/TSV (check delimiter vs extension).
  2. Re-save the file as UTF-8 from a spreadsheet editor.
  3. Extend the encodings list with 'big5','shift_jis','utf-16' if it is genuinely a CJK/UTF-16 file.
  4. Use chardet to detect encoding, then re-run with the detected value.

Example fix

# before
sheets = detect_and_load('data.tsv')   # ValueError: Cannot decode ...

# after - extend the tried encodings and surface parser errors distinctly
encodings = ['utf-8-sig','gbk','utf-8','big5','shift_jis','utf-16','latin-1']
for enc in encodings:
    try:
        df = pd.read_csv(path, sep=sep, encoding=enc)
        return {Path(path).stem: df}
    except UnicodeDecodeError:
        continue
    except Exception as e:
        raise ValueError(f'parser error ({enc}): {e}') from e
Defensive patterns

Strategy: fallback

Validate before calling

from pathlib import Path
p = Path(file_path)
if p.stat().st_size == 0:
    raise ValueError(f'{file_path} is empty')
# sniff delimiter vs extension
import csv
with open(file_path, newline='', encoding='utf-8', errors='replace') as fh:
    sample = fh.read(2048)
    delim = csv.Sniffer().sniff(sample).delimiter

Try / catch

try:
    sheets = detect_and_load(file_path)
except ValueError as e:
    if 'Cannot decode' in str(e):
        # fallback: let pandas autodetect, or convert via chardet
        import chardet
        raw = open(file_path,'rb').read()
        enc = chardet.detect(raw)['encoding'] or 'utf-8'
        sheets = {Path(file_path).stem: pd.read_csv(file_path, encoding=enc)}
    else:
        raise

Prevention

When it happens

Trigger: CSV/TSV where pd.read_csv raises under every tried encoding. Given latin-1 always decodes, this is almost always a parser error (empty file, wrong delimiter, binary/corrupt bytes, null bytes) rather than a true encoding failure.

Common situations: Empty or zero-byte CSV; tab-delimited content passed as .csv; file with embedded NUL bytes; corrupt/truncated export; pd.read_csv parser error on malformed quoting.

Related errors


AI-assisted analysis of MiniMax-AI/skills@60aaae52bb (2026-08-13). Data as JSON: /api/errors/5710be1dcd127418. Report an issue: GitHub.