EOF occurred in violation of protocol (_ssl.c:1123) The above exception was the ...

2026年01月06日 00:13 状态: processing

🚨 错误信息

Traceback (most recent call last): File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpx\_transports\default.py", line 101, in map_httpcore_exceptions yield File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpx\_transports\default.py", line 250, in handle_request resp = self._pool.handle_request(req) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpcore\_sync\connection_pool.py", line 256, in handle_request raise exc from None File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpcore\_sync\connection_pool.py", line 236, in handle_request response = connection.handle_request( File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpcore\_sync\http_proxy.py", line 288, in handle_request connect_response = self._connection.handle_request( File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpcore\_sync\connection.py", line 101, in handle_request raise exc File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpcore\_sync\connection.py", line 78, in handle_request stream = self._connect(request) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpcore\_sync\connection.py", line 156, in _connect stream = stream.start_tls(**kwargs) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpcore\_backends\sync.py", line 170, in start_tls raise exc File "E:\python3.9\lib\contextlib.py", line 135, in __exit__ self.gen.throw(type, value, traceback) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpcore\_exceptions.py", line 14, in map_exceptions raise to_exc(exc) from exc httpcore.ConnectError: EOF occurred in violation of protocol (_ssl.c:1123) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\openai\_base_client.py", line 982, in request response = self._client.send( File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpx\_client.py", line 914, in send response = self._send_handling_auth( File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpx\_client.py", line 942, in _send_handling_auth response = self._send_handling_redirects( File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpx\_client.py", line 979, in _send_handling_redirects response = self._send_single_request(request) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpx\_client.py", line 1014, in _send_single_request response = transport.handle_request(request) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpx\_transports\default.py", line 250, in handle_request resp = self._pool.handle_request(req) File "E:\python3.9\lib\contextlib.py", line 135, in __exit__ self.gen.throw(type, value, traceback) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\httpx\_transports\default.py", line 118, in map_httpcore_exceptions raise mapped_exc(message) from exc httpx.ConnectError: EOF occurred in violation of protocol (_ssl.c:1123) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "E:\learn\Vivo-闲聊-记忆\memory-system\test\testmodel.py", line 9, in <module> chat_completion = client.chat.completions.create( File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\openai\_utils\_utils.py", line 286, in wrapper return func(*args, **kwargs) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\openai\resources\chat\completions\completions.py", line 1192, in create return self._post( File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\openai\_base_client.py", line 1259, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) File "C:\Users\24256\AppData\Roaming\Python\Python39\site-packages\openai\_base_client.py", line 1014, in request raise APIConnectionError(request=request) from err openai.APIConnectionError: Connection error.

🤖 AI解决方案

错误分析报告

1. 错误翻译



原文:EOF occurred in violation of protocol (_ssl.c:1123)
译文:发生了违反协议的EOF错误(SSL连接意外中断)

2. 原因分析


这是一个 SSL/TLS 连接错误,发生在使用 OpenAI Python 客户端库发起 HTTPS 请求时。根本原因是:

连接中断:客户端(你的程序)与 OpenAI API 服务器之间的 SSL/TLS 握手过程被意外中断,可能是由于:

  • 网络代理设置问题

  • 防火墙/安全软件拦截了 SSL 流量

  • Python SSL 库与服务器不兼容

  • 网络不稳定导致连接中断
  • 错误位置:发生在 httpx HTTP 客户端的底层连接层,在尝试建立安全的 SSL/TLS 连接时失败。

    3. 解决方案

    #### 方案一:验证网络连接

    python
    import ssl
    import socket

    测试是否能连接到 OpenAI API


    try:
    with socket.create_connection(("api.openai.com", 443), timeout=10):
    print("✅ 网络连接正常")
    except socket.error as e:
    print(f"❌ 网络连接失败: {e}")

    #### 方案二:配置客户端使用更宽松的 SSL 设置

    python
    import openai
    from openai import OpenAI
    import ssl

    创建一个自定义 SSL 上下文


    ssl_context = ssl.create_default_context()

    可选:禁用证书验证(不推荐生产环境)


    ssl_context.check_hostname = False


    ssl_context.verify_mode = ssl.CERT_NONE

    创建带有自定义传输的客户端


    client = OpenAI(
    api_key="your-api-key",
    http_client=httpx.Client(
    verify=ssl_context, # 使用自定义 SSL 上下文
    timeout=30.0, # 增加超时时间
    )
    )

    #### 方案三:使用代理或绕过防火墙

    python
    import openai
    from openai import OpenAI
    import httpx

    如果有代理,配置代理


    client = OpenAI(
    api_key="your-api-key",
    http_client=httpx.Client(
    proxies="http://your-proxy:port", # 如果使用代理
    timeout=30.0,
    verify=False, # 临时关闭验证(仅用于调试)
    )
    )

    4. ️ 预防措施

    #### 最佳实践建议

  • 启用连接重试机制

  • python
    from tenacity import retry, stop_after_attempt, wait_exponential

    @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
    def call_openai_with_retry(client, messages):
    return client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=messages
    )

  • 使用更可靠的网络环境

  • - 确保网络稳定,避免使用公共 WiFi
    - 如果在中国大陆,检查网络是否能正常访问国际服务
    - 考虑使用 VPN 或代理服务器

    #### ️ 推荐检查工具

  • 网络诊断:使用 curl -v https://api.openai.com 测试连接

  • SSL 测试:使用 openssl s_client -connect api.openai.com:443 检查 SSL 握手

  • Python 环境:确保 Python 和 SSL 库是最新版本
  • ---

    提示:此错误通常是网络环境问题,建议先测试基本的网络连接性,然后逐步调整 SSL 配置。