[Bugfix] UserAgent Refactoring and Remove Duplicate Custom Prefix#44189
Merged
simorenoh merged 18 commits intoAzure:mainfrom Nov 26, 2025
Merged
[Bugfix] UserAgent Refactoring and Remove Duplicate Custom Prefix#44189simorenoh merged 18 commits intoAzure:mainfrom
simorenoh merged 18 commits intoAzure:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors user agent handling in the Azure Cosmos DB Python SDK to fix a bug where custom user agents were duplicated in initial metadata requests. The key changes move user agent feature flag logic from the request layer into a custom UserAgentPolicy, and ensure the user_agent kwarg is properly cleaned up after policy instantiation.
Key Changes
- Created
CosmosUserAgentPolicythat extends azure-core'sUserAgentPolicyto append Cosmos-specific feature flags (circuit breaker, per-partition failover) - Removed duplicate user agent logic from
_synchronized_request.pyand_asynchronous_request.py - Added
kwargs.pop("user_agent", None)after policy creation to prevent duplication in database account calls - Updated samples to use recommended
user_agent_suffixparameter instead ofuser_agentprefix - Renamed tests from "suffix" to "prefix" to accurately reflect that
user_agentparameter acts as a prefix - Added new test
test_user_agent_prefix_with_featuresto verify feature flags are properly appended
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| azure/cosmos/user_agent_policy.py | New custom policy that handles Cosmos-specific user agent feature flags by extending azure-core UserAgentPolicy |
| azure/cosmos/_cosmos_client_connection.py | Replaced standard UserAgentPolicy with CosmosUserAgentPolicy and added kwargs cleanup to prevent duplication |
| azure/cosmos/aio/_cosmos_client_connection_async.py | Same changes as sync version for async client |
| azure/cosmos/_synchronized_request.py | Removed user agent feature flag logic (moved to policy) and cleaned up imports |
| azure/cosmos/aio/_asynchronous_request.py | Same changes as sync version for async requests |
| tests/test_user_agent_prefix.py | New test file replacing suffix tests, validates user_agent acts as prefix with feature flag support |
| tests/test_user_agent_prefix_async.py | Async version of prefix tests with proper teardown handling |
| tests/test_user_agent_suffix.py | Removed - tests renamed to prefix to reflect actual behavior |
| tests/test_user_agent_suffix_async.py | Removed - tests renamed to prefix to reflect actual behavior |
| samples/user_agent_management.py | Updated to use recommended user_agent_suffix parameter instead of user_agent |
| samples/user_agent_management_async.py | Same changes as sync sample |
| CHANGELOG.md | Added entry documenting the bug fix for duplicated custom user_agent in metadata requests |
API Change CheckAPIView identified API level changes in this PR and created the following API reviews |
simorenoh
reviewed
Nov 26, 2025
simorenoh
reviewed
Nov 26, 2025
simorenoh
reviewed
Nov 26, 2025
simorenoh
approved these changes
Nov 26, 2025
simorenoh
approved these changes
Nov 26, 2025
msyyc
pushed a commit
that referenced
this pull request
Nov 27, 2025
…4189) * change workloads based on feedback * add staging yml file * add staging yml file * refactor useragent changes for enabled features and fix bug on init * update samples * update changelog * refactor tests, add suffix tests, react to copilot * pylint unused imports * Update sdk/cosmos/azure-cosmos/azure/cosmos/user_agent_policy.py --------- Co-authored-by: Simon Moreno <30335873+simorenoh@users.noreply.github.com>
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.
Description
user_agent_suffixkwargsuser_agentkwarg adds a prefix to the useragent and add a new test for when a feature is enabled like PPCBAll SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines