Skip to content
Blog

Synchronous vs Asynchronous Agent Execution with Runner.run and Runner.run_async

Runner.run_async is ADK's primary entry point; Runner.run is a sync convenience backed by a background thread. Learn the exact signatures, internals, and when each fits.

Published on • November 24, 2026

AI Assistant

ADK gives you two ways to drive an agent from your own code: Runner.run_async, an async generator that is the framework’s main entry point, and Runner.run, a synchronous generator the docstring calls “only for local testing and convenience purpose.” They yield the same Event objects — but underneath, one runs on your event loop and the other spins up a background thread to do it.

This tutorial compares the two APIs exactly as google.adk.runners.Runner implements them: signatures, internals, consumption patterns, and the pitfalls that show up when blocking code meets an async runtime. The loop both methods feed is explained in adk-event-loop-deep-dive.md.

Constructing the Runner

from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService

runner = Runner(
    app_name="my_agent",      # required when passing agent= directly
    agent=root_agent,         # exactly one of app=, agent=, node=
    session_service=InMemorySessionService(),
)

Exactly one of app, agent, or node must be provided; with agent, app_name is required (ADK also warns if app_name doesn’t match the agent’s package directory, since sessions are keyed by it).

run_async: The Primary Entry Point

async def run_async(
    self,
    *,
    user_id: str,
    session_id: str,
    invocation_id: str | None = None,
    new_message: types.Content | None = None,
    state_delta: dict[str, Any] | None = None,
    run_config: RunConfig | None = None,
    yield_user_message: bool = False,
    abort_signal: asyncio.Event | None = None,
) -> AsyncGenerator[Event, None]:

Notable behaviors straight from the implementation:

  • It’s an async generator — consume it with async for, and events arrive as the agent produces them (immediately, under StreamingMode.SSE).
  • Pass invocation_id (with no new_message) to resume an interrupted invocation; pass abort_signal to cancel it.
  • state_delta lets you seed session state with the turn.
  • Post-invocation event compaction finishes before the generator closes, but it does not block subsequent run_async calls — later turns of the same session can run concurrently.
  • Raises ValueError if the session isn’t found, or if both invocation_id and new_message are None.
async for event in runner.run_async(
    user_id="user_1",
    session_id="session_1",
    new_message=types.Content(role="user", parts=[types.Part(text="Summarize this")]),
    run_config=config,
):
    if event.is_final_response():
        print(event.content.parts[0].text)

run: Sync Convenience, Backed by a Thread

def run(
    self,
    *,
    user_id: str,
    session_id: str,
    new_message: types.Content,
    state_delta: dict[str, Any] | None = None,
    run_config: RunConfig | None = None,
) -> Generator[Event, None, None]:

Internally, run is a bridge, not a second implementation:

  1. It starts a background thread whose contextvars are copied from the caller (so the invocation joins your OpenTelemetry trace) and runs asyncio.run(self.run_async(...)) there.
  2. Events are pushed into a queue.Queue; run blocks on queue.get() on the calling thread and re-yields them.
  3. Agent exceptions are forwarded over the queue and re-raised on the calling thread once prior events have been yielded.
  4. If you stop iterating early, the background task is cancelled (so after_run callbacks still execute) and the thread is joined before run returns.

Side by Side

Runner.runRunner.run_async
Return typeGenerator[Event, None, None]AsyncGenerator[Event, None]
Event loopSpawns thread + asyncio.runUses your running loop
invocation_id / resumeNot acceptedSupported
abort_signalNot acceptedasyncio.Event to cancel
ErrorsRe-raised on calling threadPropagate in async for
Intended useScripts, tests, local convenienceProduction, servers, streaming UIs

Blocking Pitfalls

  • queue.get() blocks the calling thread. Iterating run() inside code that already owns an event loop stalls that loop for the duration of each wait — use run_async there.
  • Sync tools stall the loop. Python ADK calls synchronous tool functions inline on the asyncio event loop; long blocking I/O belongs in async tools, or behind RunConfig.tool_thread_pool_config in live mode.
  • Early break is safe but deliberate. Both methods cancel the root task if you stop consuming, so a partial read means the invocation doesn’t finish — state committed up to the last yielded event stands.
  • Blocking I/O and the GIL. Thread pools help with I/O-bound and GIL-releasing work; pure CPU-bound sync code still blocks its thread either way.
# Quick script or test: sync is fine
for event in runner.run(
    user_id="user_1", session_id="session_1", new_message=msg,
):
    ...

# Web server / FastAPI route: always async
async for event in runner.run_async(
    user_id="user_1", session_id="session_1", new_message=msg,
):
    ...

Takeaway: Treat run_async as the API — it is the framework’s main entry, supports resume and abort, and composes with your asyncio server — and reach for run only in scripts and tests, remembering that it is a thread-and-queue bridge whose blocking consumption and narrower parameter set make it unsuitable for production event loops.

Sources