{"record":{"id":"7f2f6f01ab5a86d6","repo":"xtekky/gpt4free","slug":"install-pypdf2-requirements-pip-install-u-g4f","errorCode":null,"errorMessage":"Install \"pypdf2\" requirements | pip install -U g4f[files]","messagePattern":"Install \"pypdf2\" requirements \\| pip install -U g4f\\[files\\]","errorType":"exception","errorClass":"MissingRequirementsError","httpStatus":null,"severity":"warning","filePath":"g4f/tools/files.py","lineNumber":126,"sourceCode":"    \"csv\",\n    \"twig\",\n    \"md\",\n    \"arc\",\n]\nPLAIN_CACHE = \"plain.cache\"\nDOWNLOADS_FILE = \"downloads.json\"\nFILE_LIST = \"files.txt\"\n\n\ndef supports_filename(filename: str):\n    if filename.endswith(\".pdf\"):\n        if has_pypdf2:\n            return True\n        elif has_pdfplumber:\n            return True\n        elif has_pdfminer:\n            return True\n        raise MissingRequirementsError(\n            f'Install \"pypdf2\" requirements | pip install -U g4f[files]'\n        )\n    elif filename.endswith(\".docx\"):\n        if has_docx:\n            return True\n        elif has_docx2txt:\n            return True\n        raise MissingRequirementsError(\n            f'Install \"docx\" requirements | pip install -U g4f[files]'\n        )\n    elif has_odfpy and filename.endswith(\".odt\"):\n        return True\n    elif has_ebooklib and filename.endswith(\".epub\"):\n        return True\n    elif has_openpyxl and filename.endswith(\".xlsx\"):\n        return True\n    elif filename.endswith(\".html\"):\n        if not has_beautifulsoup4:","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/tools/files.py#L108-L144","documentation":"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.","triggerScenarios":"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]'.","commonSituations":"Minimal installs in Docker/CI; slim venvs where extras were skipped; a second Python environment without the extras picked up by the runtime.","solutions":["pip install -U 'g4f[files]' — installs PyPDF2 (or any one of the PDF backends suffices).","Or install a single backend directly: pip install pypdf2 (alternatives: pdfplumber or pdfminer.six).","Verify import in the same interpreter that runs g4f: python -c 'import PyPDF2'.","Convert the file to plain text and pass that instead if you cannot add deps."],"exampleFix":"# before\npip install g4f\nclient.chat.completions.create(..., media=Attachment('doc.pdf', open('doc.pdf','rb')))\n\n# after\npip install -U 'g4f[files]'","handlingStrategy":"validation","validationCode":"from g4f.tools.files import supports_filename\n\n# cheap pre-flight: raises MissingRequirementsError before any network work\nsupports_filename('report.pdf')","typeGuard":"from g4f.errors import MissingRequirementsError\n\ndef pdf_backend_available() -> bool:\n    try:\n        import PyPDF2  # noqa\n        return True\n    except ImportError:\n        pass\n    for mod in ('pdfplumber', 'pdfminer'):\n        try:\n            __import__(mod)\n            return True\n        except ImportError:\n            continue\n    return False","tryCatchPattern":"from g4f.errors import MissingRequirementsError\ntry:\n    supports_filename('report.pdf')\nexcept MissingRequirementsError as e:\n    print(f'Cannot process PDF: {e}')  # tell the user to install g4f[files]","preventionTips":["Install g4f with extras: pip install -U 'g4f[files]'","Run supports_filename() as a pre-flight before attaching files","Add import smoke tests for file backends to your Docker build","Check attachments in the same interpreter that runs g4f"],"tags":["missing-dependency","pdf","extras","g4f"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}