Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(embedding): use word-boundary matching for numeric error patterns in classify_api_error (#2370)

Binghui Chen7bd767034248
diff --git a/openviking/utils/model_retry.py b/openviking/utils/model_retry.py
index d90eac8eb..56bea7e2e 100644
--- a/openviking/utils/model_retry.py
+++ b/openviking/utils/model_retry.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio
import logging
import random
+import re
import threading
import time
from typing import Awaitable, Callable, TypeVar
@@ -68,6 +69,33 @@ TRANSIENT_API_ERROR_PATTERNS = (
"connection reset",
)
+# Pre-compile regex for numeric status-code patterns to avoid substring false positives
+# (e.g. "413" matching inside request IDs like "d7c9130f344..." or "req-413-abcd").
+_NUMERIC_PATTERN_RE: dict[str, re.Pattern] = {}
+
+
+def _get_numeric_pattern_re(pattern: str) -> re.Pattern:
+ if pattern not in _NUMERIC_PATTERN_RE:
+ escaped = re.escape(pattern)
+ _NUMERIC_PATTERN_RE[pattern] = re.compile(
+ rf"(?:\b(?:error\s*code|status(?:\s*code)?|http(?:\s*status)?|code)"
+ rf"\s*[:=]?\s*{escaped}(?!\w)|(?<![\w-]){escaped}(?![\w-]))"
+ )
+ return _NUMERIC_PATTERN_RE[pattern]
+
+
+def _pattern_matches(text_lower: str, text_compact: str, pattern: str) -> bool:
+ """Check if pattern matches in text, using token-aware matching for numeric patterns.
+
+ Numeric-only patterns (e.g. ``"413"``) must look like HTTP status codes, not
+ request ID fragments. Non-numeric patterns use plain substring matching as before.
+ """
+ if pattern.isdigit():
+ return bool(_get_numeric_pattern_re(pattern).search(text_lower)) or bool(
+ _get_numeric_pattern_re(pattern).search(text_compact)
+ )
+ return pattern in text_lower or pattern in text_compact
+
def classify_api_error(error: Exception) -> str:
"""Classify an API error as permanent, quota_exceeded, transient, or unknown.
@@ -89,13 +117,14 @@ def classify_api_error(error: Exception) -> str:
text_lower = text.lower()
text_compact = text_lower.replace(" ", "")
for pattern in INPUT_TOO_LARGE_PATTERNS:
- if pattern in text_lower or pattern in text_compact:
+ if _pattern_matches(text_lower, text_compact, pattern):
return ERROR_CLASS_INPUT_TOO_LARGE
for text in texts:
text_lower = text.lower()
+ text_compact = text_lower.replace(" ", "")
for pattern in PERMANENT_API_ERROR_PATTERNS:
- if pattern in text_lower:
+ if _pattern_matches(text_lower, text_compact, pattern):
return ERROR_CLASS_PERMANENT
# Check quota_exceeded *before* transient so that "429 … AccountQuotaExceeded"
@@ -108,8 +137,9 @@ def classify_api_error(error: Exception) -> str:
for text in texts:
text_lower = text.lower()
+ text_compact = text_lower.replace(" ", "")
for pattern in TRANSIENT_API_ERROR_PATTERNS:
- if pattern in text_lower:
+ if _pattern_matches(text_lower, text_compact, pattern):
return ERROR_CLASS_TRANSIENT
return ERROR_CLASS_UNKNOWN
diff --git a/tests/unit/test_model_retry.py b/tests/unit/test_model_retry.py
index 5e0f7a844..3bc02aac7 100644
--- a/tests/unit/test_model_retry.py
+++ b/tests/unit/test_model_retry.py
@@ -154,3 +154,43 @@ def test_retry_sync_does_not_retry_input_too_large():
retry_sync(_call, max_retries=5)
assert attempts["count"] == 1
+
+
+# --- numeric pattern word-boundary tests ---
+
+
+def test_429_with_request_id_containing_413_is_transient():
+ """A 429 error whose request ID happens to contain '413' must NOT be
+ misclassified as INPUT_TOO_LARGE (the original bug)."""
+ error = RuntimeError(
+ "Volcengine hybrid embedding failed: Error code: 429 - "
+ "{'error': {'code': 'ModelAccountRpmRateLimitExceeded', "
+ "'message': 'RPM limit exceeded', 'param': '', "
+ "'type': 'TooManyRequests'}, "
+ "'request_id': '0217801248873024288fe53d7c9130f34413480585e683685bc95'}"
+ )
+ assert classify_api_error(error) == ERROR_CLASS_TRANSIENT
+
+
+def test_429_with_hyphenated_request_id_containing_413_is_transient():
+ """Numeric status codes must not match hyphen-delimited request ID fragments."""
+ error = RuntimeError(
+ "Volcengine hybrid embedding failed: Error code: 429 - "
+ "{'error': {'code': 'ModelAccountRpmRateLimitExceeded', "
+ "'message': 'RPM limit exceeded', 'type': 'TooManyRequests'}, "
+ "'request_id': 'req-413-abcd'}"
+ )
+ assert classify_api_error(error) == ERROR_CLASS_TRANSIENT
+
+
+def test_numeric_status_code_inside_longer_number_is_not_matched():
+ """Status code patterns must not match inside longer numbers
+ (e.g. '400' must not match '1400')."""
+ assert classify_api_error(RuntimeError("status: 1400 OK")) == "unknown"
+ assert classify_api_error(RuntimeError("status: 5020 OK")) == "unknown"
+
+
+def test_numeric_status_code_with_compact_error_code_context_still_matches():
+ assert classify_api_error(RuntimeError("Error code:413-Payload Too Large")) == (
+ ERROR_CLASS_INPUT_TOO_LARGE
+ )