Don't mutate caller message dicts in ClaudeSDKClient.query()#1129
Open
winklemad wants to merge 1 commit into
Open
Don't mutate caller message dicts in ClaudeSDKClient.query()#1129winklemad wants to merge 1 commit into
winklemad wants to merge 1 commit into
Conversation
The AsyncIterable prompt path injected session_id straight into each message dict yielded by the caller's iterable. Beyond the unexpected side effect on caller-owned data, it broke reuse: passing the same messages to query() again with a different session_id left the original id in place (the "session_id" key was already present), so the second call silently sent the stale id and ignored its session_id argument. Build a shallow copy before injecting session_id instead of mutating the caller's dict.
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.
The AsyncIterable prompt path in
ClaudeSDKClient.query()didmsg["session_id"] = session_id, injecting the id into the caller's own message dicts. Because the key is then already present, reusing the same message objects in a laterquery()with a differentsession_idleaves the original id in place — so the SDK silently sends the stale session id and ignores thesession_idargument.Reachable via the public
ClaudeSDKClient.query(prompt=<async iterable>, session_id=...): every such call mutates caller-owned data, and on reuse the wrong session id goes on the wire.Fix copies the dict before injecting (
msg = {**msg, "session_id": ...}). Adds a regression test (test_query_does_not_mutate_caller_messages) covering both no-mutation and honoringsession_idon reuse, verified in asyncio and trio.