Files
google--adk-python/contributing/workflow_samples/state
Wei (Jack) Sun 1c64a41b53 feat: ADK 2.0 alpha
Introduces two major capabilities:
- Workflow runtime: graph-based execution engine for composing
  deterministic execution flows for agentic apps, with support for
  routing, fan-out/fan-in, loops, retry, state management, dynamic
  nodes, human-in-the-loop, and nested workflows
- Task API: structured agent-to-agent delegation with multi-turn
  task mode, single-turn controlled output, mixed delegation
  patterns, human-in-the-loop, and task agents as workflow nodes

Co-Authored-By: Bo Yang <ybo@google.com>
Co-Authored-By: George Weale <gweale@google.com>
Co-Authored-By: Sean Zhou <seanzhougoogle@google.com>
Co-Authored-By: Shangjie Chen <deanchen@google.com>
Co-Authored-By: Swapnil Agarwal <swapnilag@google.com>
Co-Authored-By: Wei Sun <weisun@google.com>
Co-Authored-By: Xuan Yang <xygoogle@google.com>
Co-Authored-By: Yifan Wang <wanyif@google.com>
Change-Id: I35932c50cfe29ff68559e3781713dbb5eb7b3382
2026-03-17 23:24:58 -07:00
..
2026-03-17 23:24:58 -07:00
2026-03-17 23:24:58 -07:00

ADK Workflow State Sample

Overview

This sample demonstrates different ways to manage state in an ADK Workflow. State is a dictionary shared across all nodes in the workflow execution, useful for gathering information across multiple steps without passing everything directly from one node's output to another's input.

In this sample, we show four techniques:

  1. Updating state via direct dictionary mutation: ctx.state["key"] = "value"
  2. Updating state by yielding an event: yield Event(state={"key": "value"})
  3. Reading state via direct dictionary access: ctx.state["key"]
  4. Reading state via automatic parameter injection: def func(key: str): ...

Sample Inputs

  • Hello ADK!
  • Testing state management.

Graph

         [ START ]
             |
             v
 [ process_initial_input ]
             |
             v
 [ update_state_via_event ]
             |
             v
  [ read_state_via_ctx ]
             |
             v
 [ read_state_via_param ]

How To

  1. Update state via direct mutation: Access the context and modify ctx.state directly.

    def process_initial_input(ctx, node_input: str):
        ctx.state["original_text"] = node_input
    
  2. Update state via Event: Yield an Event object with a state delta dictionary.

    def update_state_via_event(node_input: str):
        yield Event(
            state={"uppercased_text": node_input.upper()}
        )
    
  3. Read state via context: Retrieve values from ctx.state.

    def read_state_via_ctx(ctx):
        original = ctx.state["original_text"]
    
  4. Read state via parameter injection: Declare a function parameter that matches the key in the workflow state, and ADK will automatically populate it.

    def read_state_via_param(appended_text: str):
        return f"Final Result: {appended_text}!"