fix(llm): prevent infinite recursion and params overwrite in call()/acall()#4503
Open
eren-karakus0 wants to merge 1 commit intocrewAIInc:mainfrom
Open
Conversation
…call() When a provider permanently rejects the 'stop' parameter, the retry logic in both call() and acall() recursively calls itself without checking whether 'stop' was already added to drop_params. This causes a RecursionError instead of a clean failure. Additionally, the else branch overwrites self.additional_params with a new dict containing only additional_drop_params, destroying any existing parameters (extra_headers, seed, etc.). Changes: - Add recursion guard: check if 'stop' is already in drop_params before retrying, raise immediately if retry already attempted - Preserve existing additional_params by appending to the list instead of replacing the entire dict - Apply identical fix to both sync call() and async acall() - Add 7 comprehensive tests covering recursion guard, params preservation, and async paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two critical bugs in
LLM.call()andLLM.acall()that occur when a provider permanently rejects thestopparameter with anUnsupported parameter: 'stop'error.Bug 1: Infinite Recursion (RecursionError)
When the
stopparameter is unsupported, the error handler adds"stop"toadditional_drop_paramsand recursively callsself.call()/self.acall(). However, if the provider still rejects the parameter (e.g., due to a provider-side bug, or if the drop_params mechanism fails to strip it), there is no guard to prevent re-entry. The method keeps calling itself until Python hitsRecursionErrorafter ~1000 frames.Before (crashes):
After (fails cleanly after one retry):
Bug 2: Additional Parameters Overwritten
The
elsebranch replacesself.additional_paramsentirely:This destroys any existing keys (
extra_headers,seed,top_k, etc.) that were previously configured. The fix appends to the existing dict instead:Scope
Both bugs exist in the sync
call()method and the asyncacall()method. Both are fixed with identical logic.Test Plan
Added 7 tests in
TestUnsupportedStopRetryGuard:test_retries_once_then_raises_on_persistent_stop_error- Verifies exactly 2 calls (not infinite), then raisestest_preserves_existing_additional_params- Confirmsextra_headersandseedsurvive the retrytest_appends_to_existing_drop_params- Verifies["another_param"]becomes["another_param", "stop"]test_non_stop_exceptions_are_not_retried- Non-stop errors propagate immediately (1 call only)test_acall_retries_once_then_raises_on_persistent_stop_error- Async version of recursion guardtest_acall_preserves_existing_additional_params- Async version of params preservationReproduction
Note
Medium Risk
Touches core LLM invocation error-handling and retry logic in both sync and async codepaths; incorrect conditions could change retry behavior or error propagation across providers.
Overview
Fixes
LLM.call()andLLM.acall()retry behavior when a provider errors on thestopparameter by retrying at most once (detecting whenstopis already inadditional_drop_params) and by addingstoptoadditional_drop_paramswithout clobbering otheradditional_params.Adds a focused test suite (
TestUnsupportedStopRetryGuard) validating single-retry semantics, non-stoperrors not being retried, appending to existing drop params, and preservation of pre-existingadditional_paramsfor both sync and async paths.Written by Cursor Bugbot for commit 03890d7. This will update automatically on new commits. Configure here.