{"record":{"id":"ea811511ab5facac","repo":"redis/redis-py","slug":"the-pyjwt-library-is-required-for-self-class","errorCode":null,"errorMessage":"The PyJWT library is required for {self.__class__.__name__}.","messagePattern":"The PyJWT library is required for (.+?)\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"redis/auth/token.py","lineNumber":85,"sourceCode":"\n    def get_value(self) -> str:\n        return self.value\n\n    def get_expires_at_ms(self) -> float:\n        return self.expires_at\n\n    def get_received_at_ms(self) -> float:\n        return self.received_at\n\n\nclass JWToken(TokenInterface):\n    REQUIRED_FIELDS = {\"exp\"}\n\n    def __init__(self, token: str):\n        try:\n            import jwt\n        except ImportError as ie:\n            raise ImportError(\n                f\"The PyJWT library is required for {self.__class__.__name__}.\",\n            ) from ie\n        self._value = token\n        self._decoded = jwt.decode(\n            self._value,\n            options={\"verify_signature\": False},\n            algorithms=[jwt.get_unverified_header(self._value).get(\"alg\")],\n        )\n        self._validate_token()\n\n    def is_expired(self) -> bool:\n        exp = self._decoded[\"exp\"]\n        if exp == -1:\n            return False\n\n        return (\n            self._decoded[\"exp\"] * 1000 <= datetime.now(timezone.utc).timestamp() * 1000\n        )","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/auth/token.py#L67-L103","documentation":"Raised as ImportError inside JWToken.__init__ when `import jwt` fails, because the JWToken class decodes JWTs using the PyJWT library. PyJWT is an optional dependency (the jwt extra) and is not installed by default. The error names the class so you know which token implementation needs it.","triggerScenarios":"Constructing a JWToken(token_string) instance (used by the auth/token_manager EntraID/JWT flows) in an environment where PyJWT is not installed. This happens during credential-provider or token-based-auth initialization that uses JWToken.","commonSituations":"Installing redis-py without the [jwt] extra. Using EntraID/JWT auth features that depend on JWToken without adding PyJWT to requirements. A fresh virtualenv that only installed the base redis package.","solutions":["Install PyJWT: `pip install PyJWT` (or `pip install redis[jwt]`).","Add PyJWT (or the jwt extra) to your project's requirements/lockfile.","If you did not intend to use JWT tokens, switch to a Token implementation that does not require PyJWT."],"exampleFix":"# before: ImportError when constructing JWToken\npip install redis\n# after\npip install \"redis[jwt]\"","handlingStrategy":"validation","validationCode":"try:\n    import jwt  # noqa: F401\nexcept ImportError:\n    raise SystemExit(\"PyJWT is required for JWT auth; pip install redis[jwt]\")\ntoken = JWToken(value)","typeGuard":"def jwt_available() -> bool:\n    try:\n        import jwt  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    token = JWToken(value)\nexcept ImportError as e:\n    if \"PyJWT\" in str(e):\n        raise SystemExit(\"Install PyJWT: pip install redis[jwt]\")\n    raise","preventionTips":["Pin redis[jwt] (or PyJWT) in requirements when using JWT auth.","Check dependency presence at deploy time, not at first auth."],"tags":["auth","jwt","dependency","import"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}