Skip to content

Merge User-Agent headers when cloning HTTP client#383

Merged
heyitsaamir merged 3 commits intomainfrom
copilot/merge-user-agent-headers
Apr 13, 2026
Merged

Merge User-Agent headers when cloning HTTP client#383
heyitsaamir merged 3 commits intomainfrom
copilot/merge-user-agent-headers

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 10, 2026

When Client.clone() was called with headers containing a User-Agent, it would overwrite the base client's User-Agent rather than merging. This loses the SDK's default UA token when callers supply their own.

Changes

  • packages/common/src/microsoft_teams/common/http/client.py

    • Added _merge_headers(base, overrides) helper: performs a case-insensitive User-Agent merge (space-concatenated, token-based dedup to avoid substring false positives); all other headers follow standard last-write-wins semantics
    • Client.clone() now calls _merge_headers() instead of a plain dict unpack
  • packages/common/tests/test_client.py

    • Added 7 tests covering: preservation without overrides, merge when both sides define UA, non-UA overrides leave UA untouched, case-insensitive key matching, duplicate-token suppression, substring false-positive guard, and override-only UA when base has none

Example

client = Client(ClientOptions(headers={"User-Agent": "teams-py/1.0"}))
clone = client.clone(ClientOptions(headers={"User-Agent": "myapp/2.0"}))

# Before: "myapp/2.0"  (SDK UA lost)
# After:  "teams-py/1.0 myapp/2.0"
print(clone._options.headers["User-Agent"])

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • releases.astral.sh
    • Triggering command: /home/REDACTED/.local/bin/uv uv sync --all-packages --group dev (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Implement the change described by PR microsoft/teams.ts#508 ("Merge User-Agent headers when cloning HTTP client") in the Python SDK repository @microsoft/teams.py.

Goal:

  • When the HTTP client is cloned (or a new client is created from an existing client/session), ensure that User-Agent headers are merged rather than overwritten.
  • Preserve any existing default User-Agent set by the library while also incorporating a user-provided User-Agent (or additional UA tokens) when cloning.

Requirements:

  1. Locate the HTTP client implementation and any clone/copy method(s) in the repository.
  2. Identify how headers are stored (e.g., requests.Session headers, httpx.Client headers, custom dict) and how cloning currently works.
  3. Modify cloning logic so that:
    • If both source and override headers contain User-Agent, the resulting User-Agent should include both (e.g., concatenated with a space, or comma per convention) without duplicating identical values.
    • If only one side has User-Agent, keep it.
    • Header merging should be case-insensitive for the header key.
  4. Add/adjust unit tests to cover:
    • Cloning without overrides preserves original User-Agent.
    • Cloning with headers override that includes User-Agent results in merged value.
    • Cloning with headers override without User-Agent leaves original UA intact.
    • Case-insensitive behavior (user-agent, User-Agent).
  5. Update any relevant docs or changelog entry if the repo uses one.

Reference:

Notes:

  • Ensure behavior is consistent across sync/async HTTP clients if both exist.
  • Keep backward compatibility with existing public APIs.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Can you implement this in @microsoft/teams.py ?

Assistant: I’ve queued up the implementation for microsoft/teams.py and I’m now waiting on your confirmation in the prompt to proceed with making the code changes and opening the PR.

This pull request was created from Copilot chat.

Agent-Logs-Url: https://github.com/microsoft/teams.py/sessions/ddf00433-ed81-4239-997d-652fbfc4ec72

Co-authored-by: heyitsaamir <48929123+heyitsaamir@users.noreply.github.com>
Copilot AI changed the title [WIP] Update HTTP client to merge User-Agent headers on clone Merge User-Agent headers when cloning HTTP client Apr 10, 2026
Copilot AI requested a review from heyitsaamir April 10, 2026 23:54
@heyitsaamir heyitsaamir marked this pull request as ready for review April 11, 2026 06:11
Copilot AI review requested due to automatic review settings April 11, 2026 06:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the common HTTP Client.clone() behavior so User-Agent headers are merged (instead of overwritten) when callers supply additional headers during cloning, preserving the SDK’s default UA token.

Changes:

  • Added _merge_headers(base, overrides) to merge headers with special-case User-Agent concatenation.
  • Updated Client.clone() to use _merge_headers() for header merging.
  • Added unit tests covering User-Agent merge behavior and edge cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packages/common/src/microsoft_teams/common/http/client.py Adds header merge helper and switches clone() to UA-aware merging.
packages/common/tests/test_client.py Adds clone-related tests validating UA preservation/merging and dedup behavior.

- Store User-Agent under canonical key when base headers have none
- Add test for key normalization when override uses lowercase
- Add test for multi-token User-Agent override

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@heyitsaamir heyitsaamir merged commit 4977b59 into main Apr 13, 2026
7 checks passed
@heyitsaamir heyitsaamir deleted the copilot/merge-user-agent-headers branch April 13, 2026 22:06
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.

4 participants