Skip to content

models - ollama - init async client per request #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2025

Conversation

pgrayy
Copy link
Member

@pgrayy pgrayy commented Jul 12, 2025

Description

The Ollama AsyncClient 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 AsyncClient 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 OllamaModel provider will fail:

from strands import Agent
from strands.models.ollama import OllamaModel

model = OllamaModel(host="http://localhost:11434", model_id="mistral-nemo")
agent = Agent(model=model)

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

The fix is to establish a new Ollama AsyncClient connection on every call to stream. This fits with the pattern documented in both the Ollama 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_ollama.py
  • Ran the following test script:
import gc
from strands import Agent
from strands.models.ollama import OllamaModel

model = OllamaModel(host="http://localhost:11434", model_id="tinyllama")

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 mentioned this pull request Jul 12, 2025
3 tasks
@pgrayy pgrayy marked this pull request as ready for review July 12, 2025 22:08
@pgrayy pgrayy merged commit 12b948a into strands-agents:main Jul 12, 2025
22 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.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy