litellm - ✅(Solved) Fix TypeError: '>' not supported between 'NoneType' and 'int' when Ollama returns rate limit error [1 pull requests, 1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
BerriAI/litellm#25889Fetched 2026-04-17 08:28:17
View on GitHub
Comments
1
Participants
1
Timeline
4
Reactions
0
Participants
Timeline (top)
commented ×1cross-referenced ×1labeled ×1referenced ×1

Error Message

File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5647, in async_function_with_retries
    if num_retries > 0:
    ^^^^^^^^^^^^^^
TypeError: '>' not supported between instances of 'NoneType' and 'int'

Full traceback:

File "/usr/lib/python3.13/site-packages/litellm/proxy/anthropic_endpoints/endpoints.py", line 53, in anthropic_response
    result = await base_llm_response_processor.base_process_llm_request(
File "/usr/lib/python3.13/site-packages/litellm/proxy/common_request_processing.py", line 992, in base_process_llm_request
    responses = await llm_responses
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5083, in async_wrapper
    return await self._ageneric_api_call_with_fallbacks(
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 3779, in _ageneric_api_call_with_fallbacks
    raise e
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 3766, in _ageneric_api_call_with_fallbacks
    response = await self.async_function_with_fallbacks(**kwargs)
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5489, in async_function_with_fallbacks
    return await self.async_function_with_fallbacks_common_utils(
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5446, in async_function_with_fallbacks_common_utils
    raise original_exception
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5480, in async_function_with_fallbacks
    response = await self.async_function_with_retries(*args, **kwargs)
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5647, in async_function_with_retries
    if num_retries > 0:
TypeError: '>' not supported between instances of 'NoneType' and 'int'

Root Cause

In router.py line 5647, the code assumes num_retries is always an integer:

if num_retries > 0:

But num_retries can be None in certain error paths, causing the TypeError.

Fix Action

Fixed

PR fix notes

PR #25902: fix: add None guard for num_retries comparison in router

Description (problem / solution / changelog)

What's broken?

When Ollama returns a rate limit / session usage limit error, litellm crashes with:

TypeError: '>' not supported between instances of 'NoneType' and 'int'

instead of properly propagating the original error.

Who is affected?

Any user hitting a rate limit through Ollama (or any provider path where num_retries is not explicitly set), especially when optional_pre_call_checks: enforce_model_rate_limits is configured.

Root Cause

In router.py, async_function_with_retries pops num_retries from kwargs (line 5688):

num_retries = kwargs.pop("num_retries")

This can be None when the caller doesn't set it. Later, the comparison on line 5771:

if num_retries > 0:

raises a TypeError because Python cannot compare None > int.

Fix

Added a None guard before the comparison:

if num_retries is not None and num_retries > 0:

This matches the defensive pattern already used elsewhere in the same file (e.g., line 516: if num_retries is not None:). When num_retries is None, the code falls through to raise, correctly propagating the original exception to the caller.

Testing

Verified the fix handles the None case: when num_retries is None, the condition evaluates to False and the original exception is re-raised as expected.

Fixes #25889

Changed files

  • litellm/router.py (modified, +1/-1)

Code Example

litellm.APIConnectionError: Ollama_chatException - {"error":"you ({username}) have reached your session usage limit, upgrade for higher limits: https://ollama.com/upgrade (ref: 54b63ef7-b3a4-402f-bcc2-bca8a892767c)"}

---

TypeError: '>' not supported between instances of 'NoneType' and 'int'

---

File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5647, in async_function_with_retries
    if num_retries > 0:
    ^^^^^^^^^^^^^^
TypeError: '>' not supported between instances of 'NoneType' and 'int'

---

File "/usr/lib/python3.13/site-packages/litellm/proxy/anthropic_endpoints/endpoints.py", line 53, in anthropic_response
    result = await base_llm_response_processor.base_process_llm_request(
File "/usr/lib/python3.13/site-packages/litellm/proxy/common_request_processing.py", line 992, in base_process_llm_request
    responses = await llm_responses
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5083, in async_wrapper
    return await self._ageneric_api_call_with_fallbacks(
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 3779, in _ageneric_api_call_with_fallbacks
    raise e
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 3766, in _ageneric_api_call_with_fallbacks
    response = await self.async_function_with_fallbacks(**kwargs)
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5489, in async_function_with_fallbacks
    return await self.async_function_with_fallbacks_common_utils(
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5446, in async_function_with_fallbacks_common_utils
    raise original_exception
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5480, in async_function_with_fallbacks
    response = await self.async_function_with_retries(*args, **kwargs)
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5647, in async_function_with_retries
    if num_retries > 0:
TypeError: '>' not supported between instances of 'NoneType' and 'int'

---

if num_retries > 0:

---

if num_retries is not None and num_retries > 0:
RAW_BUFFERClick to expand / collapse

Bug Description

When Ollama returns a rate limit/session usage limit error, litellm crashes with a TypeError instead of properly handling the error and returning a meaningful message to the caller.

Error Details

The original error from Ollama:

litellm.APIConnectionError: Ollama_chatException - {"error":"you ({username}) have reached your session usage limit, upgrade for higher limits: https://ollama.com/upgrade (ref: 54b63ef7-b3a4-402f-bcc2-bca8a892767c)"}

This triggers a secondary crash in the retry logic:

TypeError: '>' not supported between instances of 'NoneType' and 'int'

Stack Trace

File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5647, in async_function_with_retries
    if num_retries > 0:
    ^^^^^^^^^^^^^^
TypeError: '>' not supported between instances of 'NoneType' and 'int'

Full traceback:

File "/usr/lib/python3.13/site-packages/litellm/proxy/anthropic_endpoints/endpoints.py", line 53, in anthropic_response
    result = await base_llm_response_processor.base_process_llm_request(
File "/usr/lib/python3.13/site-packages/litellm/proxy/common_request_processing.py", line 992, in base_process_llm_request
    responses = await llm_responses
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5083, in async_wrapper
    return await self._ageneric_api_call_with_fallbacks(
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 3779, in _ageneric_api_call_with_fallbacks
    raise e
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 3766, in _ageneric_api_call_with_fallbacks
    response = await self.async_function_with_fallbacks(**kwargs)
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5489, in async_function_with_fallbacks
    return await self.async_function_with_fallbacks_common_utils(
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5446, in async_function_with_fallbacks_common_utils
    raise original_exception
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5480, in async_function_with_fallbacks
    response = await self.async_function_with_retries(*args, **kwargs)
File "/usr/lib/python3.13/site-packages/litellm/router.py", line 5647, in async_function_with_retries
    if num_retries > 0:
TypeError: '>' not supported between instances of 'NoneType' and 'int'

Expected Behavior

The rate limit error from Ollama should be caught and properly propagated as an or similar exception with the original error message, allowing the caller to handle it appropriately (e.g., implement backoff, notify user, etc.).

Actual Behavior

Instead of propagating the original error, litellm crashes with a TypeError because num_retries is None when the retry logic attempts to compare it with 0.

Root Cause

In router.py line 5647, the code assumes num_retries is always an integer:

if num_retries > 0:

But num_retries can be None in certain error paths, causing the TypeError.

Suggested Fix

Add a defensive check before the comparison:

if num_retries is not None and num_retries > 0:

Or ensure num_retries is always initialized to 0 instead of None.

Environment

  • Python: 3.13
  • litellm: installed via pip (version visible in path as site-packages)
  • Provider: Ollama (via litellm proxy)

extent analysis

TL;DR

The most likely fix is to add a defensive check for num_retries being None before comparing it with an integer.

Guidance

  • Verify that num_retries is indeed None when the TypeError occurs by adding a print statement or a debugger breakpoint before the line that crashes.
  • Add a defensive check as suggested in the issue: if num_retries is not None and num_retries > 0: to prevent the TypeError.
  • Consider initializing num_retries to 0 instead of None to avoid this issue altogether.
  • Review the code paths that lead to num_retries being None to ensure it's the expected behavior.

Example

# Before
if num_retries > 0:
    # retry logic

# After
if num_retries is not None and num_retries > 0:
    # retry logic

Notes

This fix assumes that num_retries being None is a valid state that needs to be handled. If num_retries should always be an integer, the root cause of it being None should be investigated and fixed.

Recommendation

Apply the suggested workaround by adding the defensive check to prevent the TypeError, as it's a straightforward and safe change.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

litellm - ✅(Solved) Fix TypeError: '>' not supported between 'NoneType' and 'int' when Ollama returns rate limit error [1 pull requests, 1 comments, 1 participants]