From e9022fc2ed34d62d5cbda13d899eed81337bdcf1 Mon Sep 17 00:00:00 2001 From: KrisTHL181 Date: Wed, 8 Jul 2026 22:18:34 +0800 Subject: [PATCH 1/2] Enable HTTP/2 by default in httpx client Adds http2=True to both _DefaultHttpxClient and _DefaultAsyncHttpxClient, and provides an [http2] optional dependency extra for httpx[http2]. HTTP/2 offers meaningful performance benefits: connection reuse, multiplexing, HPACK header compression, and 1-RTT TLS handshake. All major LLM API providers (OpenAI, DeepSeek, Anthropic) already support HTTP/2 via ALPN negotiation. Falls back gracefully to HTTP/1.1 if the server doesn't support h2, or if the h2 package is not installed. --- pyproject.toml | 1 + src/openai/_base_client.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 423289c26e..262e3a9dd3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,7 @@ Repository = "https://github.com/openai/openai-python" [project.optional-dependencies] aiohttp = ["aiohttp", "httpx_aiohttp>=0.1.9"] +http2 = ["httpx[http2]"] realtime = ["websockets >= 13, < 16"] datalib = ["numpy >= 1", "pandas >= 1.2.3", "pandas-stubs >= 1.1.0.11"] voice_helpers = ["sounddevice>=0.5.1", "numpy>=2.0.2"] diff --git a/src/openai/_base_client.py b/src/openai/_base_client.py index 216b36aabd..eccd5a737a 100644 --- a/src/openai/_base_client.py +++ b/src/openai/_base_client.py @@ -836,6 +836,7 @@ def __init__(self, **kwargs: Any) -> None: kwargs.setdefault("timeout", DEFAULT_TIMEOUT) kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS) kwargs.setdefault("follow_redirects", True) + kwargs.setdefault("http2", True) super().__init__(**kwargs) @@ -1423,6 +1424,7 @@ def __init__(self, **kwargs: Any) -> None: kwargs.setdefault("timeout", DEFAULT_TIMEOUT) kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS) kwargs.setdefault("follow_redirects", True) + kwargs.setdefault("http2", True) super().__init__(**kwargs) From 391e07e2b4772c06160edbf1179127e9cc9e02c8 Mon Sep 17 00:00:00 2001 From: KrisTHL181 Date: Wed, 8 Jul 2026 22:48:04 +0800 Subject: [PATCH 2/2] feat: use _HTTP2_AVAILABLE flag instead of hardcoded True Check if h2 is importable at module level and set _HTTP2_AVAILABLE accordingly. This lets pip install openai[http2] transparently enable HTTP/2 without breaking users who don't have h2 installed. --- src/openai/_base_client.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/openai/_base_client.py b/src/openai/_base_client.py index eccd5a737a..60d997caa0 100644 --- a/src/openai/_base_client.py +++ b/src/openai/_base_client.py @@ -39,6 +39,13 @@ from httpx import URL from pydantic import PrivateAttr +try: + import h2 # noqa: F401 + + _HTTP2_AVAILABLE = True +except ImportError: + _HTTP2_AVAILABLE = False + from . import _exceptions from ._qs import Querystring from ._files import to_httpx_files, async_to_httpx_files @@ -836,7 +843,7 @@ def __init__(self, **kwargs: Any) -> None: kwargs.setdefault("timeout", DEFAULT_TIMEOUT) kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS) kwargs.setdefault("follow_redirects", True) - kwargs.setdefault("http2", True) + kwargs.setdefault("http2", _HTTP2_AVAILABLE) super().__init__(**kwargs) @@ -1424,7 +1431,7 @@ def __init__(self, **kwargs: Any) -> None: kwargs.setdefault("timeout", DEFAULT_TIMEOUT) kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS) kwargs.setdefault("follow_redirects", True) - kwargs.setdefault("http2", True) + kwargs.setdefault("http2", _HTTP2_AVAILABLE) super().__init__(**kwargs)