{"id":"ed0968e65e56e8b0","repo":"psf/requests","slug":"unsupported-event-specified-with-event-name-eve","errorCode":null,"errorMessage":"Unsupported event specified, with event name \"{event}\"","messagePattern":"Unsupported event specified, with event name \"(.+?)\"","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/requests/models.py","lineNumber":263,"sourceCode":"            rf = RequestField(name=k, data=fdata, filename=fn, headers=fh)\n            rf.make_multipart(content_type=ft)\n            new_fields.append(rf)\n\n        body, content_type = encode_multipart_formdata(new_fields)\n\n        return body, content_type\n\n\nclass RequestHooksMixin:\n    hooks: dict[str, list[_t.HookType]]\n\n    def register_hook(\n        self, event: str, hook: Iterable[_t.HookType] | _t.HookType\n    ) -> None:\n        \"\"\"Properly register a hook.\"\"\"\n\n        if event not in self.hooks:\n            raise ValueError(f'Unsupported event specified, with event name \"{event}\"')\n\n        if isinstance(hook, Callable):\n            self.hooks[event].append(hook)\n        elif hasattr(hook, \"__iter__\"):\n            self.hooks[event].extend(\n                h for h in hook if isinstance(h, Callable)\n            )  # defensive runtime filter\n\n    def deregister_hook(self, event: str, hook: _t.HookType) -> bool:\n        \"\"\"Deregister a previously registered hook.\n        Returns True if the hook existed, False if not.\n        \"\"\"\n\n        try:\n            self.hooks[event].remove(hook)\n            return True\n        except ValueError:\n            return False","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/psf/requests/blob/414f0513c33883adf6f2b46901d4f0b38a455851/src/requests/models.py#L245-L281","documentation":"Raised by `RequestHooksMixin.register_hook` when the `event` name is not a key in the object's `hooks` dict. Requests only dispatches a fixed set of hook events (in practice just `\"response\"`, initialized by `default_hooks()`), so registering a hook for any other event name would silently never fire — the ValueError fails fast instead.","triggerScenarios":"`requests.get(url, hooks={'pre_request': fn})` or `session.hooks` / `Request(hooks={...})` with any key other than 'response'; calling `req.register_hook('request', fn)` directly. The hooks dict passed to the Request constructor is registered event-by-event, so one bad key raises.","commonSituations":"Code ported from very old requests versions (pre-1.0 supported events like 'pre_request', 'args', 'post_request' that were removed); typos like 'reponse'; assuming a richer event model similar to other HTTP libraries' middleware.","solutions":["Use the only supported event name, 'response': `hooks={'response': my_hook}`.","For pre-send behavior, subclass `requests.adapters.HTTPAdapter` or use a Session with a custom `prepare_request` wrapper instead of a hook.","Check `request.hooks.keys()` (from `requests.hooks.default_hooks()`) to see the valid event names for your version."],"exampleFix":"# before\nrequests.get(url, hooks={'pre_request': log_hook})\n\n# after\nrequests.get(url, hooks={'response': log_hook})","handlingStrategy":"validation","validationCode":"VALID_HOOKS = {\"response\"}  # requests only supports the 'response' hook\nassert all(k in VALID_HOOKS for k in hooks), f\"invalid hook events: {set(hooks) - VALID_HOOKS}\"","typeGuard":null,"tryCatchPattern":"try:\n    resp = requests.get(url, hooks={\"response\": my_hook})\nexcept ValueError as e:\n    if \"Unsupported event\" in str(e):\n        ...  # misspelled or unsupported hook name\n    raise","preventionTips":["Only register the 'response' hook — it is the only event requests supports","Validate hook dict keys against Request.hooks.keys() before passing them","Watch for typos like 'responses' or 'pre_request' (removed in requests 1.x)"],"tags":["python","requests","hooks","api-usage"],"analyzedSha":"414f0513c33883adf6f2b46901d4f0b38a455851","analyzedAt":"2026-07-31T19:24:57.696Z","schemaVersion":2}