Skip to content

Conversation

@padak
Copy link

@padak padak commented Nov 11, 2025

Summary

Fixes two MEDIUM priority bugs affecting SDK functionality.

1. Tool Runner Cache Invalidation Bug

Problem:
BaseToolRunner.set_messages_params() promises to "invalidate any cached tool responses" in its docstring, but never actually cleared _cached_tool_call_response. This caused:

  • Stale cached blocks reused after parameter mutations
  • 400 errors and runaway loops with wrong tool_use_id
  • Message duplication when _messages_modified flag wasn't set

Fix:

  • Clear _cached_tool_call_response = None when params change
  • Set _messages_modified = True to prevent message duplication

2. Bedrock HTTP/2 KeyError

Problem:
get_auth_headers() unconditionally executed del headers["connection"]:

  • HTTP/2 requests don't have a Connection header (HTTP/1.x only)
  • HTTPX defaults to HTTP/2 where available
  • Caused KeyError, preventing SigV4 signing from running

Fix:

  • Use headers.pop("connection", None) for safe removal
  • Works with both HTTP/1.1 (removes header) and HTTP/2 (no-op)

Implementation Details

Cache Invalidation

  • Issue Location: src/anthropic/lib/tools/_beta_runner.py:79-92
  • Root Cause: Method updated _params but never cleared cached tool responses
  • Fix: Added two lines:
    • self._cached_tool_call_response = None - clears cached tool response
    • self._messages_modified = True - prevents message duplication in loops

HTTP/2 Compatibility

  • Issue Location: src/anthropic/lib/bedrock/_auth.py:56-61
  • Root Cause: del headers["connection"] assumes header exists
  • Fix: Changed to headers.pop("connection", None)
  • Safety: Works with httpx.Headers (case-insensitive) and plain dicts

Testing

  • ✅ Cache invalidation is complete (both cache and flag cleared)
  • ✅ HTTP/2 compatibility fix uses best approach
  • ✅ No message duplication edge cases
  • ✅ Safe header removal across HTTP versions

Changes

  • 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)

Impact

  • Tool Runner: Fixes 400 errors and runaway loops when mutating parameters
  • Bedrock Client: Enables HTTP/2 requests for AWS Bedrock integration
  • Backwards Compatibility: Fully maintained, no breaking changes

…lity

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)
@padak padak requested a review from a team as a code owner November 11, 2025 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant