feat(security): add max_context_messages to prevent retry amplificati…#2074
Open
Waqar53 wants to merge 1 commit into567-labs:mainfrom
Open
feat(security): add max_context_messages to prevent retry amplificati…#2074Waqar53 wants to merge 1 commit into567-labs:mainfrom
Waqar53 wants to merge 1 commit into567-labs:mainfrom
Conversation
…on (567-labs#2056) This PR addresses the retry amplification security issue described in 567-labs#2056. ## Problem During retries, each failed attempt appends 2 messages (assistant response + tool error). With 10 retries, this can cause 506x context growth, leading to: - Token budget exhaustion - Cost amplification - Potential denial of service ## Solution Added parameter that limits the number of messages kept in context during retries. The system message is always preserved. ### Usage ```python client = instructor.from_openai(OpenAI()) response = client.chat.completions.create( model='gpt-4', response_model=MyModel, max_retries=10, max_context_messages=20, # Limit context to 20 messages messages=[...] ) ``` ### Changes - Added `truncate_context_messages()` function in `instructor/core/retry.py` - Added `max_context_messages` parameter to `retry_sync()` and `retry_async()` - Exposed parameter through `patch.py` for user access - Added comprehensive unit tests (10 test cases) ### Testing - All tests pass - Backwards compatible (default is None = no truncation)
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.
…on (#2056)
This PR addresses the retry amplification security issue described in #2056.
Problem
During retries, each failed attempt appends 2 messages (assistant response + tool error). With 10 retries, this can cause 506x context growth, leading to:
Solution
Added parameter that limits the number of messages kept in context during retries. The system message is always preserved.
Usage
Changes
truncate_context_messages()function ininstructor/core/retry.pymax_context_messagesparameter toretry_sync()andretry_async()patch.pyfor user accessTesting