{"id":"baa38231c98255c2","repo":"encode/httpx","slug":"attempted-to-send-an-async-request-with-a-sync-cli","errorCode":null,"errorMessage":"Attempted to send an async request with a sync Client instance.","messagePattern":"Attempted to send an async request with a sync Client instance\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"httpx/_client.py","lineNumber":1009,"sourceCode":"                if follow_redirects:\n                    response.read()\n                else:\n                    response.next_request = request\n                    return response\n\n            except BaseException as exc:\n                response.close()\n                raise exc\n\n    def _send_single_request(self, request: Request) -> Response:\n        \"\"\"\n        Sends a single request, without handling any redirections.\n        \"\"\"\n        transport = self._transport_for_url(request.url)\n        start = time.perf_counter()\n\n        if not isinstance(request.stream, SyncByteStream):\n            raise RuntimeError(\n                \"Attempted to send an async request with a sync Client instance.\"\n            )\n\n        with request_context(request=request):\n            response = transport.handle_request(request)\n\n        assert isinstance(response.stream, SyncByteStream)\n\n        response.request = request\n        response.stream = BoundSyncStream(\n            response.stream, response=response, start=start\n        )\n        self.cookies.extract_cookies(response)\n        response.default_encoding = self._default_encoding\n\n        logger.info(\n            'HTTP Request: %s %s \"%s %d %s\"',\n            request.method,","sourceCodeStart":991,"sourceCodeEnd":1027,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_client.py#L991-L1027","documentation":"Raised as RuntimeError by sync Client._send_single_request when request.stream is not a SyncByteStream. The request body was built from an async iterable/generator, which cannot be driven by a synchronous client.","triggerScenarios":"Constructing an httpx.Request with an async byte generator as content (e.g. content=<async generator>) and sending it through the sync client.send()/client.request().","commonSituations":"Mixing an async content source (async def/async generator yielding bytes) with a synchronous Client; copying a request built for async use into sync code.","solutions":["Use httpx.AsyncClient to send requests whose body is an async iterable.","If you must use the sync Client, supply a sync iterable/bytes as content instead."],"exampleFix":"// before\nasync def gen(): yield b\"data\"\nhttpx.Client().send(httpx.Request(\"POST\", url, content=gen()))  # RuntimeError\n// after\n# use AsyncClient for async content\nasync with httpx.AsyncClient() as c:\n    await c.post(url, content=gen())","handlingStrategy":"type-guard","validationCode":"import inspect, collections.abc as abc\n\ndef is_sync_content(content) -> bool:\n    if isinstance(content, (bytes, str)):\n        return True\n    if isinstance(content, abc.AsyncIterable):\n        return False\n    return True  # plain iterable\n\n# for sync Client, ensure content is NOT an async iterable\nassert is_sync_content(content), \"async content cannot be used with sync Client\"","typeGuard":"import collections.abc as abc\n\ndef is_async_content(content) -> bool:\n    return isinstance(content, abc.AsyncIterable)","tryCatchPattern":"try:\n    client.send(req)\nexcept RuntimeError as exc:\n    if \"async request with a sync Client\" in str(exc):\n        raise RuntimeError(\"Use httpx.AsyncClient for async-iterable content\") from exc\n    raise","preventionTips":["Match the client kind to the content kind: sync Client -> sync iterable, AsyncClient -> async iterable.","Never build a Request with an async generator for use with the sync Client.","Buffer content to bytes when in doubt."],"tags":["sync-async","streaming","sync"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}