fix: 客户端响应结构损坏归入 LLMResponseError;3xx 不再误判成功
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import time
|
||||
from typing import Protocol, Sequence
|
||||
|
||||
import httpx
|
||||
|
||||
from .exceptions import LLMNetworkError, LLMNotConfiguredError, LLMTimeoutError
|
||||
from .exceptions import (
|
||||
LLMNetworkError,
|
||||
LLMNotConfiguredError,
|
||||
LLMResponseError,
|
||||
LLMTimeoutError,
|
||||
)
|
||||
from .types import ChatMessage, TokenUsage
|
||||
|
||||
|
||||
@@ -81,9 +87,16 @@ class HttpLLMClient:
|
||||
continue
|
||||
if resp.status_code >= 400:
|
||||
raise LLMNetworkError(f"LLM HTTP {resp.status_code}: {resp.text[:200]}")
|
||||
if not (200 <= resp.status_code < 300):
|
||||
# 3xx 重定向不自动跟随,不得误判为成功
|
||||
raise LLMNetworkError(f"LLM HTTP {resp.status_code}: {resp.text[:200]}")
|
||||
|
||||
data = resp.json()
|
||||
content = data["choices"][0]["message"]["content"]
|
||||
try:
|
||||
data = resp.json()
|
||||
content = data["choices"][0]["message"]["content"]
|
||||
except (json.JSONDecodeError, KeyError, IndexError, TypeError) as exc:
|
||||
# 2xx 但响应结构损坏(非 JSON / 缺字段)→ 结构化错误,不重试
|
||||
raise LLMResponseError(f"LLM 响应结构损坏: {exc}") from exc
|
||||
usage_raw = data.get("usage", {})
|
||||
usage = TokenUsage(
|
||||
input_tokens=usage_raw.get("prompt_tokens", 0),
|
||||
|
||||
Reference in New Issue
Block a user