LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
[MISC] Remove max_time from SDK1 retry decorator (#1800)
Chandrasekharan M2a7ee4a810c3
diff --git a/.gitignore b/.gitignoreindex 1ccedad14..f6837ce07 100644--- a/.gitignore+++ b/.gitignore@@ -689,7 +689,10 @@ prompting/# ClaudeCLAUDE.md.claude/*+# Must un-ignore each parent dir level before negating nested paths (gitignore won't descend into ignored dirs)!.claude/skills/+.claude/skills/*+!.claude/skills/worktree/CONTRIBUTION_GUIDE.md.mcp.jsondiff --git a/unstract/sdk1/README.md b/unstract/sdk1/README.mdindex 4e7de612a..99c72fa0e 100644--- a/unstract/sdk1/README.md+++ b/unstract/sdk1/README.md@@ -16,7 +16,6 @@ The SDK automatically retries platform and prompt service calls on transient fai| Variable | Default | Description ||----------|---------|-------------|| `MAX_RETRIES` | 3 | Maximum retry attempts |-| `MAX_TIME` | 60 | Maximum total time (seconds) || `BASE_DELAY` | 1.0 | Initial delay (seconds) || `MULTIPLIER` | 2.0 | Backoff multiplier || `JITTER` | true | Add random jitter (0-25%) |diff --git a/unstract/sdk1/src/unstract/sdk1/platform.py b/unstract/sdk1/src/unstract/sdk1/platform.pyindex 4f212461b..a7995164c 100644--- a/unstract/sdk1/src/unstract/sdk1/platform.py+++ b/unstract/sdk1/src/unstract/sdk1/platform.py@@ -110,7 +110,6 @@ class PlatformHelper:Retry behavior is configurable via environment variables:- PLATFORM_SERVICE_MAX_RETRIES (default: 3)- - PLATFORM_SERVICE_MAX_TIME (default: 60s)- PLATFORM_SERVICE_BASE_DELAY (default: 1.0s)- PLATFORM_SERVICE_MULTIPLIER (default: 2.0)- PLATFORM_SERVICE_JITTER (default: true)@@ -230,7 +229,6 @@ class PlatformHelper:Retry behavior is configurable via environment variables:- PLATFORM_SERVICE_MAX_RETRIES (default: 3)- - PLATFORM_SERVICE_MAX_TIME (default: 60s)- PLATFORM_SERVICE_BASE_DELAY (default: 1.0s)- PLATFORM_SERVICE_MULTIPLIER (default: 2.0)- PLATFORM_SERVICE_JITTER (default: true)diff --git a/unstract/sdk1/src/unstract/sdk1/prompt.py b/unstract/sdk1/src/unstract/sdk1/prompt.pyindex f46737c65..c64850620 100644--- a/unstract/sdk1/src/unstract/sdk1/prompt.py+++ b/unstract/sdk1/src/unstract/sdk1/prompt.py@@ -230,7 +230,6 @@ class PromptTool:Retry behavior is configurable via environment variables:- PROMPT_SERVICE_MAX_RETRIES (default: 3)- - PROMPT_SERVICE_MAX_TIME (default: 60s)- PROMPT_SERVICE_BASE_DELAY (default: 1.0s)- PROMPT_SERVICE_MULTIPLIER (default: 2.0)- PROMPT_SERVICE_JITTER (default: true)diff --git a/unstract/sdk1/src/unstract/sdk1/utils/retry_utils.py b/unstract/sdk1/src/unstract/sdk1/utils/retry_utils.pyindex 315df44bf..9d1a1e4ec 100644--- a/unstract/sdk1/src/unstract/sdk1/utils/retry_utils.py+++ b/unstract/sdk1/src/unstract/sdk1/utils/retry_utils.py@@ -87,7 +87,6 @@ def calculate_delay(def retry_with_exponential_backoff( # noqa: C901max_retries: int,- max_time: float,base_delay: float,multiplier: float,jitter: bool,@@ -100,7 +99,6 @@ def retry_with_exponential_backoff( # noqa: C901Args:max_retries: Maximum number of retry attempts- max_time: Maximum total time in secondsbase_delay: Initial delay in secondsmultiplier: Backoff multiplierjitter: Whether to add jitter@@ -116,7 +114,6 @@ def retry_with_exponential_backoff( # noqa: C901def decorator(func: Callable) -> Callable: # noqa: C901@wraps(func)def wrapper(*args: Any, **kwargs: Any) -> Any: # noqa: C901, ANN401- start_time = time.time()last_exception = Nonefor attempt in range(max_retries + 1): # +1 for initial attempt@@ -144,17 +141,6 @@ def retry_with_exponential_backoff( # noqa: C901if retry_predicate is not None:should_retry = retry_predicate(e)- # Check if we've exceeded max time- elapsed_time = time.time() - start_time- if elapsed_time >= max_time:- logger_instance.exception(- "Giving up '%s' after %.1fs (max time exceeded): %s",- func.__name__,- elapsed_time,- e,- )- raise-# If not retryable or last attempt, raise the errorif not should_retry or attempt == max_retries:if attempt > 0:@@ -166,22 +152,8 @@ def retry_with_exponential_backoff( # noqa: C901)raise- # Calculate delay for next retry- delay = calculate_delay(- attempt, base_delay, multiplier, max_time, jitter- )-- # Ensure we don't exceed max_time with the delay- remaining_time = max_time - elapsed_time- if delay >= remaining_time:- logger_instance.exception(- "Giving up '%s' - next delay %.1fs would exceed "- "max time %.1fs",- func.__name__,- delay,- max_time,- )- raise+ # Calculate delay for next retry (capped at 60s)+ delay = calculate_delay(attempt, base_delay, multiplier, 60.0, jitter)# Log retry attemptlogger_instance.warning(@@ -230,7 +202,6 @@ def create_retry_decorator(Environment variables (using prefix):{prefix}_MAX_RETRIES: Maximum retry attempts (default: 3)- {prefix}_MAX_TIME: Maximum total time in seconds (default: 60){prefix}_BASE_DELAY: Initial delay in seconds (default: 1.0){prefix}_MULTIPLIER: Backoff multiplier (default: 2.0){prefix}_JITTER: Enable jitter true/false (default: true)@@ -253,7 +224,6 @@ def create_retry_decorator(# Load configuration from environmentmax_retries = int(os.getenv(f"{prefix}_MAX_RETRIES", "3"))- max_time = float(os.getenv(f"{prefix}_MAX_TIME", "60"))base_delay = float(os.getenv(f"{prefix}_BASE_DELAY", "1.0"))multiplier = float(os.getenv(f"{prefix}_MULTIPLIER", "2.0"))use_jitter = os.getenv(f"{prefix}_JITTER", "true").strip().lower() in {@@ -265,8 +235,6 @@ def create_retry_decorator(if max_retries < 0:raise ValueError(f"{prefix}_MAX_RETRIES must be >= 0")- if max_time <= 0:- raise ValueError(f"{prefix}_MAX_TIME must be > 0")if base_delay <= 0:raise ValueError(f"{prefix}_BASE_DELAY must be > 0")if multiplier <= 0:@@ -277,7 +245,6 @@ def create_retry_decorator(return retry_with_exponential_backoff(max_retries=max_retries,- max_time=max_time,base_delay=base_delay,multiplier=multiplier,jitter=use_jitter,@@ -290,7 +257,6 @@ def create_retry_decorator(# Retry configured through below envs.# - PLATFORM_SERVICE_MAX_RETRIES (default: 3)-# - PLATFORM_SERVICE_MAX_TIME (default: 60s)# - PLATFORM_SERVICE_BASE_DELAY (default: 1.0s)# - PLATFORM_SERVICE_MULTIPLIER (default: 2.0)# - PLATFORM_SERVICE_JITTER (default: true)@@ -298,7 +264,6 @@ retry_platform_service_call = create_retry_decorator("PLATFORM_SERVICE")# Retry configured through below envs.# - PROMPT_SERVICE_MAX_RETRIES (default: 3)-# - PROMPT_SERVICE_MAX_TIME (default: 60s)# - PROMPT_SERVICE_BASE_DELAY (default: 1.0s)# - PROMPT_SERVICE_MULTIPLIER (default: 2.0)# - PROMPT_SERVICE_JITTER (default: true)diff --git a/unstract/sdk1/tests/conftest.py b/unstract/sdk1/tests/conftest.pyindex e5a246b4c..b3858b7e2 100644--- a/unstract/sdk1/tests/conftest.py+++ b/unstract/sdk1/tests/conftest.py@@ -22,12 +22,10 @@ def clean_env(monkeypatch: MonkeyPatch) -> MonkeyPatch:# Remove any retry-related environment variablesenv_vars = ["PLATFORM_SERVICE_MAX_RETRIES",- "PLATFORM_SERVICE_MAX_TIME","PLATFORM_SERVICE_BASE_DELAY","PLATFORM_SERVICE_MULTIPLIER","PLATFORM_SERVICE_JITTER","PROMPT_SERVICE_MAX_RETRIES",- "PROMPT_SERVICE_MAX_TIME","PROMPT_SERVICE_BASE_DELAY","PROMPT_SERVICE_MULTIPLIER","PROMPT_SERVICE_JITTER",diff --git a/unstract/sdk1/tests/utils/test_retry_utils.py b/unstract/sdk1/tests/utils/test_retry_utils.pyindex 4619b114e..cbf73c731 100644--- a/unstract/sdk1/tests/utils/test_retry_utils.py+++ b/unstract/sdk1/tests/utils/test_retry_utils.py@@ -155,7 +155,6 @@ class TestRetryWithExponentialBackoff:decorator = retry_with_exponential_backoff(max_retries=3,- max_time=60.0,base_delay=1.0,multiplier=2.0,jitter=False,@@ -180,7 +179,6 @@ class TestRetryWithExponentialBackoff:decorator = retry_with_exponential_backoff(max_retries=3,- max_time=60.0,base_delay=0.1, # Short delay for testingmultiplier=2.0,jitter=False,@@ -206,7 +204,6 @@ class TestRetryWithExponentialBackoff:decorator = retry_with_exponential_backoff(max_retries=2,- max_time=60.0,base_delay=0.1,multiplier=2.0,jitter=False,@@ -224,30 +221,6 @@ class TestRetryWithExponentialBackoff:# Should log giving upmock_logger.exception.assert_called()- def test_max_time_exceeded(self, mock_logger: MagicMock) -> None:- """Test that max time causes failure."""- mock_func = Mock(- side_effect=ConnectionError("Always fails"), __name__="test_func"- )-- decorator = retry_with_exponential_backoff(- max_retries=10,- max_time=0.5, # Very short max time- base_delay=0.2,- multiplier=2.0,- jitter=False,- exceptions=(ConnectionError,),- logger_instance=mock_logger,- prefix="TEST",- )- decorated_func = decorator(mock_func)-- with pytest.raises(ConnectionError):- decorated_func()-- # Should fail before reaching max retries due to time limit- assert mock_func.call_count < 10-def test_retry_with_custom_predicate(self, mock_logger: MagicMock) -> None:"""Test retry with custom predicate."""@@ -261,7 +234,6 @@ class TestRetryWithExponentialBackoff:decorator = retry_with_exponential_backoff(max_retries=3,- max_time=60.0,base_delay=0.1,multiplier=2.0,jitter=False,@@ -287,7 +259,6 @@ class TestRetryWithExponentialBackoff:decorator = retry_with_exponential_backoff(max_retries=3,- max_time=60.0,base_delay=0.1,multiplier=2.0,jitter=False,@@ -310,7 +281,6 @@ class TestRetryWithExponentialBackoff:decorator = retry_with_exponential_backoff(max_retries=3,- max_time=60.0,base_delay=0.1,multiplier=2.0,jitter=False,@@ -326,32 +296,6 @@ class TestRetryWithExponentialBackoff:# Should not retryassert mock_func.call_count == 1- def test_delay_would_exceed_max_time(self, mock_logger: MagicMock) -> None:- """Test that delay exceeding max time causes immediate failure."""- mock_func = Mock(- __name__="test_func", side_effect=ConnectionError("Always fails")- )-- decorator = retry_with_exponential_backoff(- max_retries=10,- max_time=0.3, # Very short max time- base_delay=1.0, # Large delay- multiplier=2.0,- jitter=False,- exceptions=(ConnectionError,),- logger_instance=mock_logger,- prefix="TEST",- )- decorated_func = decorator(mock_func)-- with pytest.raises(ConnectionError):- decorated_func()-- # Should fail quickly due to delay exceeding remaining time- mock_logger.exception.assert_called()- exception_calls = [str(c) for c in mock_logger.exception.call_args_list]- assert any("would exceed max time" in c for c in exception_calls)-class TestCreateRetryDecorator:"""Tests for create_retry_decorator function."""@@ -376,7 +320,6 @@ class TestCreateRetryDecorator:set_env("TEST_SERVICE",max_retries=5,- max_time=120,base_delay=2.0,multiplier=3.0,jitter="false",@@ -403,15 +346,6 @@ class TestCreateRetryDecorator:with pytest.raises(ValueError, match="MAX_RETRIES must be >= 0"):create_retry_decorator("TEST_SERVICE")- def test_invalid_max_time(- self, clean_env: MonkeyPatch, set_env: Callable[..., None]- ) -> None:- """Test that non-positive max_time raises error."""- set_env("TEST_SERVICE", max_time=0)-- with pytest.raises(ValueError, match="MAX_TIME must be > 0"):- create_retry_decorator("TEST_SERVICE")-def test_invalid_base_delay(self, clean_env: MonkeyPatch, set_env: Callable[..., None]) -> None:@@ -597,7 +531,6 @@ class TestRetryLogging:decorator = retry_with_exponential_backoff(max_retries=3,- max_time=60.0,base_delay=0.1,multiplier=2.0,jitter=False,@@ -623,7 +556,6 @@ class TestRetryLogging:decorator = retry_with_exponential_backoff(max_retries=3,- max_time=60.0,base_delay=0.1,multiplier=2.0,jitter=False,@@ -648,7 +580,6 @@ class TestRetryLogging:decorator = retry_with_exponential_backoff(max_retries=1,- max_time=60.0,base_delay=0.1,multiplier=2.0,jitter=False,