Skip to content

Cosmos: change query response headers to only return latest page#47172

Merged
simorenoh merged 7 commits into
mainfrom
query-headers-fix
May 28, 2026
Merged

Cosmos: change query response headers to only return latest page#47172
simorenoh merged 7 commits into
mainfrom
query-headers-fix

Conversation

@simorenoh
Copy link
Copy Markdown
Member

@simorenoh simorenoh commented May 27, 2026

Simplify query response headers to last-page-only

Replaces the unbounded list[CaseInsensitiveDict] accumulated per page with a single CaseInsensitiveDict that is overwritten on each page fetch. This avoids memory growth for queries spanning thousands of pages.

Changes

  • _cosmos_responses.py — Removed get_last_response_headers() and the by_page() override from CosmosItemPaged/CosmosAsyncItemPaged. get_response_headers() now returns a copy of the most recent page's headers (consistent with CosmosDict/CosmosList).
  • _cosmos_client_connection.py / _cosmos_client_connection_async.py — Changed __QueryFeed from appending to a shared list to overwriting a shared dict via clear() + update().
  • Tests — Updated all assertions and added per-page x-ms-item-count validation to prove headers refresh each page.
  • CHANGELOG — Added breaking change entry under 4.16.0b3.

Copilot AI review requested due to automatic review settings May 27, 2026 18:20
@simorenoh simorenoh requested a review from a team as a code owner May 27, 2026 18:20
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

This PR changes Cosmos query pagination header capture from retaining all page headers to exposing only the latest page’s headers, reducing memory growth for large paginated queries.

Changes:

  • Replaces per-page header lists with a shared latest-header CaseInsensitiveDict.
  • Removes get_last_response_headers() and updates tests for latest-page semantics.
  • Adds a breaking-change changelog entry for the API behavior change.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_responses.py Updates paged response wrappers to return latest headers only.
sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py Updates sync query header capture to overwrite latest headers.
sdk/cosmos/azure-cosmos/azure/cosmos/aio/_cosmos_client_connection_async.py Updates async query header capture to overwrite latest headers.
sdk/cosmos/azure-cosmos/tests/test_query_response_headers.py Adjusts sync tests for latest-page header behavior.
sdk/cosmos/azure-cosmos/tests/test_query_response_headers_async.py Adjusts async tests for latest-page header behavior.
sdk/cosmos/azure-cosmos/CHANGELOG.md Documents the breaking change.

Comment thread sdk/cosmos/azure-cosmos/tests/test_query_response_headers.py Outdated
Comment thread sdk/cosmos/azure-cosmos/tests/test_query_response_headers_async.py Outdated
Comment thread sdk/cosmos/azure-cosmos/CHANGELOG.md Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_responses.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_responses.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/aio/_cosmos_client_connection_async.py Outdated
simorenoh and others added 3 commits May 27, 2026 14:26
- Add None guard on kwargs.pop('response_headers') in CosmosItemPaged
  and CosmosAsyncItemPaged constructors
- Empty result tests: assert x-ms-request-charge presence instead of
  tautological assertIsNotNone
- By-page tests: loosen exact page count assertions since
  max_item_count is a hint, not a contract
- Copies tests: add assertIsNot identity check
- Thread-safety tests: add cross-iterable isolation assertion
  (assertIsNot on headers from different queries)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Member

@tvaron3 tvaron3 left a comment

Choose a reason for hiding this comment

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

LGTM, with friendly reminder to run live tests as well

…subclass

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simorenoh
Copy link
Copy Markdown
Member Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@dibahlfi
Copy link
Copy Markdown
Member

LGTM.

Comment thread sdk/cosmos/azure-cosmos/CHANGELOG.md Outdated
@simorenoh
Copy link
Copy Markdown
Member Author

only tests taking forever to run, everything else passed:
image

simorenoh and others added 2 commits May 27, 2026 19:11
Co-authored-by: Kushagra Thapar <kuthapar@microsoft.com>
@simorenoh simorenoh merged commit 3706f36 into main May 28, 2026
35 checks passed
@simorenoh simorenoh deleted the query-headers-fix branch May 28, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants