Skip to content

fix(agent): import _pool_may_recover_from_rate_limit in conversation_loop (#27719)#27732

Closed
luyao618 wants to merge 1 commit into
NousResearch:mainfrom
luyao618:fix/fallback-pool-recover-import-27719
Closed

fix(agent): import _pool_may_recover_from_rate_limit in conversation_loop (#27719)#27732
luyao618 wants to merge 1 commit into
NousResearch:mainfrom
luyao618:fix/fallback-pool-recover-import-27719

Conversation

@luyao618

Copy link
Copy Markdown
Contributor

Summary

Fixes #27719NameError: name '_pool_may_recover_from_rate_limit' is not defined when the eager rate-limit fallback path fires.

Root cause

agent/conversation_loop.py calls _pool_may_recover_from_rate_limit(...) at the rate-limit/billing fallback branch (~L2254), but the helper is defined in run_agent.py and was never imported into agent/conversation_loop.py.

So whenever a primary provider returned a 429/quota error and a fallback chain was configured (e.g. DeepSeek exhausted → OpenRouter fallback as in the issue), the code crashed with NameError instead of switching to the fallback provider.

grep confirms the missing reference:

agent/conversation_loop.py:2254:  pool_may_recover = _pool_may_recover_from_rate_limit(...)
run_agent.py:239: def _pool_may_recover_from_rate_limit(...)   ← defined here

Fix

Use a lazy local import right above the call site:

from run_agent import _pool_may_recover_from_rate_limit
pool_may_recover = _pool_may_recover_from_rate_limit(...)

Why lazy: run_agent.py already imports extensively from agent/* (including agent.conversation_loop), so a top-level from run_agent import ... would create a circular import. The lazy import runs only when the rate-limit fallback branch is hit, which is a cold path.

Test

Added tests/agent/test_fallback_pool_recover_import.py with two checks:

  1. _pool_may_recover_from_rate_limit is callable from run_agent and None short-circuits to False.
  2. AST scan of agent/conversation_loop.py confirms an explicit from run_agent import _pool_may_recover_from_rate_limit exists — so the historical NameError cannot silently regress.
$ pytest tests/agent/test_fallback_pool_recover_import.py \
         tests/run_agent/test_provider_fallback.py \
         tests/agent/test_gemini_fast_fallback.py -q
30 passed in 12.59s

Scope

  • agent/conversation_loop.py — 1 line added (lazy import)
  • tests/agent/test_fallback_pool_recover_import.py — new regression test

…loop

The eager rate-limit fallback path in agent/conversation_loop.py calls
_pool_may_recover_from_rate_limit, but the helper is defined in
run_agent.py and was never imported here. When a primary provider (e.g.
DeepSeek) returned a 429/quota error and a fallback chain (e.g.
OpenRouter) was configured, the codepath crashed with:

    NameError: name '_pool_may_recover_from_rate_limit' is not defined

instead of switching to the configured fallback provider.

Use a lazy local import to avoid the circular import that a top-level
'from run_agent import ...' would introduce (run_agent.py already
imports extensively from agent/*).

Add a regression test that:
1. Confirms the helper is callable from run_agent.
2. Asserts agent/conversation_loop.py contains the explicit import so
   the historical NameError can never silently regress.

Fixes NousResearch#27719
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent loop, run_agent.py, prompt builder P1 High — major feature broken, no workaround duplicate This issue or pull request already exists labels May 18, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #27359 (canonical fix for NameError on _pool_may_recover_from_rate_limit, #27370). Uses lazy import at call site. Multiple competing fix PRs exist: #27359, #27374, #27468, #27534, #27583, #27734, #27735.

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Closing as fixed-on-main

Thanks for the patch — appreciated! Closing as redundant: the same one-line _ra()._pool_may_recover_from_rate_limit(...) wrap landed via PR #28345 (salvage of @AceWattGit's PR #28159), merged 2026-05-19, before this PR could be reviewed. Same code change, just a different cherry-pick.

Several contributors converged on the same fix here (#27370, #27465, #27468, #27583, #27686, #27732, #27734, #27735, #27750, #27891, #27903, #28304) — your diagnosis was correct in every case. Sorry for the duplicate-work cleanup; the volume of independent reports made it hard to coordinate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder duplicate This issue or pull request already exists P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Fallback fails with NameError when primary provider (DeepSeek) runs out of tokens

3 participants