-
-
Notifications
You must be signed in to change notification settings - Fork 146
Retry transient embedding API failures #476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
mia-fourier
wants to merge
1
commit into
mnemosyne-oss:main
from
mia-fourier:fix/transient-embedding-retry
+95
−3
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Regression coverage for transient embedding endpoint failures.""" | ||
|
|
||
| import io | ||
| import json | ||
| import urllib.error | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from mnemosyne.core import embeddings | ||
|
|
||
|
|
||
| class Response: | ||
| def __init__(self, payload): | ||
| self.body = io.BytesIO(json.dumps(payload).encode()) | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, *_args): | ||
| return False | ||
|
|
||
| def read(self): | ||
| return self.body.read() | ||
|
|
||
|
|
||
| def test_embed_api_retries_transient_network_failures(monkeypatch): | ||
| monkeypatch.setenv("MNEMOSYNE_EMBEDDING_API_URL", "http://127.0.0.1:11435/v1") | ||
| result = Response({"data": [{"embedding": [0.25, 0.75]}]}) | ||
| failures = [urllib.error.URLError(OSError(65, "No route to host")), TimeoutError(), result] | ||
|
|
||
| with patch("urllib.request.urlopen", side_effect=failures) as request, \ | ||
| patch("mnemosyne.core.embeddings.random.uniform", return_value=0.1), \ | ||
| patch("mnemosyne.core.embeddings.time.sleep") as sleep: | ||
| vectors = embeddings._embed_api(["retry me"]) | ||
|
|
||
| assert vectors.tolist() == [[0.25, 0.75]] | ||
| assert request.call_count == 3 | ||
| assert [call.args[0] for call in sleep.call_args_list] == [0.6, 1.1] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("status", [429, 503]) | ||
| def test_embed_api_retries_transient_http_errors(monkeypatch, status): | ||
| monkeypatch.setenv("MNEMOSYNE_EMBEDDING_API_URL", "http://127.0.0.1:11435/v1") | ||
| error = urllib.error.HTTPError("http://example", status, "transient", {}, None) | ||
| result = Response({"data": [{"embedding": [0.25, 0.75]}]}) | ||
|
|
||
| with patch("urllib.request.urlopen", side_effect=[error, result]) as request, \ | ||
| patch("mnemosyne.core.embeddings.random.uniform", return_value=0), \ | ||
| patch("mnemosyne.core.embeddings.time.sleep") as sleep: | ||
| vectors = embeddings._embed_api(["retry me"]) | ||
|
|
||
| assert vectors.tolist() == [[0.25, 0.75]] | ||
| assert request.call_count == 2 | ||
| sleep.assert_called_once_with(0.5) | ||
|
|
||
|
|
||
| def test_embed_api_does_not_retry_nontransient_client_error(monkeypatch): | ||
| monkeypatch.setenv("MNEMOSYNE_EMBEDDING_API_URL", "http://127.0.0.1:11435/v1") | ||
| error = urllib.error.HTTPError("http://example", 400, "bad request", {}, None) | ||
|
|
||
| with patch("urllib.request.urlopen", side_effect=error) as request, \ | ||
| patch("mnemosyne.core.embeddings.time.sleep") as sleep: | ||
| assert embeddings._embed_api(["bad request"]) is None | ||
|
|
||
| assert request.call_count == 1 | ||
| sleep.assert_not_called() | ||
|
|
||
|
|
||
| def test_embed_api_stops_after_three_transient_attempts(monkeypatch): | ||
| monkeypatch.setenv("MNEMOSYNE_EMBEDDING_API_URL", "http://127.0.0.1:11435/v1") | ||
| error = urllib.error.URLError(OSError(65, "No route to host")) | ||
|
|
||
| with patch("urllib.request.urlopen", side_effect=error) as request, \ | ||
| patch("mnemosyne.core.embeddings.random.uniform", return_value=0), \ | ||
| patch("mnemosyne.core.embeddings.time.sleep") as sleep: | ||
| assert embeddings._embed_api(["offline"]) is None | ||
|
|
||
| assert request.call_count == 3 | ||
| assert sleep.call_count == 2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Duplicates and diverges from the existing
_is_rate_limit_errorhelper.The fallback branch reimplements the rate-limit string matching that
_is_rate_limit_error(Line 221) already provides, but drops the"too many requests"case that the existing helper checks. This creates two slightly different classifiers for the same concept — an embedding API error whose message is"Too many requests"(no"429"or"rate limit"phrase) won't be retried here even though_is_rate_limit_errorwould flag it as transient.♻️ Proposed fix: reuse the existing helper
def _is_transient_embedding_error(exc: Exception) -> bool: """Return whether an API embedding request is safe to retry.""" if isinstance(exc, urllib.error.HTTPError): return exc.code == 429 or 500 <= exc.code < 600 if isinstance(exc, (urllib.error.URLError, TimeoutError, ConnectionError, OSError)): return True - message = str(exc).lower() - return "429" in message or "rate limit" in message or "rate-limit" in message + return _is_rate_limit_error(exc)📝 Committable suggestion
🤖 Prompt for AI Agents