Skip to content

Conversation

mkmeral
Copy link
Contributor

@mkmeral mkmeral commented Sep 26, 2025

Description

This PR fixes a critical issue where the Gemini model provider would fail with RuntimeError: Event loop is closed when used in multi-turn conversations. The problem occurred when multiple synchronous agent calls were made sequentially, each creating a new event loop via asyncio.run().

Root Cause

The original implementation stored a single genai.Client instance during initialization, which would bind its underlying HTTP session (aiohttp) to the first event loop. When subsequent calls created new event loops, the old HTTP session would attempt to use the closed event loop, causing the runtime error.

Solution

Changed the Gemini model to create a fresh client for each async operation instead of reusing a single instance:

Before:

def __init__(self, ...):
    self.client = genai.Client(**client_args)  # Reused across calls

async def stream(self, ...):
    response = await self.client.aio.models.generate_content_stream(**request)

After:

def __init__(self, ...):
    self.client_args = client_args or {}  # Store args instead

async def stream(self, ...):
    client = genai.Client(**self.client_args).aio  # Fresh client per call
    response = await client.models.generate_content_stream(**request)

Related Issues

Fixes the failing test_agent_invoke_multiturn test in the Gemini model integration tests. This issue is related to a known upstream problem in the Google genai SDK regarding connection pooling performance (googleapis/python-genai#516).

Documentation PR

No documentation changes required - this is an internal implementation fix that maintains the same public API.

Type of Change

Bug fix

Testing

How have you tested the change?

  • Verified the previously failing test_agent_invoke_multiturn test now passes
  • Ran all Gemini model integration tests to ensure no regressions (9/9 tests pass)
  • Confirmed the fix resolves the "Event loop is closed" error in multi-turn scenarios
  • Tested that single-turn conversations continue to work as expected

Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Unshure
Unshure previously approved these changes Sep 26, 2025
@mkmeral mkmeral enabled auto-merge (squash) September 26, 2025 13:42
zastrowm
zastrowm previously approved these changes Sep 26, 2025
@mkmeral mkmeral dismissed stale reviews from zastrowm and Unshure via 2bc26c5 September 26, 2025 14:06
@mkmeral mkmeral requested review from Unshure and zastrowm September 26, 2025 14:07
@mkmeral mkmeral merged commit 439653d into strands-agents:main Sep 26, 2025
12 checks passed
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.

3 participants