fix: prevent messages list mutation in reask handlers - #2458
Closed
truecallerabreham wants to merge 4 commits into
Closed
fix: prevent messages list mutation in reask handlers#2458truecallerabreham wants to merge 4 commits into
truecallerabreham wants to merge 4 commits into
Conversation
Collaborator
|
Superseded by #2495, which isolates retry-loop kwargs centrally before reask handlers mutate message lists, preserving cache-key stability across providers. |
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.
Describe your changes
All reask handlers across every provider use
kwargs = kwargs.copy()(shallow copy) at the top, then mutatekwargs["messages"]via.append()or.extend(). Becausedict.copy()is shallow, themessageslist is shared between the original caller's dict and the handler's local copy. Any mutation tokwargs["messages"]inside the handler also modifies the caller's original list.This means:
messageslist permanently contains the reask messagesmax_retriesis exhausted, the caller'smessageslist is corrupted with stale reask contentkwargsdict for a secondcreate()call, the corrupted messages carry overThe fix: Replace
kwargs = kwargs.copy()withkwargs = {**kwargs, "messages": list(kwargs["messages"])}in every reask handler. This creates a new list formessagesso mutations are isolated to the handler's local copy.Checklist before requesting a review
Summary
One-line change per reask handler:
kwargs = {**kwargs, "messages": list(kwargs["messages"])}instead ofkwargs = kwargs.copy().Affected files:
instructor/v2/providers/openai/handlers.pyreask_tools,reask_responses_tools,reask_md_json,reask_defaultinstructor/v2/providers/anthropic/handlers.pyAnthropicToolsHandler.handle_reaskinstructor/v2/providers/mistral/handlers.pyMistralToolsHandler.handle_reask,MistralJSONSchemaHandler.handle_reask,MistralMDJSONHandler.handle_reaskRegression test:
tests/test_messages_mutation.py— verifies that every reask handler does not mutate the caller'smessageslist, including across multiple retry iterations.How to reproduce