xtekky/gpt4free · warning · MissingRequirementsError

Install "docx" requirements | pip install -U g4f[files]

Error message

Install "docx" requirements | pip install -U g4f[files]

What it means

supports_filename raises MissingRequirementsError when a .docx file is passed but neither python-docx (docx) nor docx2txt is installed. Like the PDF case, Word-document support is an optional 'files' extra in g4f.

Source

Thrown at g4f/tools/files.py:134


def supports_filename(filename: str):
    if filename.endswith(".pdf"):
        if has_pypdf2:
            return True
        elif has_pdfplumber:
            return True
        elif has_pdfminer:
            return True
        raise MissingRequirementsError(
            f'Install "pypdf2" requirements | pip install -U g4f[files]'
        )
    elif filename.endswith(".docx"):
        if has_docx:
            return True
        elif has_docx2txt:
            return True
        raise MissingRequirementsError(
            f'Install "docx" requirements | pip install -U g4f[files]'
        )
    elif has_odfpy and filename.endswith(".odt"):
        return True
    elif has_ebooklib and filename.endswith(".epub"):
        return True
    elif has_openpyxl and filename.endswith(".xlsx"):
        return True
    elif filename.endswith(".html"):
        if not has_beautifulsoup4:
            raise MissingRequirementsError(
                f'Install "beautifulsoup4" requirements | pip install -U g4f[files]'
            )
        return True
    elif filename.endswith(".zip"):
        return True
    elif filename.endswith("package-lock.json") and filename != FILE_LIST:
        return False

View on GitHub (pinned to 973504e177)

Solutions

  1. pip install -U 'g4f[files]'.
  2. Or install one backend: pip install python-docx (or docx2txt).
  3. Confirm with: python -c 'import docx'.
  4. As a workaround, save the document as .txt/.pdf(with backend) before attaching.

Example fix

# before
pip install g4f

# after
pip install -U 'g4f[files]'
Defensive patterns

Strategy: validation

Validate before calling

from g4f.tools.files import supports_filename
supports_filename('notes.docx')  # raises MissingRequirementsError if no docx backend

Type guard

from g4f.errors import MissingRequirementsError

def docx_backend_available() -> bool:
    for mod in ('docx', 'docx2txt'):
        try:
            __import__(mod)
            return True
        except ImportError:
            continue
    return False

Try / catch

from g4f.errors import MissingRequirementsError
try:
    supports_filename('notes.docx')
except MissingRequirementsError as e:
    raise SystemExit('Run: pip install -U g4f[files]') from e

Prevention

When it happens

Trigger: Attaching a .docx to a g4f request without having installed python-docx or docx2txt in the active environment.

Common situations: Base g4f install without extras; CI images that prune optional deps; activating the wrong virtualenv.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/9dfade196355bb83. Report an issue: GitHub.