fix: 客户端响应结构损坏归入 LLMResponseError;3xx 不再误判成功

This commit is contained in:
lhl
2026-08-09 08:36:02 +08:00
parent 8bc5e5e571
commit 14062d1954
3 changed files with 59 additions and 6 deletions
+39 -2
View File
@@ -2,7 +2,12 @@ import pytest
import httpx
from genesis.inference.client import HttpLLMClient, LLMClient
from genesis.inference.exceptions import LLMNetworkError, LLMNotConfiguredError, LLMTimeoutError
from genesis.inference.exceptions import (
LLMNetworkError,
LLMNotConfiguredError,
LLMResponseError,
LLMTimeoutError,
)
from genesis.inference.types import ChatMessage
@@ -99,4 +104,36 @@ def test_chat_4xx_no_retry():
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)
assert calls["n"] == 1 # 4xx 不重试
assert calls["n"] == 1 # 4xx 不重试
def test_chat_3xx_no_retry():
# 3xx(重定向,httpx 不自动跟随)不得被当作成功,应立即抛 LLMNetworkError
calls = {"n": 0}
def handler(request):
calls["n"] += 1
return httpx.Response(302, headers={"Location": "https://elsewhere"})
with pytest.raises(LLMNetworkError):
make_client(handler).chat(
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)
assert calls["n"] == 1 # 3xx 不重试
def test_chat_malformed_response_raises_llm_response_error():
# 2xx 但响应体结构损坏(非 JSON / 缺 choices/message/content)→ LLMResponseError
def not_json(request):
return httpx.Response(200, text="not-json")
def missing_key(request):
return httpx.Response(200, json={"choices": []})
for handler in (not_json, missing_key):
with pytest.raises(LLMResponseError):
make_client(handler).chat(
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)