mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-05 08:07:05 +00:00
refactor(mcp): make context window detection iterative for the recursion gate
The code-quality recursive_detector CI step bans recursive functions under litellm/; walk the exception cause chain with a bounded loop instead
This commit is contained in:
parent
898182b0e6
commit
38efe98720
1 changed files with 11 additions and 8 deletions
|
|
@ -33,15 +33,18 @@ class SemanticToolFilterContextWindowError(Exception):
|
|||
)
|
||||
|
||||
|
||||
def _is_context_window_error(error: Optional[BaseException], depth: int = 5) -> bool:
|
||||
def _is_context_window_error(error: Optional[BaseException], max_depth: int = 5) -> bool:
|
||||
"""Detect a context-window overflow anywhere in an exception's cause chain."""
|
||||
if error is None or depth == 0:
|
||||
return False
|
||||
if isinstance(error, ContextWindowExceededError):
|
||||
return True
|
||||
if ExceptionCheckers.is_error_str_context_window_exceeded(str(error)):
|
||||
return True
|
||||
return _is_context_window_error(error.__cause__ or error.__context__, depth - 1)
|
||||
current = error
|
||||
for _ in range(max_depth):
|
||||
if current is None:
|
||||
return False
|
||||
if isinstance(current, ContextWindowExceededError):
|
||||
return True
|
||||
if ExceptionCheckers.is_error_str_context_window_exceeded(str(current)):
|
||||
return True
|
||||
current = current.__cause__ or current.__context__
|
||||
return False
|
||||
|
||||
|
||||
class SemanticMCPToolFilter:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue