{"record":{"id":"18d5a6e7c5aa4403","repo":"xtekky/gpt4free","slug":"failed-to-get-pypi-version-for-package-name","errorCode":null,"errorMessage":"Failed to get PyPI version for '{package_name}'","messagePattern":"Failed to get PyPI version for '(.+?)'","errorType":"exception","errorClass":"VersionNotFoundError","httpStatus":null,"severity":"warning","filePath":"g4f/version.py","lineNumber":34,"sourceCode":"\n@lru_cache(maxsize=1)\ndef get_pypi_version(package_name: str) -> str:\n    \"\"\"\n    Retrieves the latest version of a package from PyPI.\n\n    Raises:\n        VersionNotFoundError: If there is a network or parsing error.\n    \"\"\"\n    try:\n        import requests\n\n        response = requests.get(\n            f\"https://pypi.org/pypi/{package_name}/json\", timeout=REQUEST_TIMEOUT\n        )\n        response.raise_for_status()\n        return response.json()[\"info\"][\"version\"]\n    except Exception as e:\n        raise VersionNotFoundError(\n            f\"Failed to get PyPI version for '{package_name}'\"\n        ) from e\n\n\n@lru_cache(maxsize=1)\ndef get_github_version(repo: str) -> str:\n    \"\"\"\n    Retrieves the latest release version from a GitHub repository.\n\n    Raises:\n        VersionNotFoundError: If there is a network or parsing error.\n    \"\"\"\n    try:\n        import requests\n\n        response = requests.get(\n            f\"https://api.github.com/repos/{repo}/releases/latest\",\n            timeout=REQUEST_TIMEOUT,","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/version.py#L16-L52","documentation":"get_latest_pypi_version in g4f/version.py raises VersionNotFoundError when fetching https://pypi.org/pypi/{package}/json fails for any reason: network error, non-2xx status (raise_for_status), missing 'info.version' key, or a JSON parse error. The original exception is chained via 'from e'.","triggerScenarios":"Calling get_latest_pypi_version('g4f') (directly or through an update-check helper) while offline, behind a proxy that blocks pypi.org, when PyPI rate-limits or 404s the package, or when the response body is truncated/invalid JSON.","commonSituations":"Air-gapped or corporate-proxy environments; DNS failures; checking a package name that does not exist on PyPI; transient PyPI outages during update checks.","solutions":["Check network connectivity and proxy settings so https://pypi.org/pypi/<pkg>/json is reachable","Verify the package name exists on PyPI (a 404 triggers raise_for_status)","Catch VersionNotFoundError and fall back to the locally known version instead of crashing the update check","Retry after a short delay for transient PyPI errors; pin to cached results (the function is not cached, unlike get_github_version)"],"exampleFix":"# before\nversion = get_latest_pypi_version('g4f')  # raises if offline\n\n# after\nfrom g4f.version import get_latest_pypi_version, VersionNotFoundError\ntry:\n    version = get_latest_pypi_version('g4f')\nexcept VersionNotFoundError:\n    version = None  # skip update check","handlingStrategy":"try-catch","validationCode":"import socket, urllib.request\n\ndef pypi_reachable(timeout=3) -> bool:\n    try:\n        urllib.request.urlopen(\"https://pypi.org/pypi/g4f/json\", timeout=timeout)\n        return True\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"from g4f.version import get_latest_pypi_version, VersionNotFoundError\n\ntry:\n    latest = get_latest_pypi_version(\"g4f\")\nexcept VersionNotFoundError as e:\n    latest = None  # inspect e.__cause__ for the underlying network error","preventionTips":["Treat version checks as best-effort: always catch VersionNotFoundError","Cache or skip update checks in offline/CI environments","Confirm package names exist on PyPI before querying"],"tags":["network","pypi","version-check","update-check"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}