Skip to content

Google ADK streams lose iterator semantics and leave spans active after close #267

Closed

Description

@jabrailkhalil

Affected package

traceai-google-adk 0.1.5, main at 54421a6.

What happened?

Instrumenting Google ADK changes the async-generator contract of both Runner.run_async() and BaseAgent.run_async():

  • Direct await stream.__anext__() fails with TypeError: '_AsyncGenerator' object is not an async iterator.
  • After iterator = stream.__aiter__(); await iterator.__anext__(), await stream.aclose() closes the original source but leaves the tracing iterator and its span open.
  • The stream's span/context stays current in the consumer between yielded events. Consumer spans and interleaved invocations can consequently acquire the wrong parent/session.

Reproduction

Use an InMemoryRunner with a local BaseAgent whose _run_async_impl yields two Event objects. Instrument it with GoogleADKInstrumentor().instrument(tracer_provider=provider), where provider exports through SimpleSpanProcessor(InMemorySpanExporter()).

stream = runner.run_async(user_id="user", session_id="session", new_message=message)
await stream.__anext__()  # TypeError

In a fresh run, replace the last line with:

iterator = stream.__aiter__()
await iterator.__anext__()
# get_current_span() is the stream's span here, rather than the consumer's.
await stream.aclose()
# No completed invocation/agent spans have reached the in-memory exporter.
await iterator.aclose()  # Additional cleanup the original API never required.

Expected

Retain the original generator protocol and event objects. Finish tracing and release the underlying generator on close, failure or cancellation; activate its context only while executing it. Preserve nested span context when resuming.

Verification

I reproduced this without a provider call or API key, including through the real ADK Runner. Of 26 new regression cases, 22 fail against the original implementation. The patch passes all 46 Google ADK tests, including the existing suite, on ADK 1.2.1, 1.32.0 and 2.1.0. Full commands, before/after reports and versions are in the linked PR's validation artifacts.

Implementation and regression tests written with an OpenAI assistant under my supervision.

Activity

  1. nik13 commented on Nov 18, 2026

    @nik13
    Contributor

    Thanks for reporting and independently validating the replacement. #445 removed the stream wrapper that changed direct iteration and early close; the native adapter shipped in Google ADK 0.1.7. This closes the traceAI wrapper defect, not all upstream ADK 1.x tracing behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions