{"record":{"id":"4314081b81e1eb76","repo":"arduino/Arduino","slug":"you-can-only-send-preparedrequests","errorCode":null,"errorMessage":"You can only send PreparedRequests.","messagePattern":"You can only send PreparedRequests\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"arduino-core/src/processing/app/i18n/python/requests/sessions.py","lineNumber":398,"sourceCode":"        \"\"\"\n\n        return self.request('PATCH', url,  data=data, **kwargs)\n\n    def delete(self, url, **kwargs):\n        \"\"\"Sends a DELETE request. Returns :class:`Response` object.\n\n        :param url: URL for the new :class:`Request` object.\n        :param \\*\\*kwargs: Optional arguments that ``request`` takes.\n        \"\"\"\n\n        return self.request('DELETE', url, **kwargs)\n\n    def send(self, request, **kwargs):\n        \"\"\"Send a given PreparedRequest.\"\"\"\n        # It's possible that users might accidentally send a Request object.\n        # Guard against that specific failure case.\n        if getattr(request, 'prepare', None):\n            raise ValueError('You can only send PreparedRequests.')\n\n        # Set up variables needed for resolve_redirects and dispatching of\n        # hooks\n        allow_redirects = kwargs.pop('allow_redirects', True)\n        req = kwargs.pop('req', None)\n        stream = kwargs.get('stream', False)\n        timeout = kwargs.get('timeout')\n        verify = kwargs.get('verify')\n        cert = kwargs.get('cert')\n        proxies = kwargs.get('proxies')\n        hooks = request.hooks\n\n        # Get the appropriate adapter to use\n        adapter = self.get_adapter(url=request.url)\n\n        # Start time (approximately) of the request\n        start = datetime.utcnow()\n        # Send the request","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/arduino/Arduino/blob/a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee/arduino-core/src/processing/app/i18n/python/requests/sessions.py#L380-L416","documentation":"Session.send() only accepts PreparedRequest objects. If the passed object still has a .prepare attribute (i.e. it's an unprepared requests.Request), send raises ValueError, guarding against the common mistake of passing a plain Request.","triggerScenarios":"Calling session.send(request.Request(...)) directly; building a Request manually and forgetting to call Request.prepare() / requests.PreparedRequest; passing the wrong object through wrapper code that forwards to session.send().","commonSituations":"Low-level usage of requests internals (retry middleware, custom adapters) where authors mix Request and PreparedRequest; copying example code that uses session.send without the preparation step.","solutions":["Call .prepare() on the Request before sending, or use session.request(...) which prepares for you.","Use requests.PreparedRequest() explicitly and populate prepared fields before send().","Fix wrapper functions to accept PreparedRequest or call prepare internally."],"exampleFix":"// before\nreq = requests.Request('GET', 'https://example.com')\nsession.send(req)  # ValueError\n// after\nreq = requests.Request('GET', 'https://example.com').prepare()\nsession.send(req)","handlingStrategy":"type-guard","validationCode":"from requests import PreparedRequest\ndef send_prepared(session, req, **kw):\n    if not isinstance(req, PreparedRequest):\n        raise TypeError('send() requires PreparedRequest, got %s' % type(req).__name__)\n    return session.send(req, **kw)","typeGuard":"from requests import PreparedRequest\ndef is_prepared(req) -> bool:\n    return isinstance(req, PreparedRequest)","tryCatchPattern":"try:\n    session.send(req, timeout=10)\nexcept ValueError as e:\n    if 'PreparedRequests' in str(e):\n        req = req.prepare()\n        session.send(req, timeout=10)\n    else:\n        raise","preventionTips":["Prefer session.request()/get()/post() which handle preparation automatically.","In low-level code, always call .prepare() before .send().","Type-hint helper functions with PreparedRequest to catch mistakes early.","Add isinstance assertions in wrappers around session.send."],"tags":["requests","python","api-misuse","type-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee","analyzedAt":"2026-09-06T10:13:38.901Z","contentChangedAt":"2026-09-06T10:13:38.901Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}