Skip to content

Commit 0812616

Browse files
authored
fix: propagate Xinference rerank failures instead of returning empty results (#10051)
The Xinference rerank provider swallowed upstream failures and an uninitialized model by returning an empty list, which the retrieval manager read as a legitimate empty rerank and used to overwrite the fused candidates with nothing. Other providers (e.g. TEI) raise, and the manager only preserves the fused results on that path, so a transient Xinference outage silently degraded every knowledge-base query to zero hits. Raise instead, matching the provider contract and letting the manager's degradation keep the unreranked results. Fixes #10000
1 parent 8492df4 commit 0812616

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎astrbot/core/provider/sources/xinference_rerank_source.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ async def rerank(
9191
top_n: int | None = None,
9292
) -> list[RerankResult]:
9393
if not self.model:
94-
logger.error("Xinference rerank model is not initialized.")
95-
return []
94+
raise RuntimeError("Xinference rerank model is not initialized")
9695
try:
9796
response = await self.model.rerank(documents, query, top_n)
9897
results = response.get("results", [])
@@ -111,9 +110,12 @@ async def rerank(
111110
for result in results
112111
]
113112
except Exception as e:
113+
# An empty list reads as a legitimate empty rerank to the caller,
114+
# which would overwrite the fused candidates with nothing (#10000).
115+
# Propagate so the retrieval manager keeps the unreranked results.
114116
logger.error(f"Xinference rerank failed: {e}")
115117
logger.debug(f"Xinference rerank failed with exception: {e}", exc_info=True)
116-
return []
118+
raise
117119

118120
async def terminate(self) -> None:
119121
"""关闭客户端会话"""
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Regression tests for the Xinference rerank provider failure contract (#10000).
2+
3+
The provider used to swallow upstream failures and return an empty list,
4+
which the retrieval manager read as a legitimate empty rerank result and
5+
used to overwrite the fused candidates. Failures now propagate so the
6+
manager can fall back to the unreranked results.
7+
"""
8+
9+
from unittest.mock import AsyncMock, Mock
10+
11+
import pytest
12+
13+
from astrbot.core.provider.sources.xinference_rerank_source import (
14+
XinferenceRerankProvider,
15+
)
16+
17+
18+
@pytest.fixture
19+
def provider() -> XinferenceRerankProvider:
20+
instance = XinferenceRerankProvider.__new__(XinferenceRerankProvider)
21+
instance.model = Mock()
22+
return instance
23+
24+
25+
@pytest.mark.asyncio
26+
async def test_rerank_failure_propagates_instead_of_empty_list(provider):
27+
provider.model.rerank = AsyncMock(side_effect=RuntimeError("upstream down"))
28+
29+
with pytest.raises(RuntimeError, match="upstream down"):
30+
await provider.rerank(query="q", documents=["a", "b"])
31+
32+
33+
@pytest.mark.asyncio
34+
async def test_uninitialized_model_raises_instead_of_empty_list(provider):
35+
provider.model = None
36+
37+
with pytest.raises(RuntimeError, match="not initialized"):
38+
await provider.rerank(query="q", documents=["a"])

0 commit comments

Comments
 (0)