Title: Add support for httpx and async transport · Issue #15134 · googleapis/google-cloud-python · GitHub
Open Graph Title: Add support for httpx and async transport · Issue #15134 · googleapis/google-cloud-python
X Title: Add support for httpx and async transport · Issue #15134 · googleapis/google-cloud-python
Description: Add support for httpx for async transport. An exemplar PR to support this is available (it is currently closed to enable migration to the Python mono-repo). Both aiohttp and httpx are powerful libraries for making asynchronous HTTP reque...
Open Graph Description: Add support for httpx for async transport. An exemplar PR to support this is available (it is currently closed to enable migration to the Python mono-repo). Both aiohttp and httpx are powerful libr...
X Description: Add support for httpx for async transport. An exemplar PR to support this is available (it is currently closed to enable migration to the Python mono-repo). Both aiohttp and httpx are powerful libr...
Opengraph URL: https://github.com/googleapis/google-cloud-python/issues/15134
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add support for httpx and async transport","articleBody":"Add support for httpx for async transport. [An exemplar PR](https://github.com/googleapis/google-auth-library-python/pull/1908) to support this is available (it is currently closed to enable migration to the [Python mono-repo](https://github.com/googleapis/google-cloud-python)).\n\nBoth `aiohttp` and `httpx` are powerful libraries for making asynchronous HTTP requests in Python, but they serve slightly different purposes and have different design philosophies.\n\nHere is a breakdown of their differences, pros, cons, and when to use each, as provided by Gemini AI.\n\n---\n\n### 1. High-Level Overview\n\n* **aiohttp:** The \"venerable\" choice. It has been the industry standard for asynchronous HTTP in Python for years. It is a full-featured networking library that provides both a **Client** and a **Server** framework.\n* **httpx:** The \"modern\" choice. It was designed to be the spiritual successor to the famous `requests` library, but built for the modern `asyncio` era. Its biggest selling point is that it supports both **Synchronous** and **Asynchronous** APIs and has built-in support for **HTTP/2**.\n\n---\n\n### 2. Key Differences\n\n| Feature | aiohttp | httpx |\n| :--- | :--- | :--- |\n| **Sync Support** | No (Async only) | Yes (Sync and Async) |\n| **API Style** | Unique, explicit (requires `ClientSession`) | Very similar to `requests` |\n| **HTTP/2 Support** | No (supports HTTP/1.1 only) | Yes (Optional) |\n| **Web Server** | Yes (Built-in) | No (Client only) |\n| **Type Hinting** | Good | Excellent (Fully type-annotated) |\n| **Cookie/Session** | Strictly via `ClientSession` | Flexible (Top-level or Client) |\n\n---\n\n### 3. aiohttp\n\n#### Pros:\n* **Performance:** Generally considered slightly faster and more efficient in high-concurrency benchmarks because it is highly optimized for `asyncio`.\n* **Maturity:** It is battle-tested and has been used in production by major companies for nearly a decade.\n* **All-in-one:** If you are building a web server and also need to make outgoing requests, you only need one library.\n* **Control:** Offers lower-level control over connections and TCP parameters.\n\n#### Cons:\n* **Async-Only:** You cannot use it in a synchronous script. This makes it difficult to share code between sync and async parts of a codebase.\n* **Boilerplate:** It is more verbose. You *must* use a `ClientSession` context manager for almost everything.\n* **No HTTP/2:** It currently does not support HTTP/2 for its client.\n\n---\n\n### 4. httpx\n\n#### Pros:\n* **The \"Requests\" Feel:** If you know how to use `requests`, you know how to use `httpx`. The transition is almost seamless.\n* **Sync \u0026 Async:** You can use `httpx.get()` for a quick script and `await client.get()` for an async app. This allows library authors to support both paradigms easily.\n* **HTTP/2 Support:** This is a major advantage for modern web scraping and communicating with APIs that prioritize HTTP/2.\n* **Strictness:** It has better defaults for things like timeouts (it enforces them by default, whereas `aiohttp` often doesn't).\n* **WSGI/ASGI Support:** You can call your FastAPI or Flask app directly through `httpx` for testing without needing a running server.\n\n#### Cons:\n* **Performance:** In some raw benchmarks, it is slightly slower than `aiohttp` (though this is rarely the bottleneck in real-world IO-bound applications).\n* **Client Only:** It does not include a web server framework.\n\n---\n\n### 5. Code Comparison\n\n**aiohttp (Async only):**\n```python\nimport aiohttp\nimport asyncio\n\nasync def main():\n async with aiohttp.ClientSession() as session:\n async with session.get('https://example.com') as response:\n print(response.status)\n print(await response.text())\n\nasyncio.run(main())\n```\n\n**httpx (Async):**\n```python\nimport httpx\nimport asyncio\n\nasync def main():\n async with httpx.AsyncClient() as client:\n response = await client.get('https://example.com')\n print(response.status_code)\n print(response.text)\n\nasyncio.run(main())\n```\n\n---\n\n### 6. When to use which?\n\n#### Use **httpx** if:\n1. **You want the best developer experience:** The API is cleaner and more intuitive.\n2. **You need HTTP/2:** This is a non-negotiable requirement for some modern APIs.\n3. **You are migrating from `requests`:** It will be much easier to port your code.\n4. **You need both Sync and Async:** If you are writing a library or a CLI tool that needs to support both modes.\n5. **You are using FastAPI:** `httpx` is the standard recommendation for testing and making external calls within FastAPI.\n\n#### Use **aiohttp** if:\n1. **You are building a high-performance web server:** Using `aiohttp` as both the server and client keeps your stack unified.\n2. **Raw speed is the googleapis/google-auth-library-python#1 priority:** If you are making millions of requests and every millisecond of overhead matters.\n3. **Legacy codebases:** If you are working on an existing project that already uses `aiohttp`, there is rarely a compelling reason to switch.\n4. **Complex TCP/Networking requirements:** If you need deep control over the underlying transport layer.\n\n### Summary\nFor **90% of new projects**, **`httpx`** is the recommended choice because of its flexibility, HTTP/2 support, and modern API. Use **`aiohttp`** only if you need its server-side capabilities or are working in a performance-critical, async-only environment where every drop of optimization counts.","author":{"url":"https://github.com/chalmerlowe","@type":"Person","name":"chalmerlowe"},"datePublished":"2025-12-30T12:40:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/15134/google-cloud-python/issues/15134"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bf3ad691-70eb-e611-5445-cdf4269b5154 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E8A4:3B806F:10FF888:16EF89D:697328B6 |
| html-safe-nonce | 82c5e9dc25d325296cd3ea82e4b04cf19fe72390b050a190636b8068d95e9991 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOEE0OjNCODA2RjoxMEZGODg4OjE2RUY4OUQ6Njk3MzI4QjYiLCJ2aXNpdG9yX2lkIjoiMjkyNzQwODY1NDE4MzAzMjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4a1c4297410c5c0f2a4f48e0906be621ef37061f6646404c3858235a6b999149 |
| hovercard-subject-tag | issue:3805914811 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/google-cloud-python/15134/issue_layout |
| twitter:image | https://opengraph.githubassets.com/642f0f36a89daba04942f505748e6b19a3c6ef81cc9b34e985ca54ff97eb4fbe/googleapis/google-cloud-python/issues/15134 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/642f0f36a89daba04942f505748e6b19a3c6ef81cc9b34e985ca54ff97eb4fbe/googleapis/google-cloud-python/issues/15134 |
| og:image:alt | Add support for httpx for async transport. An exemplar PR to support this is available (it is currently closed to enable migration to the Python mono-repo). Both aiohttp and httpx are powerful libr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chalmerlowe |
| hostname | github.com |
| expected-hostname | github.com |
| None | 44ab3188c1dcfe3be0f9c3feca2e04e14fb79f120939ce2395e4f15ab96ec1d4 |
| turbo-cache-control | no-preview |
| go-import | github.com/googleapis/google-cloud-python git https://github.com/googleapis/google-cloud-python.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 16316451 |
| octolytics-dimension-repository_nwo | googleapis/google-cloud-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 16316451 |
| octolytics-dimension-repository_network_root_nwo | googleapis/google-cloud-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a5e2b48bd1260476599758f5d253b5d24092ab84 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width