feat: retry mechanism for known status codes#71
Open
christianalfoni wants to merge 3 commits intomainfrom
Open
feat: retry mechanism for known status codes#71christianalfoni wants to merge 3 commits intomainfrom
christianalfoni wants to merge 3 commits intomainfrom
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Automatic retry on transient failures — both SDKs
❌ Current behavior
All API calls fail immediately on any error, including transient ones:
No way for callers to customise retry behaviour. Network-level failures and rate-limit responses require manual retry loops.
✅ New behavior
All operations retry automatically on transient failures. Users can observe and override via
RetryConfig:🤔 Assumptions
408 / 429 / 500 / 502 / 503 / 504covers the standard transient server-side codes;4xxauth/validation errors are intentionally excludedTypeError(the fetch API convention); Python useshttpx.TimeoutException,httpx.ConnectError,httpx.RemoteProtocolErrorsnapshots.createis not idempotent — retrying after a transient 500 can register a duplicate; this is documented prominently and users are shown the exclusion patterndelayis expressed in milliseconds in TypeScript and seconds in Python — consistent with each language's sleep convention (setTimeoutvsasyncio.sleep)🧠 Decisions
callApi/_call_apihelper — all operations go through it; retry config flows down from the SDK constructor through every namespaceshouldRetryreturn type isboolean | number(TS) /bool | float(Python) — returning a numeric value lets the user both approve the retry AND set the delay in one callback, without needing to mutate the context objectonRetryreceives the final resolveddelay(aftershouldRetrymay have overridden it) so loggers and UI code see the same value the SDK will sleep forRetryConfigandRetryContextare exported from the package root in both SDKs — users do not need to reach into internal modulesSandboxinstance methodshibernate()andshutdown()were prefixed_hibernate/_shutdown(private) despite being documented as public — renamed to fix theAttributeErrorat runtimedocs/typescript-sdk.md🔄 Discussions
_unwrap_or_raise(Python) and directthrowOnError(TypeScript) were used for error handling — replaced entirely by_call_api/callApito unify error unwrapping and retry logic in one place🧪 Testing
src/utils.test.tscovering all retryable status codes, network errors (TypeError),shouldRetryreturn shapes (false/true/ number),onRetrycall count and delay values, exponential backoff math,ApiError/SandboxErrortyped throws, 204 no-body path, and context string formattingtests/test_call_api.pyincluding parametrized status codes, all threehttpxexception types, async callbacks, backoff delay values with patchedrandom.random, and asyncshould_retry/on_retrycoroutines📁 References