Skip to content

Conversation

pgrayy
Copy link
Member

@pgrayy pgrayy commented Jul 12, 2025

Description

The Mistral async client has an issue where making multiple requests with separate asyncio.run() calls causes the second request to fail with "Event loop is closed" error. The reason is that the client uses httpx internally, which maintains a connection pool that persists beyond individual event loops. This means connections created in the first event loop remain in the pool after that loop closes, and when the second asyncio.run() creates a new event loop, httpx tries to reuse those existing pooled connections that are still tied to the first event loop instead of the new event loop, thus causing a failure.

This is a problem for Strands as Agent.__call__ runs asyncio.run() on every invocation. Consequently, doing something like the following with the MistralModel provider will fail:

from strands import Agent
from strands.models.mistral import MistralModel

model = MistralModel(api_key="****", model_id="mistral-medium-latest")
agent = Agent(model=model)

agent("Hello")
agent("Hello again")  # Event loop is closed encountered

The fix is to establish a new Mistral client connection on every call to stream. This fits with the pattern documented in both the Mistral docs and the httpx docs.

Related Issues

#431

Documentation PR

N/A

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Other (please describe):

Testing

How have you tested the change? 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
  • Ran hatch test tests_integ/models/test_model_mistral.py
  • Ran the following test script:
import gc
from strands import Agent
from strands.models.mistral import MistralModel

model = MistralModel(api_key="****", model_id="mistral-medium-latest")

for _ in range(10):
    agent = Agent(model=model)
    agent("what is 2+2")

    gc.collect()

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.

@pgrayy pgrayy marked this pull request as ready for review July 12, 2025 22:46
@pgrayy pgrayy mentioned this pull request Jul 12, 2025
3 tasks
@pgrayy pgrayy merged commit 9f2f13d into strands-agents:main Jul 12, 2025
22 checks passed
@pgrayy pgrayy deleted the mistral-fix branch July 14, 2025 13:18
dbschmigelski pushed a commit to Ketansuhaas/sdk-python that referenced this pull request Jul 24, 2025
@pgrayy pgrayy mentioned this pull request Sep 12, 2025
8 tasks
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.

2 participants