Skip to content

Official Codebase of 3DCodeVerse

Python

models: the SDK ladder and the retry wrapper, once instead of twice

gaoypeng24c867a58d61
diff --git a/harness/codeverse/models/anthropic.py b/harness/codeverse/models/anthropic.py
index f79ca8f9..863ecb8e 100644
--- a/harness/codeverse/models/anthropic.py
+++ b/harness/codeverse/models/anthropic.py
@@ -30,9 +30,15 @@ from codeverse.contracts.chat import (
)
from codeverse.contracts.common import Usage
from codeverse.models.base import ModelError
-from codeverse.models.parts import BoundedCache, Stopwatch, image_b64
+from codeverse.models.parts import (
+ BoundedCache,
+ Stopwatch,
+ classify_sdk_exception,
+ image_b64,
+ with_logged_retries,
+)
from codeverse.models.pricing import cache_write_surcharge, estimate_cost
-from codeverse.models.retry import MAX_WAIT_S, with_retries
+from codeverse.models.retry import MAX_WAIT_S
from codeverse.models.schema_utils import (
JsonParseError,
parse_json_lenient,
@@ -238,24 +244,10 @@ log = logging.getLogger(__name__)
def classify_exception(exc: BaseException) -> ModelError:
- """Map anthropic SDK exceptions onto ``ModelError``."""
- if isinstance(exc, ModelError):
- return exc
+ """Map anthropic SDK exceptions onto ``ModelError`` (the shared ladder in parts.py)."""
import anthropic
- if isinstance(exc, anthropic.APIStatusError):
- status = int(getattr(exc, "status_code", 0) or 0)
- retry = status in (408, 409, 429, 529) or status >= 500
- return ModelError(
- f"Anthropic API error {status}: {exc.message}", retryable=retry, status=status
- )
- if isinstance(exc, anthropic.APITimeoutError):
- return ModelError(f"Anthropic request timed out: {exc}", retryable=True, status=408)
- if isinstance(exc, anthropic.APIConnectionError):
- return ModelError(f"Anthropic connection error: {exc}", retryable=True)
- if isinstance(exc, anthropic.APIError):
- return ModelError(f"Anthropic API error: {exc}", retryable=False)
- return ModelError(f"Anthropic unexpected error: {type(exc).__name__}: {exc}", retryable=False)
+ return classify_sdk_exception(exc, anthropic, "Anthropic", extra_retry=frozenset({529}))
class AnthropicModel:
@@ -320,27 +312,9 @@ class AnthropicModel:
except Exception as exc: # noqa: BLE001 - classified
raise classify_exception(exc) from exc
- def on_retry(n: int, exc: BaseException, delay: float) -> None:
- log.warning(
- "anthropic %s attempt %d/%d failed (%s); retrying in %.1fs",
- self.model,
- n,
- self.max_attempts,
- exc,
- delay,
- )
-
- # ChatRequest.max_wait_s is not honoured here: with_retries has no deadline, and its
- # 6 attempts x <= 5 s backoff bound one call to ~20 s of waiting plus the round-trips.
- return with_retries(
- attempt,
- is_retryable=lambda e: isinstance(e, ModelError) and e.retryable,
- attempts=self.max_attempts,
- base_delay=self.base_delay,
- max_delay=self.max_delay,
- on_retry=on_retry,
- sleep=self._sleep,
- )
+ return with_logged_retries(attempt, label="anthropic", model=self.model,
+ attempts=self.max_attempts, base_delay=self.base_delay,
+ max_delay=self.max_delay, sleep=self._sleep, log=log)
def _once(self, kwargs: dict[str, Any], request: ChatRequest) -> ChatResponse:
client = self.client()
diff --git a/harness/codeverse/models/openai.py b/harness/codeverse/models/openai.py
index bfe392da..c5eb8047 100644
--- a/harness/codeverse/models/openai.py
+++ b/harness/codeverse/models/openai.py
@@ -26,9 +26,9 @@ from codeverse.contracts.chat import (
)
from codeverse.contracts.common import Usage
from codeverse.models.base import ModelError
-from codeverse.models.parts import Stopwatch, image_b64
+from codeverse.models.parts import Stopwatch, classify_sdk_exception, image_b64, with_logged_retries
from codeverse.models.pricing import estimate_cost
-from codeverse.models.retry import MAX_WAIT_S, with_retries
+from codeverse.models.retry import MAX_WAIT_S
from codeverse.models.schema_utils import (
JsonParseError,
inline_refs,
@@ -201,24 +201,10 @@ log = logging.getLogger(__name__)
def classify_exception(exc: BaseException) -> ModelError:
- """Map openai SDK exceptions onto ``ModelError``."""
- if isinstance(exc, ModelError):
- return exc
+ """Map openai SDK exceptions onto ``ModelError`` (the shared ladder in parts.py)."""
import openai
- if isinstance(exc, openai.APIStatusError):
- status = int(getattr(exc, "status_code", 0) or 0)
- retry = status in (408, 409, 429) or status >= 500
- return ModelError(
- f"OpenAI API error {status}: {exc.message}", retryable=retry, status=status
- )
- if isinstance(exc, openai.APITimeoutError):
- return ModelError(f"OpenAI request timed out: {exc}", retryable=True, status=408)
- if isinstance(exc, openai.APIConnectionError):
- return ModelError(f"OpenAI connection error: {exc}", retryable=True)
- if isinstance(exc, openai.APIError):
- return ModelError(f"OpenAI API error: {exc}", retryable=False)
- return ModelError(f"OpenAI unexpected error: {type(exc).__name__}: {exc}", retryable=False)
+ return classify_sdk_exception(exc, openai, "OpenAI")
def _is_schema_rejection(err: ModelError) -> bool:
@@ -308,27 +294,9 @@ class OpenAIModel:
raise classify_exception(exc2) from exc2
raise err from exc
- def on_retry(n: int, exc: BaseException, delay: float) -> None:
- log.warning(
- "openai %s attempt %d/%d failed (%s); retrying in %.1fs",
- self.model,
- n,
- self.max_attempts,
- exc,
- delay,
- )
-
- # ChatRequest.max_wait_s is not honoured here: with_retries has no deadline, and its
- # 6 attempts x <= 5 s backoff bound one call to ~20 s of waiting plus the round-trips.
- return with_retries(
- attempt,
- is_retryable=lambda e: isinstance(e, ModelError) and e.retryable,
- attempts=self.max_attempts,
- base_delay=self.base_delay,
- max_delay=self.max_delay,
- on_retry=on_retry,
- sleep=self._sleep,
- )
+ return with_logged_retries(attempt, label="openai", model=self.model,
+ attempts=self.max_attempts, base_delay=self.base_delay,
+ max_delay=self.max_delay, sleep=self._sleep, log=log)
def _once(self, kwargs: dict[str, Any], request: ChatRequest) -> ChatResponse:
client = self.client()
diff --git a/harness/codeverse/models/parts.py b/harness/codeverse/models/parts.py
index 4d45d1d4..097958d0 100644
--- a/harness/codeverse/models/parts.py
+++ b/harness/codeverse/models/parts.py
@@ -9,6 +9,7 @@ import threading
import time
from collections import OrderedDict
from pathlib import Path
+from typing import Any
from codeverse.contracts.chat import ImagePart
from codeverse.models.base import ModelError
@@ -92,3 +93,52 @@ class BoundedCache[T]:
def get(self, key: str) -> T | None:
with self._lock:
return self._d.get(key)
+
+
+def classify_sdk_exception(exc: BaseException, sdk: Any, label: str,
+ *, extra_retry: frozenset[int] = frozenset()) -> Any:
+ """The openai/anthropic SDK exception ladder → ``ModelError``.
+
+ anthropic-sdk-python is a fork of openai-python, so ``APIStatusError`` /
+ ``APITimeoutError`` / ``APIConnectionError`` / ``APIError`` and ``.status_code`` /
+ ``.message`` are the same names on both — this ladder was written out twice,
+ differing only in the module, the label and anthropic's extra 529.
+
+ The MESSAGE WORDING IS LOAD-BEARING: bench/_infra.py string-matches "request timed
+ out" and "connection error" to tell a provider outage from a model failure, so the
+ f"{label} …" forms below must stay byte-identical to what each adapter emitted.
+ """
+ from codeverse.models.base import ModelError
+
+ if isinstance(exc, ModelError):
+ return exc
+ if isinstance(exc, sdk.APIStatusError):
+ status = int(getattr(exc, "status_code", 0) or 0)
+ retry = status in ({408, 409, 429} | set(extra_retry)) or status >= 500
+ return ModelError(f"{label} API error {status}: {exc.message}", retryable=retry, status=status)
+ if isinstance(exc, sdk.APITimeoutError):
+ return ModelError(f"{label} request timed out: {exc}", retryable=True, status=408)
+ if isinstance(exc, sdk.APIConnectionError):
+ return ModelError(f"{label} connection error: {exc}", retryable=True)
+ if isinstance(exc, sdk.APIError):
+ return ModelError(f"{label} API error: {exc}", retryable=False)
+ return ModelError(f"{label} unexpected error: {type(exc).__name__}: {exc}", retryable=False)
+
+
+def with_logged_retries(attempt: Any, *, label: str, model: str, attempts: int,
+ base_delay: float, max_delay: float, sleep: Any, log: Any) -> Any:
+ """``with_retries`` plus the one log line both SDK adapters write.
+
+ ChatRequest.max_wait_s is not honoured here: with_retries has no deadline, and its
+ 6 attempts x <= 5 s backoff bound one call to ~20 s of waiting plus the round-trips.
+ """
+ from codeverse.models.base import ModelError
+ from codeverse.models.retry import with_retries
+
+ def on_retry(n: int, exc: BaseException, delay: float) -> None:
+ log.warning("%s %s attempt %d/%d failed (%s); retrying in %.1fs",
+ label, model, n, attempts, exc, delay)
+
+ return with_retries(attempt, is_retryable=lambda e: isinstance(e, ModelError) and e.retryable,
+ attempts=attempts, base_delay=base_delay, max_delay=max_delay,
+ on_retry=on_retry, sleep=sleep)