Skip to content

About

Fix Python httpx HTTP/2 timeout with Ollama — one line

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

🔧 Fix Ollama + httpx HTTP/2 Timeout

Python httpx 连接 Ollama 时神秘超时?一行代码解决!

License: MIT Python 3.10+


🤔 你遇到过吗?

import httpx

# 这样调用 Ollama → 永远超时 😱
async with httpx.AsyncClient() as client:
    resp = await client.post(
        "http://localhost:11434/api/chat",
        json={"model": "qwen3", "messages": [...]},
        timeout=60
    )
# httpx.ReadTimeout ... 等了 60 秒然后挂了

但用 curl 就完全正常:

curl http://localhost:11434/api/chat -d '{...}'  # ✅ 秒回

同样的端点,httpx 崩了,curl 好好的 —— 诡异吧?


🔍 根因:HTTP/2 是"元凶"

httpx 默认启用 HTTP/2 协议协商(RFC 7540 ALPN)。连接时它会先问服务器"你支持 HTTP/2 吗?"

Ollama 的 HTTP 服务器不支持 HTTP/2。 更糟的是,它也不优雅地拒绝——有些版本直接沉默,导致 httpx 一直等待协商完成,直到超时。

httpx: "Hi Ollama,支持 HTTP/2 吗?🙋"
Ollama: "......" (沉默)
httpx: "......还在等..."
        ↓ 60 秒后
httpx: "ReadTimeout!💀"

而 curl 默认走 HTTP/1.1,不触发协商,所以完全正常。


✅ 修复:一行代码

httpx.AsyncClient

# ❌ 旧代码 — 会超时
async with httpx.AsyncClient() as client:
    ...

# ✅ 修复 — 显式禁用 HTTP/2
async with httpx.AsyncClient(http2=False) as client:
    ...

httpx.Client(同步版)

# ✅ 同步版本也一样
with httpx.Client(http2=False) as client:
    ...

就这么简单!http2=False 强制走 HTTP/1.1,Ollama 开心了,你也开心了 🎉


📦 适用场景

这个问题不只影响 Hermes Agent。任何用 httpx 连接 Ollama 的 Python 项目都会中招:

项目 影响
Hermes Agent 使用 Ollama 本地模型时超时
LiteLLM 作为 Ollama 代理时
OpenWebUI 后端连接 Ollama 时
LangChain Ollama 集成
任何用 httpx + Ollama 的项目 全都可能中招 💀

🧪 复现 & 验证

# 1. 确保 Ollama 在运行
ollama serve

# 2. 跑测试脚本
python test_repro.py

test_repro.py:

import asyncio
import httpx

async def test():
    # ❌ 这个会超时
    try:
        async with httpx.AsyncClient(timeout=10) as c:
            r = await c.get("http://localhost:11434/api/tags")
            print(f"默认(HTTP/2): {r.status_code}")
    except httpx.ReadTimeout:
        print("默认(HTTP/2): ❌ 超时了!")

    # ✅ 这个正常
    async with httpx.AsyncClient(http2=False, timeout=10) as c:
        r = await c.get("http://localhost:11434/api/tags")
        print(f"http2=False:   ✅ {r.status_code}")

asyncio.run(test())

🗺️ 排查历程

我们在 Hermes Agent + Ollama 上踩了这个坑,排查过程堪称侦探剧:

1. curl 正常,Python 超时          → 不是网络问题
2. urllib 正常,httpx 超时         → 锁定 httpx
3. httpx 日志: "HTTP/2 negotiation" → 根因浮现
4. http2=False                  → 🎯 秒修复!

🔗 相关讨论


⭐ Star History

如果这个 trick 帮到了你,点个 Star 让更多人看到 ⭐


📄 License

MIT © 2026

About

Fix Python httpx HTTP/2 timeout with Ollama — one line

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages