xtekky/gpt4free · warning · MissingRequirementsError

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

Error message

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

What it means

supports_filename raises MissingRequirementsError when a .pdf file is offered but none of the optional PDF backends (PyPDF2, pdfplumber, pdfminer) is importable. g4f keeps file-parsing deps optional, so PDF support must be installed explicitly via the 'files' extra.

Source

Thrown at g4f/tools/files.py:126

    "csv",
    "twig",
    "md",
    "arc",
]
PLAIN_CACHE = "plain.cache"
DOWNLOADS_FILE = "downloads.json"
FILE_LIST = "files.txt"


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:

View on GitHub (pinned to 973504e177)

Solutions

  1. pip install -U 'g4f[files]' — installs PyPDF2 (or any one of the PDF backends suffices).
  2. Or install a single backend directly: pip install pypdf2 (alternatives: pdfplumber or pdfminer.six).
  3. Verify import in the same interpreter that runs g4f: python -c 'import PyPDF2'.
  4. Convert the file to plain text and pass that instead if you cannot add deps.

Example fix

# before
pip install g4f
client.chat.completions.create(..., media=Attachment('doc.pdf', open('doc.pdf','rb')))

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

Strategy: validation

Validate before calling

from g4f.tools.files import supports_filename

# cheap pre-flight: raises MissingRequirementsError before any network work
supports_filename('report.pdf')

Type guard

from g4f.errors import MissingRequirementsError

def pdf_backend_available() -> bool:
    try:
        import PyPDF2  # noqa
        return True
    except ImportError:
        pass
    for mod in ('pdfplumber', 'pdfminer'):
        try:
            __import__(mod)
            return True
        except ImportError:
            continue
    return False

Try / catch

from g4f.errors import MissingRequirementsError
try:
    supports_filename('report.pdf')
except MissingRequirementsError as e:
    print(f'Cannot process PDF: {e}')  # tell the user to install g4f[files]

Prevention

When it happens

Trigger: Uploading or attaching a .pdf in g4f (e.g. media/file attachment in a chat completion) on an environment installed with plain 'pip install g4f' instead of 'g4f[files]'.

Common situations: Minimal installs in Docker/CI; slim venvs where extras were skipped; a second Python environment without the extras picked up by the runtime.

Related errors


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