From b414e53bd69621190c5ca0084b134dde10b40ac4 Mon Sep 17 00:00:00 2001 From: Petr Date: Mon, 10 Nov 2025 22:17:30 -0800 Subject: [PATCH] fix: resolve tool cache invalidation bug and bedrock HTTP/2 compatibility Fixes two medium-priority bugs affecting SDK functionality: 1. Tool Runner Cache Invalidation (MEDIUM): - BaseToolRunner.set_messages_params() promised to invalidate cached tool responses but never cleared _cached_tool_call_response - After mutating request parameters, generate_tool_call_response() kept returning stale cached blocks from previous iteration - Caused 400 errors and runaway loops with wrong tool_use_id - Additionally, _messages_modified flag was not set, causing message duplication in loop iterations - Fix: Clear _cached_tool_call_response and set _messages_modified = True 2. Bedrock HTTP/2 KeyError (MEDIUM): - get_auth_headers unconditionally executed del headers["connection"] - HTTP/2 requests legitimately omit Connection header - HTTPX defaults to HTTP/2 where available - Caused KeyError, preventing SigV4 signing from running - Fix: Use headers.pop("connection", None) for safe removal Changed: - src/anthropic/lib/tools/_beta_runner.py:91-92 Added self._cached_tool_call_response = None Added self._messages_modified = True - src/anthropic/lib/bedrock/_auth.py:61 Changed del headers["connection"] to headers.pop("connection", None) --- src/anthropic/lib/bedrock/_auth.py | 3 ++- src/anthropic/lib/tools/_beta_runner.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/anthropic/lib/bedrock/_auth.py b/src/anthropic/lib/bedrock/_auth.py index 0a8b2109..77e74aa6 100644 --- a/src/anthropic/lib/bedrock/_auth.py +++ b/src/anthropic/lib/bedrock/_auth.py @@ -56,8 +56,9 @@ def get_auth_headers( # The connection header may be stripped by a proxy somewhere, so the receiver # of this message may not see this header, so we remove it from the set of headers # that are signed. + # Note: HTTP/2 requests don't have a Connection header, so we safely remove it only if present. headers = headers.copy() - del headers["connection"] + headers.pop("connection", None) request = AWSRequest(method=method.upper(), url=url, headers=headers, data=data) credentials = session.get_credentials() diff --git a/src/anthropic/lib/tools/_beta_runner.py b/src/anthropic/lib/tools/_beta_runner.py index f466c153..562e3cf6 100644 --- a/src/anthropic/lib/tools/_beta_runner.py +++ b/src/anthropic/lib/tools/_beta_runner.py @@ -88,6 +88,8 @@ def set_messages_params( if callable(params): params = params(self._params) self._params = params + self._cached_tool_call_response = None + self._messages_modified = True def append_messages(self, *messages: BetaMessageParam | BetaMessage) -> None: """Add one or more messages to the conversation history.