Repository navigation
[google-genai] Restore caller context between stream reads - #899
JacksonWeber wants to merge 7 commits into
Conversation
Suspend streamed invocations between reads and cover lifecycle context isolation across supported SDK versions. Assisted-by: GPT-6 Astra Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: d301755c-099c-497f-87d9-8f86611e71ff
Pull request dashboard statusWaiting on maintainers · refreshed 2026-10-17 01:45 UTC Merge when ready. Status above doesn't look right?
|
Assisted-by: GPT-6 Astra Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: d301755c-099c-497f-87d9-8f86611e71ff
There was a problem hiding this comment.
🟢 Approval recommended
The focused changes follow the shared stream lifecycle contract and include comprehensive regression coverage, with no identified blockers.
0 open findings
What changed in this PR
Restores caller context between Google GenAI stream reads while preserving invocation context during iteration and cleanup.
Changes:
- Suspends invocations in all six sync/async stream wrappers.
- Adds 72 lifecycle regression cases and includes them in the SDK v2 test environment.
- Documents the fix in a changelog fragment.
| File | Description |
|---|---|
| tox.ini | Runs context regression tests against SDK v2. |
| instrumentation/opentelemetry-instrumentation-google-genai/tests/test_stream_context.py | Covers context restoration across stream lifecycle paths. |
| instrumentation/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/interactions.py | Suspends Interactions and fetch stream invocations. |
| instrumentation/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py | Suspends sync/async generation stream invocations. |
| instrumentation/opentelemetry-instrumentation-google-genai/.changelog/899.fixed | Records the context-restoration fix. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| ], | ||
| ) | ||
| async def test_stream_context( | ||
| monkeypatch: pytest.MonkeyPatch, |
There was a problem hiding this comment.
why does simply calling invocation.suspend in the initializer of these stream wrappers require such a massive test ? Can we have a simpler test or can you explain why this is necessary ?
There was a problem hiding this comment.
I’ve trimmed it to context restoration for sync and async calls. Tests should be simpler now, let me know if they look good now
There was a problem hiding this comment.
its better but i dont love all the branching logic.. maybe split it in 2 1 for sync 1 with async ? I think that'd get rid of a lot of it
There was a problem hiding this comment.
Split it into separate sync and async tests, should remove the branching logic
Assisted-by: GPT-6 Astra Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 503e6eda-a2b7-4bfe-a357-70d47b0e7aa1
Keep context restoration checks for sync and async APIs on completion and caller or stream errors. Leave generic stream lifecycle edge cases to the shared wrapper tests. Assisted-by: GPT-6 Astra Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 503e6eda-a2b7-4bfe-a357-70d47b0e7aa1
Assisted-by: GPT-6 Astra Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 76e8b89b-ab68-42de-abed-7045507fb0d5
Keep synchronous and asynchronous stream lifecycle tests separate while preserving operation, error, and context assertions. Assisted-by: GPT-6 Astra Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: d301755c-099c-497f-87d9-8f86611e71ff
Description
Move invocation suspension into the shared
SyncStreamWrapperandAsyncStreamWrapperconstructors, removing duplicate Google GenAI and tool-wrapper calls. Keep spans current only during stream reads and cleanup, with shared sync/async regressions and 24 Google GenAI API cases covering generation, Interactions model/agent calls, and fetch streams.Part of #872. This shared fix also applies to other invocation-backed stream wrappers. The separate stream-timing metrics fix is not included.
Type of change
How has this been tested?
Dependencies were restored with pip after uv TLS failures. The Python 3.10 environment was manually pinned to the declared dependency floors, including Google GenAI 1.32.0. Disabling suspension makes all eight shared constructor regression cases fail.
Full-repository typechecking and conformance were not rerun for this follow-up.
Checklist
See CONTRIBUTING.md
for the style guide, changelog guidance, and more.