contributing/workflow_samples/state/README.md
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:
ctx.state["key"] = "value"yield Event(state={"key": "value"})ctx.state["key"]def func(key: str): ...Hello ADK!Testing state management. [ START ]
|
v
[ process_initial_input ]
|
v
[ update_state_via_event ]
|
v
[ read_state_via_ctx ]
|
v
[ read_state_via_param ]
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
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()}
)
Read state via context: Retrieve values from ctx.state.
def read_state_via_ctx(ctx):
original = ctx.state["original_text"]
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}!"