Skip to content

fix(smolagents): Reuse AnyLLM client instance to avoid event loop errors#902

Merged
peteski22 merged 2 commits intomainfrom
fix/smolagents-reuse-anyllm-client
Feb 6, 2026
Merged

fix(smolagents): Reuse AnyLLM client instance to avoid event loop errors#902
peteski22 merged 2 commits intomainfrom
fix/smolagents-reuse-anyllm-client

Conversation

@peteski22
Copy link
Contributor

@peteski22 peteski22 commented Feb 4, 2026

Summary

  • Fixes the "Event loop is closed" error when making multiple completion calls with smolagents
  • Creates an AnyLLM client instance once via AnyLLM.create() instead of returning the any_llm module
  • Follows the same pattern used by tinyagent and openai framework implementations

Changes

  • Parse model_id using AnyLLM.split_model_provider() to extract provider and model
  • Store provider config separately from completion kwargs
  • Create AnyLLM instance in create_client() for reuse by ApiModel base class
  • Pass allow_running_loop=True to completion calls to permit sync API in async contexts
  • Add error handling for malformed model_id with clear error message
  • Add unit tests for AnyLLMModel class including regression test for issue smolagents AnyLLM client should be created only once and reused for completions #824

Test plan

  • Existing unit tests pass (5 tests)
  • New unit tests added (4 tests):
    • test_malformed_model_id_raises_clear_error
    • test_parses_model_id_correctly
    • test_create_client_creates_anyllm_instance
    • test_client_reused_across_multiple_calls (regression test)
  • Pre-commit hooks pass (ruff, mypy, etc.)

Fixes #824

@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 97.77778% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/any_agent/frameworks/smolagents.py 93.75% 1 Missing ⚠️
Files with missing lines Coverage Δ
src/any_agent/testing/helpers.py 40.98% <ø> (-29.51%) ⬇️
tests/unit/frameworks/test_smolagents.py 100.00% <100.00%> (ø)
src/any_agent/frameworks/smolagents.py 61.83% <93.75%> (-9.48%) ⬇️

... and 43 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@peteski22 peteski22 marked this pull request as draft February 4, 2026 12:31
@peteski22 peteski22 force-pushed the fix/smolagents-reuse-anyllm-client branch 2 times, most recently from cd07ac7 to 8e961f1 Compare February 4, 2026 13:45
@peteski22 peteski22 marked this pull request as ready for review February 4, 2026 13:48
@peteski22 peteski22 requested a review from daavoo February 4, 2026 14:27
Copy link
Contributor

@HareeshBahuleyan HareeshBahuleyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and worked with a sample script using smolagents with openai/gpt-4.1 model

@peteski22 peteski22 force-pushed the fix/smolagents-reuse-anyllm-client branch from 0dcfde2 to 9bcb046 Compare February 4, 2026 17:16
Create an AnyLLM client instance once via AnyLLM.create() instead of
returning the any_llm module from create_client(). This fixes "Event
loop is closed" errors that occurred when making multiple completion
calls, as the functional API created per-call async resources tied to
an event loop that could be closed between calls.

Changes:
- Parse model_id using AnyLLM.split_model_provider() to extract provider
- Store provider config separately from completion kwargs
- Create AnyLLM instance in create_client() for reuse by ApiModel
- Add error handling for malformed model_id with clear message
- Add unit tests for AnyLLMModel class including regression test

Fixes #824
- Remove unnecessary create_client() call in test (already called by ApiModel.__init__)
- Remove redundant comment about simulating completion calls
@peteski22 peteski22 force-pushed the fix/smolagents-reuse-anyllm-client branch from 9bcb046 to 5d87019 Compare February 4, 2026 17:17
@peteski22 peteski22 merged commit b2d17f7 into main Feb 6, 2026
10 checks passed
@peteski22 peteski22 deleted the fix/smolagents-reuse-anyllm-client branch February 6, 2026 12:02
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.

smolagents AnyLLM client should be created only once and reused for completions

2 participants