@@ -159,20 +159,40 @@ def _api_chat_with_id(
159159 # self.logger.exception(f"API request failed (id = {id}) with status {response.status_code}: {response.text}")
160160 self .logger .error (f"API request failed id={ id } status={ response .status_code } cost={ cost :.2f} s body={ response .text [:500 ]} " )
161161 return id , None
162+ # ✅ 1) 连接阶段超时:认为“根本连不上” => 统一抛 RuntimeError(Win/Linux 一致)
163+ except requests .exceptions .ConnectTimeout as e :
164+ cost = time .time () - start
165+ self .logger .error (f"API connect timeout (id={ id } ) cost={ cost :.2f} s: { e } " )
166+ raise RuntimeError (f"Cannot connect to LLM server (connect timeout): { e } " ) from e
167+
168+ # ✅ 2) 读超时:服务可达但没有数据(排队/推理太久)=> warn + None
169+ except requests .exceptions .ReadTimeout as e :
170+ cost = time .time () - start
171+ warnings .warn (f"API read timeout (id={ id } ) cost={ cost :.2f} s: { e } " , RuntimeWarning )
172+ return id , None
173+
174+ # ✅ 3) 其他 Timeout(极少)按 warn 处理
162175 except requests .exceptions .Timeout as e :
163176 cost = time .time () - start
164177 warnings .warn (f"API timeout (id={ id } ) cost={ cost :.2f} s: { e } " , RuntimeWarning )
165178 return id , None
166179
180+ # ✅ 4) ConnectionError:这里面 Win/Linux/urllib3 可能包装了各种“超时/断开”
167181 except requests .exceptions .ConnectionError as e :
168182 cost = time .time () - start
169183 msg = str (e ).lower ()
170184
171185 # requests/urllib3 有时会把 ReadTimeout 包装成 ConnectionError
172- if "timed out" in msg or " read timed out" in msg :
173- warnings .warn (f"API timeout (id={ id } ) cost={ cost :.2f} s: { e } " , RuntimeWarning )
186+ if "read timed out" in msg :
187+ warnings .warn (f"API read timeout (id={ id } ) cost={ cost :.2f} s: { e } " , RuntimeWarning )
174188 return id , None
175189
190+ # 连接阶段超时在某些平台也可能表现为 ConnectionError 文本
191+ if "connect timeout" in msg or ("timed out" in msg and "connect" in msg ):
192+ self .logger .error (f"API connect timeout (id={ id } ) cost={ cost :.2f} s: { e } " )
193+ raise RuntimeError (f"Cannot connect to LLM server (connect timeout): { e } " ) from e
194+
195+ # 其它连接错误:refused/reset/remote disconnected 等 => 统一抛 RuntimeError
176196 self .logger .error (f"API connection error (id={ id } ) cost={ cost :.2f} s: { e } " )
177197 raise RuntimeError (f"Cannot connect to LLM server: { e } " ) from e
178198
0 commit comments