Files
Bo Yang ed8add2e9b test(cli): Make JSON test files deterministic and sequential
Sort keys when saving rebuilt JSON test files and re-assign sequential event IDs to avoid gaps from filtered events. Also applied these changes to all existing test files in the contributing folder.

Change-Id: Ib126228f016db1d28030eee4f7d9d8cbc604e038
2026-04-10 11:08:42 -07:00
..

ADK Workflow Sample: Node Retries

Overview

In real-world applications, interacting with external APIs, databases, or third-party services can occasionally result in transient failures (e.g., temporary network outages, rate limits, or bad gateways).

The ADK framework allows you to easily handle these scenarios by wrapping the unreliable logic in a @node decorator configured with RetryConfig. If the node raises one of the expected exceptions, the workflow engine automatically pauses, waits for a backoff delay, and reschedules the node for another attempt.

When a node raises an exception, the framework automatically emits an error event (with error_code and error_message) so the error is visible in the event stream. If the node has retry configured, it will be retried after the backoff delay.

This sample demonstrates a get_weather node that intentionally fails randomly (70% chance) by raising an HTTPError representing a 500 Internal Server error. The framework gracefully recovers and eventually succeeds, passing the result to report_weather.

Graph

       [ START ]
           |
           v
     [get_weather]
(Retries on HTTPError)
           |
           v
   [report_weather]

How To

  1. Import RetryConfig: Ensure you import the configuration class to set your retry parameters.

    from google.adk.workflow import RetryConfig
    
  2. Configure the Decorator: Apply the @node decorator to your Python function and specify the retry_config parameter with your desired logic (e.g., max_attempts, initial_delay).

    @node(retry_config=RetryConfig(max_attempts=5, initial_delay=1))
    def get_weather(ctx: Context) -> str:
        # ... flaky logic here ...
    

    When an exception like HTTPError occurs, the ADK framework catches it, emits an error event, and processes the backoff delay automatically. As long as max_attempts hasn't been exceeded, the node executes again.

  3. Track Retries (Optional): If you need to know which attempt the node is currently running, you can access ctx.attempt_count from the Context.

    yield Event(message=f"Getting weather... attempt {ctx.attempt_count}")