[fix] plugin: repeat the request on 5xx as well

A server that is not ready does not always take its time about saying so.
Ollama answers 5xx immediately while a model is still being loaded, and a
gateway with no upstream yet does the same, so the failure arrives at
once rather than as a timeout.

Both mean "not right now" and are worth one more attempt.  4xx keeps
being final -- a wrong API key or an unknown model name is not going to
change between two requests.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
jasonwitty
2026-08-11 11:58:31 -07:00
parent 4a582c0a15
commit 0cc3748866
2 changed files with 40 additions and 6 deletions
+25 -1
View File
@@ -473,7 +473,7 @@ class PluginAISummary(SearxTestCase):
self.assertEqual(res.status_code, 502)
self.assertEqual(2, len(attempts))
def test_endpoint_does_not_retry_an_error_status(self):
def test_endpoint_does_not_retry_a_client_error(self):
# a wrong API key or an unknown model does not fix itself
attempts = []
bad = sse_stream_mock([], status_code=401)
@@ -492,6 +492,30 @@ class PluginAISummary(SearxTestCase):
self.assertEqual(res.status_code, 502)
self.assertEqual(1, len(attempts))
def test_endpoint_retries_a_server_error(self):
# 5xx means "not right now" -- e.g. a model that is still loading;
# this is the failure that arrives immediately instead of timing out
attempts = []
ok = sse_stream_mock([{"choices": [{"delta": {"content": "hi"}}]}])
bad = sse_stream_mock([], status_code=503)
@contextmanager
def stream(*_args, **_kwargs):
attempts.append(1)
src = bad if len(attempts) == 1 else ok
with src.stream() as resp:
yield resp
client_mock = Mock()
client_mock.stream = stream
self.mock_upstream(client_mock)
res = self.client.post("/ai_summary", json={"messages": [{"role": "user", "content": "hi"}]})
self.assertEqual(res.status_code, 200)
self.assertEqual(2, len(attempts))
lines = [json.loads(line) for line in res.data.decode().splitlines() if line]
self.assertEqual(lines[0], {"delta": "hi"})
def test_endpoint_upstream_error(self):
self.mock_upstream(sse_stream_mock([], status_code=500))