Skip to content

fix: Add retry with exponential backoff for 429 errors and batch append support in VertexAiSessionService - #5720

Closed
mahasarabesh wants to merge 3 commits into
google:mainfrom
mahasarabesh:fix/5714-retry-429-append-event
Closed

mahasarabesh wants to merge 3 commits into
google:mainfrom
mahasarabesh:fix/5714-retry-429-append-event

Conversation

@mahasarabesh

Copy link
Copy Markdown
Contributor

Fixes #5714

Problem

When deployed on Agent Engine, _init_session() replays all prior session events by calling session_service.append_event() individually for each event. For sessions with many events, this triggers 429 RESOURCE_EXHAUSTED errors from the Vertex AI API due to rate limiting, causing sessions to fail on initialization.

The root cause is in the external vertexai SDK (outside this repo), but adk-python can defend against this on its own side.

Changes

1. Retry with exponential backoff on append_event() (core fix)

VertexAiSessionService now retries API calls that fail with HTTP 429 using exponential backoff with jitter:

  • Up to 5 attempts
  • Initial delay of 1s, exponential base of 2x, capped at 30s
  • Random jitter (0-50%% of delay) to avoid thundering herd
  • Non-429 errors are raised immediately without retry

2. append_events_batch() method

Added append_events_batch() to BaseSessionService with a default sequential implementation. VertexAiSessionService overrides it with asyncio.Semaphore(5) concurrency control, allowing callers that need to replay many events to do so with bounded parallelism instead of sequential one-by-one calls.

Files changed

File Change
sessions/base_session_service.py Added append_events_batch() default implementation
sessions/vertex_ai_session_service.py Added _append_with_retry(), overridden append_events_batch() with semaphore
tests/.../test_vertex_ai_session_service.py 4 new tests: retry on 429, max retries exhausted, non-429 immediate raise, batch append
tests/.../test_session_service.py 1 new test: batch append across all session service backends

Testing

  • All 36 existing + 4 new vertex session tests pass
  • Batch test passes across InMemory, InMemory (light copy), Database, and SQLite backends
  • Zero existing tests modified

I'm new to contributing to this project and happy to make changes based on your review comments. Looking forward to your feedback!

…nd support

- Add _append_with_retry() to VertexAiSessionService with exponential
  backoff and jitter on 429 RESOURCE_EXHAUSTED (up to 5 attempts)
- Add append_events_batch() to BaseSessionService (default sequential)
- Override append_events_batch() in VertexAiSessionService with
  asyncio.Semaphore(5) concurrency control
- Add 5 new tests covering retry, max retries, non-429, and batch

Fixes google#5714
@google-cla

google-cla Bot commented May 16, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label May 18, 2026
@rohityan

rohityan commented May 18, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @mahasarabesh , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you please fix the pre commit check errors and failing unit test.

@rohityan rohityan added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label May 18, 2026
copybara-service Bot pushed a commit that referenced this pull request Sep 3, 2026
Merge #5720

VertexAiSessionService built its API client with no retry configuration, so a
429 RESOURCE_EXHAUSTED failed on the first response. It now retries 429 once.
A call that keeps getting 429 therefore issues one extra request and takes
about a second longer before it raises.

This does not resolve #5714, which asks for batched or windowed event replay.
That replay loop is in the vertexai SDK's _init_session, not here, and the
issue notes each retry consumes more of the same per-minute quota, which is
why the retry is bounded to a single attempt.

PiperOrigin-RevId: 975844908
@adk-bot

adk-bot commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Thank you @mahasarabesh for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit cc5985a.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Sep 3, 2026
@adk-bot adk-bot closed this Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged request clarification [Status] The maintainer need clarification or more information from the author services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent Engine: _init_session replays all events via append_event causing 429 RESOURCE_EXHAUSTED on long sessions

3 participants