Consider enabling HTTP2 by default
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 src/openai/_base_client.py 中的 _DefaultHttpxClient 和 pyproject.toml 中的依赖声明开始。确定启用 HTTP/2 时应如何处理 h2 依赖项以及现有的 custom http_client 行为;当默认客户端使用 HTTP/2,且不影响回退到 HTTP/1.1 或用户提供的客户端时,这项工作就完成了。
由索引模型根据 Issue 内容生成。
描述
Confirm this is a feature request for the Python library and not the underlying OpenAI API.
- This is a feature request for the Python library
Describe the feature or improvement you're requesting
Motivation
The OpenAI Python SDK uses httpx as its HTTP client, but HTTP/2 is disabled by default (http2=False in httpx.Client.__init__). This means every SDK user is stuck with HTTP/1.1 unless they manually configure a custom httpx.Client(http2=True) — a detail most users never discover.
Checked against v2.44.0 (latest), the _DefaultHttpxClient (src/openai/_base_client.py:834) still doesn't pass http2=True, and pyproject.toml only declares httpx>=0.23.0, <1 with no [http2] extra:
class _DefaultHttpxClient(httpx.Client):
def __init__(self, **kwargs: Any) -> None:
kwargs.setdefault("timeout", DEFAULT_TIMEOUT)
kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS)
kwargs.setdefault("follow_redirects", True)
super().__init__(**kwargs) # http2 defaults to False
Proposed Change
class _DefaultHttpxClient(httpx.Client):
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)
And optionally add an [http2] extra in pyproject.toml:
[project.optional-dependencies]
http2 = ["httpx[http2]"]
Prior Art
- Anthropic Python SDK (v0.104.1) already passes
http2through to the httpx transport (_base_client.py:871) - httpx has supported HTTP/2 since v0.13 (current declared dep is
httpx>=0.23.0) - The
h2package is lightweight, pure-Python, and widely used - HTTP/2 is supported across all major LLM API providers
Compatibility
- If the user provides a custom
http_client, their configuration takes precedence — no breaking change - If the server doesn't support HTTP/2, httpx gracefully downgrades to HTTP/1.1 (this has been handled by httpx for years)
- Connection limits already set by the SDK are fully compatible with HTTP/2
- No API changes needed — purely internal client configuration
Why Not Just Document It
Users can already do this manually:
import httpx
from openai import OpenAI
client = OpenAI(http_client=httpx.Client(http2=True))
But most users never find this knob. Making it the default means everyone benefits, especially developers using the SDK for production workloads with high request volumes. The downside is negligible: users who don't have h2 installed would get an ImportError at client construction (easily debuggable), or we mitigate this by making h2 optional and wrapping the import.
Additional context
Many popular LLM API providers already support HTTP/2 (confirmed via ALPN h2):
- OpenAI (
api.openai.com) - DeepSeek (
api.deepseek.com) - Anthropic (
api.anthropic.com)
Performance Impact
HTTP/2 brings meaningful improvements, especially for chat completions (frequent, streaming requests):
| Metric | HTTP/1.1 | HTTP/2 |
|---|---|---|
| TLS handshake | ~100ms | ~67ms (1-RTT) |
| Single request | ~134ms | ~123ms |
| Header compression | ❌ | ✅ HPACK |
| Multiplexing | ❌ (6 conn/browser limit) | ✅ (1-2 connections) |
| Connections for 10 requests | 10 | 1-2 |
The biggest win is connection reuse + multiplexing. In HTTP/1.1, every concurrent or back-to-back request opens a new TCP+TLS handshake. With HTTP/2, a single persistent connection handles multiple concurrent requests — critical for streaming chat.completions.create(stream=True) where the SDK opens many chunks over the same connection. Real-world test on DeepSeek API showed TLS handshake dropping from ~100ms to ~67ms just from HTTP/2's 1-RTT TLS.
- 主要语言
- Python
- 星标
- 31.8k
- 派生
- 7.3k
- 平均合并
- 1 天 3 小时
- 30 天内合并 PR
- 131
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openai/openai-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
openai/openai-python#4022 · 19 条评论 ·
维护者通常 1 天内回复
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providers可能已有人在做 @mohmedmm 于 9 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
openai/openai-python#4017 · 3 条评论 ·
维护者通常 1 天内回复
-
Querystring drops explicit empty-string scalar values可能已有人在做 @sylvesterkaczmarek 于 31 天前认领。 未关闭sdk-breaking-change v4
难度 2/5 1-3 小时 新手友好度 86/100
openai/openai-python#3837 ·
维护者通常 1 天内回复
-
Define + export `ServiceTiers` string literal可能已有人在做 @SparshGarg999 于 58 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
openai/openai-python#3556 · 3 条评论 ·
维护者通常 1 天内回复
-
Empty OPENAI_BASE_URL prevents fallback to default API endpoint可能已有人在做 @Sehastrajit-S 于 24 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 68/100
openai/openai-python#2927 · 6 条评论 ·
维护者通常 1 天内回复
查看 openai/openai-python 的全部 Issue
相似的 Issue
-
needs triage
难度 2/5 1-3 小时 新手友好度 83/100
darnitdevorg/darnit#587 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
accessibility good first issue
难度 1/5 1-3 小时 新手友好度 92/100
维护者通常 1 天内回复
-
llm translation
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
bug bug-hunt
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复