{"id":"fc42cfa728a81acb","repo":"encode/httpx","slug":"attempted-to-send-a-sync-request-with-an-asyncclie","errorCode":null,"errorMessage":"Attempted to send a sync request with an AsyncClient instance.","messagePattern":"Attempted to send a sync request with an AsyncClient instance\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"httpx/_client.py","lineNumber":1725,"sourceCode":"                if follow_redirects:\n                    await response.aread()\n                else:\n                    response.next_request = request\n                    return response\n\n            except BaseException as exc:\n                await response.aclose()\n                raise exc\n\n    async 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, AsyncByteStream):\n            raise RuntimeError(\n                \"Attempted to send a sync request with an AsyncClient instance.\"\n            )\n\n        with request_context(request=request):\n            response = await transport.handle_async_request(request)\n\n        assert isinstance(response.stream, AsyncByteStream)\n        response.request = request\n        response.stream = BoundAsyncStream(\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,\n            request.url,","sourceCodeStart":1707,"sourceCodeEnd":1743,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_client.py#L1707-L1743","documentation":"Raised as RuntimeError by AsyncClient._send_single_request when request.stream is not an AsyncByteStream. The body was built from a sync iterable/generator, which the async client cannot await.","triggerScenarios":"Building an httpx.Request with a sync generator/iterable as content and sending it through AsyncClient.send()/request().","commonSituations":"Passing a sync bytes-iterator (e.g. a plain generator yielding bytes) into an async client; reusing a request constructed for the sync client in async code.","solutions":["Provide an async iterable (async generator yielding bytes) as content for AsyncClient.","Or use the sync httpx.Client for sync content sources."],"exampleFix":"// before\ndef gen(): yield b\"data\"\nawait client.send(httpx.Request(\"POST\", url, content=gen()))  # RuntimeError\n// after\nasync def gen(): yield b\"data\"\nawait client.post(url, content=gen())","handlingStrategy":"type-guard","validationCode":"import collections.abc as abc\n\ndef is_async_content(content) -> bool:\n    return isinstance(content, abc.AsyncIterable)\n\n# for AsyncClient, ensure content is an async iterable (or bytes/str)\nassert is_async_content(content) or isinstance(content, (bytes, str)), \\\n    \"sync-only content cannot be used with AsyncClient\"","typeGuard":"import collections.abc as abc\n\ndef is_async_content(content) -> bool:\n    return isinstance(content, abc.AsyncIterable)","tryCatchPattern":"try:\n    await client.send(req)\nexcept RuntimeError as exc:\n    if \"sync request with an AsyncClient\" in str(exc):\n        raise RuntimeError(\"Use sync httpx.Client for sync-iterable content\") from exc\n    raise","preventionTips":["Use async-iterable content with AsyncClient and sync-iterable content with Client.","Define async generators for streaming uploads in async code.","Buffer content to bytes when the iterable kind is uncertain."],"tags":["sync-async","streaming","async"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}