Skip to content

feat: Cosmos DB Change Feed Mode Support#346

Merged
hallvictoria merged 7 commits into
Azure:devfrom
swapnil-nagar:swapnil/CosmosDBUpdate-Python
Jul 20, 2026
Merged

feat: Cosmos DB Change Feed Mode Support#346
hallvictoria merged 7 commits into
Azure:devfrom
swapnil-nagar:swapnil/CosmosDBUpdate-Python

Conversation

@swapnil-nagar

@swapnil-nagar swapnil-nagar commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Updated Python Azure Functions library to support Cosmos DB change feed modes (LatestVersion and AllVersionsAndDeletes), aligning with Azure WebJobs Extensions upstream implementation.

Changes

  • Type Definitions: Added CosmosDBChangeFeedMode type hint in _cosmosdb.py supporting 'LatestVersion' | 'AllVersionsAndDeletes'
  • Module Documentation: Updated CosmosDBConverter and CosmosDBTriggerConverter class docstrings to document change feed mode behavior
  • Public API: Exported CosmosDBChangeFeedMode type from main __init__.py for public consumption
  • Tests: Added 3 new unit tests validating change feed mode support:
    • test_cosmosdb_change_feed_mode_latest_version_support: Validates LatestVersion mode
    • test_cosmosdb_change_feed_mode_all_versions_and_deletes_support: Validates AllVersionsAndDeletes mode
    • test_cosmosdb_change_feed_mode_type_available: Verifies type availability in public API

Files Modified

  • azure/functions/_cosmosdb.py: Added CosmosDBChangeFeedMode type
  • azure/functions/cosmosdb.py: Updated documentation, exported type
  • azure/functions/__init__.py: Exported and documented CosmosDBChangeFeedMode
  • tests/test_cosmosdb.py: Added change feed mode tests

Alignment

This change mirrors the Node.js library v4.16.1 Cosmos DB updates, ensuring consistent change feed support across both runtimes.

- Added CosmosDBChangeFeedMode type supporting LatestVersion and AllVersionsAndDeletes modes
- Updated CosmosDBConverter and CosmosDBTriggerConverter documentation
- Exported CosmosDBChangeFeedMode from public API
- Added 3 unit tests for change feed mode support
- Updated version to 1.26.0
@swapnil-nagar swapnil-nagar changed the title Cosmos DB Change Feed Mode Support feat: Cosmos DB Change Feed Mode Support Jun 19, 2026
@swapnil-nagar

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 346 in repo Azure/azure-functions-python-library

Comment thread azure/functions/__init__.py Outdated
Comment thread azure/functions/cosmosdb.py Outdated
Comment thread azure/functions/_cosmosdb.py Outdated
@jviau

jviau commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

I am not familiar with E2E-test situation for this repo, but want to make sure the feature is validated either manually or by tests. A specific scenario to validate for is the incoming payload type when using AllVersionsAndDelete. With LatestVersion it will be T, for AVAD it will be ChangeFeedItem<T>. So the JSON representation of this CSharp class: https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.changefeeditem-1?view=azure-dotnet-preview

Comment thread tests/test_cosmosdb.py
@hallvictoria

Copy link
Copy Markdown
Contributor

For Jacob's comment, our Cosmos DB E2E tests are defined here, and we use the Cosmos DB emulator.

If this feature is supported in the emulator, you can create a PR to the python worker repo with the new function apps & tests. If the feature is not supported, we can create a follow-up task to update the Cosmos tests when the emulators have this change. Please let me know if you have any questions for testing!

@hallvictoria

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@jviau

jviau commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@hallvictoria I don't think it is supported in the emulator 😞. That has been the challenge with all my E2E tests also. I manually test with a real CosmosDB then disable the tests before checking in.

@swapnil-nagar

swapnil-nagar commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

I am not familiar with E2E-test situation for this repo, but want to make sure the feature is validated either manually or by tests. A specific scenario to validate for is the incoming payload type when using AllVersionsAndDelete. With LatestVersion it will be T, for AVAD it will be ChangeFeedItem<T>. So the JSON representation of this CSharp class: https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.changefeeditem-1?view=azure-dotnet-preview

I validate the changes after getting access to feature. Here is the sample code.

Once these changes are merged and library is release we will create a sample repo for CosmosDb team, but below is the sample which is working

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import logging

import azure.functions as func

app = func.FunctionApp()


# Latest-version change feed (default mode).
# Equivalent to the Node.js "cosmosLatestVersion" sample.
@app.function_name(name="cosmosLatestVersion")
@app.cosmos_db_trigger(
    arg_name="documents",
    connection="CosmosConnection",
    database_name="%DATABASE_NAME%",
    container_name="%COSMOS_CONTAINER_NAME%",
    lease_container_name="%LEASE_CONTAINER_NAME%",
    lease_container_prefix="py-latest-version",
    create_lease_container_if_not_exists=True,
    start_from_beginning=True,
    change_feed_mode=func.CosmosDBChangeFeedMode.LATEST_VERSION,
)
def cosmos_latest_version(documents: func.DocumentList) -> None:
    logging.info("LatestVersion batch count=%d", len(documents))
    for document in documents:
        item = dict(document)
        logging.info(
            "LatestVersion item id=%s status=%s",
            item.get("id"),
            item.get("status"),
        )


# All-versions-and-deletes change feed (full-fidelity mode).
# Equivalent to the Node.js "cosmosFullFidelity" sample.
# NOTE: Unlike the Node.js CosmosDBChangeFeedItem (which exposes
# current/previous/metadata.operationType), the Python DocumentList binding
# delivers each change as a flat document (current image) that additionally
# carries the change-feed "_lsn" (log sequence number) field.
@app.function_name(name="cosmosFullFidelity")
@app.cosmos_db_trigger(
    arg_name="changes",
    connection="CosmosConnection",
    database_name="%DATABASE_NAME%",
    container_name="%COSMOS_CONTAINER_NAME%",
    lease_container_name="%LEASE_CONTAINER_NAME%",
    lease_container_prefix="py-full-fidelity",
    create_lease_container_if_not_exists=True,
    change_feed_mode=func.CosmosDBChangeFeedMode.ALL_VERSIONS_AND_DELETES,
)
def cosmos_full_fidelity(changes: func.DocumentList) -> None:
    logging.info("FullFidelity batch count=%d", len(changes))
    for index, change in enumerate(changes):
        item = dict(change)
        logging.info(
            "FullFidelity change index=%d id=%s status=%s lsn=%s",
            index,
            item.get("id"),
            item.get("status"),
            item.get("_lsn"),
        )
        logging.info("FullFidelity raw[%d]=%s", index, json.dumps(item, default=str))


image

@swapnil-nagar

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 346 in repo Azure/azure-functions-python-library

@hallvictoria

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@hallvictoria

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@hallvictoria
hallvictoria merged commit 9ec73e0 into Azure:dev Jul 20, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants