Skip to content

Migrate from httpx to httpx2 #4427

Done

Description

@r4victor

httpx sees little maintenance, and the ecosystem is moving to httpx2, Pydantic's maintained fork of httpx 0.28.1. openai 3.x already requires httpx2, starlette's TestClient prefers it, and opentelemetry-instrumentation-httpx supports it. dstack already uses httpx2 for gateway auth, for its pool fixes (#4338), while the shared proxy code still uses httpx, so the gateway runs both libraries and their exception classes don't mix.

We currently use three HTTP clients:

  • requests (+ requests-unixsocket): APIClient (CLI and Python API), backend API clients, runner/shim client, docker registry, REST plugin.
  • httpx: server and gateway proxy, gateway client, probes, router worker sync, job replica client, fluentbit, vastai.
  • httpx2: gateway auth and gateway server client.

Proposal

  1. Replace httpx with httpx2 everywhere. This is mostly mechanical, since httpx2 keeps the httpx API. It includes switching the OTel instrumentor to HTTPX2ClientInstrumentor, which HTTPXClientInstrumentor doesn't cover:

SQLAlchemyInstrumentor().instrument(engine=engine.sync_engine)
_register_db_span_renaming(engine.sync_engine)
HTTPXClientInstrumentor().instrument()
# unixsocket calls to runners should not be instrumented: every runner
# tunnel has a unique socket path that becomes the net.peer.name metric
# attribute, so the SDK would accumulate state for an unbounded number of series.
RequestsInstrumentor().instrument(excluded_urls=r"http\+unix://")

  1. Optionally, move our own requests code to httpx2 too, so we have one client for sync and async code, one TLS and proxy model, one exception hierarchy, one OTel instrumentor and one mocking approach. It would also drop requests-unixsocket, since httpx2 supports unix sockets natively. requests itself stays installed through third-party dependencies (gpuhunt, docker, kubernetes, google/azure SDKs, etc.).

TLS: certifi vs the system trust store

httpx2 no longer uses certifi. For verify=True (2.15.1+):

  • SSL_CERT_FILE / SSL_CERT_DIR replace the default store on all OSes.
  • macOS and Windows use the OS verifier via truststore (Keychain, Windows store).
  • Linux loads the system CA files (OpenSSL's compiled-in paths, then the common distro bundle locations) into a stdlib SSLContext once.
  • With no system CA bundle, all HTTPS fails with CERTIFICATE_VERIFY_FAILED. There is no certifi fallback.

User impact:

Options:

Python 3.10

httpx2 2.15+ requires Python 3.11, and on 3.10 the resolver stays on 2.14.0, which gets no further fixes. The migration should come with dropping Python 3.10, which depends on #4423.

Behaviour changes when moving off requests (optional part)

  • Timeouts: httpx2 defaults to a 5s timeout, while requests has none. APIClient sets no timeout today, so long calls would start failing unless we set one explicitly.

for _ in range(_MAX_RETRIES):
try:
# TODO: set adequate timeout here or everywhere the method is used
resp = self._s.request(method, f"{self._base_url}/{path}", **kwargs)
break
except requests.exceptions.ConnectionError as e:
self._logger.debug("Could not connect to server: %s", e)
time.sleep(_RETRY_INTERVAL)
else:
raise ClientError(f"Failed to connect to dstack server {self._base_url}")

  • Redirects: httpx2 doesn't follow them by default. APIClient treats any status below 400 as success, so a 3xx needs explicit handling.
  • Exceptions: requests.ConnectionError becomes httpx2.ConnectError / TransportError, and raise_for_status() also raises on 3xx. Python API users catching requests exceptions around dstack calls would break.
  • TLS errors and retries: httpcore2 doesn't map every TLS error, so ssl.SSLError can escape the retry loop. See ssl.SSLError during a request is no longer retried since 3.14.1 openai/openai-python#3969.
  • API differences: data=<str> becomes content=, .ok becomes .is_success, iter_content becomes iter_bytes.
  • Unix sockets: http+unix:// URLs become HTTPTransport(uds=...) with a regular base URL.
  • Tests: requests_mock (300+ usages) moves to httpx2.MockTransport or an httpx2 mocking plugin.

Plan

  • Drop Python 3.10 ([Feature]: Support Python version upgrade for gateways and migrate from Python 3.10 #4423)
  • Decide on TLS policy (A or B)
  • httpx → httpx2 in server, proxy, gateway, fluentbit, vastai; switch OTel instrumentor
  • (optional) APIClient and CLI → httpx2, with explicit timeouts, redirect handling, and release notes on REQUESTS_CA_BUNDLE → SSL_CERT_FILE
  • (optional) Runner/shim client and backend API clients → httpx2
  • Test the CLI on macOS with a private-CA server, on uv-managed Python, and in a minimal container

Activity

  1. github-actions commented on Nov 28, 2026

    @github-actions

    This issue is stale because it has been open for 30 days with no activity.

  2. self-assigned this
    on Nov 30, 2026
  3. r4victor commented on Nov 30, 2026

    @r4victor
    CollaboratorAuthor

    On TLS, let's start with B, but treat the certifi fallback as transitional:

    • Use the httpx2 default context (httpcore2.default_ssl_context()): SSL_CERT_FILE / SSL_CERT_DIR if set, the OS verifier via truststore on macOS and Windows, the system CA files on Linux.
    • On Linux only, if neither env var is set and no system CAs are found, fall back to certifi and log a warning recommending to install ca-certificates (or set SSL_CERT_FILE).
    • Eventually, once users have had time to fix their images (e.g. some releases after the CLI moves to httpx2), drop the certifi fallback and turn the warning into an error with the same recommendation.

    Detecting "no system CAs" needs both cert_store_stats()["x509_ca"] == 0 and no hashed certs in OpenSSL's CA directory. uv-managed Python on Debian has no /etc/ssl/cert.pem and uses only /etc/ssl/certs, whose certs load lazily, so the count alone reads 0 even with a working store.

    Which popular images lack a CA store (checked for /etc/ssl/cert.pem, /etc/ssl/certs/ca-certificates.crt, /etc/pki/tls/certs/ca-bundle.crt):

    No CA store:

    • debian, including the full debian:bookworm, not only -slim
    • ubuntu
    • node:*-slim
    • busybox, scratch

    Has a CA store:

    • python and python:*-slim
    • node (non-slim)
    • alpine
    • fedora, redhat/ubi9-minimal, rockylinux:9-minimal, amazonlinux:2023
    • nvidia/cuda:*-base-*, and images built on it, such as the dstack base image
    • gcr.io/distroless/python3-debian12

    Full Debian and Ubuntu installs (VMs, servers, desktops) ship ca-certificates; only the container base images strip it. In debian:bookworm-slim, uv python install works fine but that Python then fails every HTTPS request with CERTIFICATE_VERIFY_FAILED, so users may not notice the missing store until Python makes a request.

    For the first step (httpx → httpx2 only, requests unchanged), the only HTTPS caller moving to httpx2 is the VastAI backend client on the server, and the server image has ca-certificates. The warning matters mostly for the later CLI / APIClient migration.

  4. Kludex commented on Nov 30, 2026

    @Kludex
  5. Kludex commented on Nov 30, 2026

    @Kludex

    Oh... I'm late... 😅

  6. r4victor commented on Nov 30, 2026

    @r4victor
    CollaboratorAuthor

    @Kludex, yup, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions