Skip to content

fix(bedrock): use safe dict comprehension to remove connection header - #1352

Open
FuturizeRush wants to merge 4 commits into
anthropics:mainfrom
FuturizeRush:fix/bedrock-auth-connection-header-keyerror
Open

fix(bedrock): use safe dict comprehension to remove connection header#1352
FuturizeRush wants to merge 4 commits into
anthropics:mainfrom
FuturizeRush:fix/bedrock-auth-connection-header-keyerror

Conversation

@FuturizeRush

Copy link
Copy Markdown

Summary

Replace del headers["connection"] with a dict comprehension that safely filters out the header, preventing KeyError when the header is absent.

Bug

lib/bedrock/_auth.py:60:

headers = headers.copy()
del headers["connection"]  # KeyError if "connection" not in headers

The equivalent code in lib/aws/_auth.py:60 already handles this safely:

headers = {k: v for k, v in dict(headers).items() if k.lower() != "connection"}

The bedrock version crashes when connection header is missing, which happens with:

  • HTTP/2 connections (no connection header by spec)
  • Certain proxy configurations that strip it before the SDK sees it

Fix

Align bedrock's implementation with the aws version — one line, same dict comprehension pattern.

Test plan

  • When connection header exists: filtered out (same behavior as before)
  • When connection header is absent: no error (fixed)
  • Case-insensitive matching via .lower() (matches aws version)

`del headers["connection"]` raises KeyError when the header is absent
(e.g. HTTP/2 connections or certain proxy configurations).

The equivalent code in lib/aws/_auth.py already uses a safe dict
comprehension. This aligns the bedrock version with the same pattern.
@FuturizeRush
FuturizeRush requested a review from a team as a code owner April 9, 2026 16:12
Comment thread src/anthropic/lib/bedrock/_auth.py Outdated
# that are signed.
headers = headers.copy()
del headers["connection"]
headers = {k: v for k, v in dict(headers).items() if k.lower() != "connection"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
headers = {k: v for k, v in dict(headers).items() if k.lower() != "connection"}
headers = {k: v for k, v in headers.items() if k.lower() != "connection"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI fails because it expects headers to be an instance of Headers.
We can just keep the original headers.copy() but instead of del headers["connection"] do headers.pop("connection", None)

Rush added 3 commits April 10, 2026 12:32
Fixes pyright reportAssignmentType error -- dict comprehension
returns dict[str, str] which is not assignable to httpx.Headers.
Use new_headers variable instead, matching the pattern in
lib/aws/_auth.py.
Simplify by reusing the headers variable name instead of
introducing new_headers, reducing the diff to a single line change.
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.

2 participants