Skip to content

fix(google-adk): preserve async stream lifecycle and tracing context - #268

Closed
jabrailkhalil wants to merge 3 commits into
future-agi:mainfrom
jabrailkhalil:fix/google-adk-stream-lifecycle
Closed

jabrailkhalil wants to merge 3 commits into
future-agi:mainfrom
jabrailkhalil:fix/google-adk-stream-lifecycle

Conversation

@jabrailkhalil

@jabrailkhalil jabrailkhalil commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #267. Runner.run_async() and BaseAgent.run_async() now return a traced native async generator. Direct iteration, asend(), athrow() and aclose() keep their normal behavior; stream cleanup finishes its span, and the event objects pass through unchanged.

Why?

The previous proxy only implemented __aiter__(). Direct iteration raised TypeError, closing the proxy bypassed the tracing iterator's cleanup, and its active context leaked into event-processing code. One shared generator now forwards control operations and restores the consumer's context at every yield, while retaining any nested agent context for the next resume.

How was it tested?

  • 26 new cases cover direct iteration, exhaustion, early close, lazy close, send/throw, source and cleanup errors, cancellation, nested spans, interleaved sessions, suppression and the real ADK Runner with an in-memory session service.

  • The original source fails 22 of those cases. The patched source passes all 46 tests, including the existing 20 Google ADK tests, with no skips.

  • Linux validation covers Python 3.10 / ADK 1.2.1 and Python 3.12 / ADK 1.32.0 and 2.1.0: complete reports and commands. The workflow checks out 9696d4e43ac0246bd6ef2272c99b754e5dc1d7f9 directly; validation files are excluded from this PR.

  • Windows / Python 3.12 / ADK 2.1.0 also passes all 46 tests. Six local mutation checks catch omitted close, context detach/snapshot, send/throw forwarding and span termination.

  • Legacy mock-only tests use monkeypatch so singleton method overrides are restored before the integration tests run. CI runs those legacy tests first.

  • Ruff correctness checks, Black on the new helper/tests, import sorting, diff checks and Mypy 1.18.2 pass in all three CI environments; before/after type reports are included.

  • Unit tests added / updated.

  • Offline integration tests pass with real ADK Runner and BaseAgent; no LLM provider or gateway calls.

  • SDK behavior and close-on-early-exit usage documented in the README and changelog.

Checklist

  • Branch is off main.
  • Conventional commit message.
  • No TODOs, commented-out code, API keys or secrets in the diff.
  • Prose reviewed; the template's linked docs/VOCABULARY.md is absent on this base.

Notes for reviewers

Normal exhaustion marks a span OK; errors are recorded once. Explicit close and cancellation finish an incomplete span without marking it successful. The underlying generator is closed in finally. Live Gemini/provider behavior was not tested.

ADK 1.2.1 finalizes nested Runner streams asynchronously. The integration test checks the invocation span ends immediately, then waits for the nested agent's cleanup; direct close of either wrapper is verified separately.

A Python 3.9 attempt could not collect tests even against the original source: the existing EvalTag.eval_name: str | EvalName annotation in fi_instrumentation/fi_types.py fails at import. Python 3.9 therefore remains unverified; no runtime floor or unrelated core code is changed here.

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

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

I checked the native ADK integration now on dev in #445. It no longer wraps Runner.run_async or BaseAgent.run_async, so the iterator proxy this PR repairs is gone. The maintained suite passes with ADK 1.2.1 and 2.13.0, including direct iteration and early close: validation. I'm closing this legacy-wrapper change as superseded.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant