From 411a7e9d83065c7827e3635522fd8c4101632d14 Mon Sep 17 00:00:00 2001 From: NNIIOVO Date: Thu, 12 Nov 2026 14:49:54 -0600 Subject: [PATCH 1/2] Python: return plain ints for bson Int64 values from Azure DocumentDB Signed-off-by: NNIIOVO --- .../_vector_store.py | 14 ++++++++- .../azure_documentdb/test_vector_store.py | 29 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/python/packages/azure-documentdb/agent_framework_azure_documentdb/_vector_store.py b/python/packages/azure-documentdb/agent_framework_azure_documentdb/_vector_store.py index b3db9b7f65c..1cd978ca41c 100644 --- a/python/packages/azure-documentdb/agent_framework_azure_documentdb/_vector_store.py +++ b/python/packages/azure-documentdb/agent_framework_azure_documentdb/_vector_store.py @@ -32,6 +32,7 @@ from agent_framework.exceptions import IntegrationException, IntegrationInvalidResponseException from bson import BSON from bson.errors import InvalidDocument +from bson.int64 import Int64 from pymongo import AsyncMongoClient, ReplaceOne from pymongo.asynchronous.collection import AsyncCollection from pymongo.asynchronous.database import AsyncDatabase @@ -248,6 +249,17 @@ def _prepare_key(value: Any, expected_type: str | None) -> str | int: return value +def _normalize_returned_ints(value: Any) -> Any: + """Turn PyMongo's ``Int64`` (used for integers that need 64 bits) back into a plain ``int``.""" + if isinstance(value, Int64): + return int(value) + if isinstance(value, dict): + return {key: _normalize_returned_ints(item) for key, item in cast(dict[Any, Any], value).items()} + if isinstance(value, list): + return [_normalize_returned_ints(item) for item in cast(list[Any], value)] + return value + + def _prepare_untyped_bson_value(value: Any, *, path: str) -> Any: if value is None or isinstance(value, (str, bytes, bool)): return value @@ -879,7 +891,7 @@ def _deserialize_store_models_to_dicts( for item in records: if not isinstance(item, Mapping): raise IntegrationInvalidResponseException("Azure DocumentDB returned a non-document record.") - document = dict(cast(Mapping[str, Any], item)) + document = cast(dict[str, Any], _normalize_returned_ints(dict(cast(Mapping[str, Any], item)))) if "_id" not in document: raise IntegrationInvalidResponseException("Azure DocumentDB returned a document without '_id'.") document[key_name] = _prepare_key(document.pop("_id"), self.definition.key_field.type_) diff --git a/python/packages/azure-documentdb/tests/azure_documentdb/test_vector_store.py b/python/packages/azure-documentdb/tests/azure_documentdb/test_vector_store.py index 77a3a6f21ee..78e31576364 100644 --- a/python/packages/azure-documentdb/tests/azure_documentdb/test_vector_store.py +++ b/python/packages/azure-documentdb/tests/azure_documentdb/test_vector_store.py @@ -12,6 +12,7 @@ from agent_framework import Filter, FilterGroup, VectorStoreCollectionDefinition from agent_framework.exceptions import IntegrationException, IntegrationInvalidResponseException from bson import ObjectId +from bson.int64 import Int64 from pymongo.errors import OperationFailure, PyMongoError from agent_framework_azure_documentdb import AzureDocumentDBCollection, AzureDocumentDBStore @@ -124,6 +125,34 @@ async def test_keys_are_strict_before_io(collection, pymongo_objects, record_fac pymongo_objects[2].find.assert_not_called() +async def test_get_returns_plain_ints_for_bson_int64_values(definition_factory, pymongo_objects): + """PyMongo returns integers that need 64 bits as bson Int64; they come back as int and can be written again.""" + collection = AzureDocumentDBCollection( + dict, definition=definition_factory(key_type="int"), collection=pymongo_objects[2] + ) + big = 2**40 + pymongo_objects[2].find.return_value = AsyncCursor([ + { + "_id": Int64(big), + "body": "x", + "category": "db", + "number": Int64(big), + "ratio": 1.0, + "flag": True, + "tags": [Int64(big)], + "contentVector": [1.0, 0.0, 0.0], + } + ]) + + records = await collection.get([big], include_vectors=True) + + assert type(records[0]["id"]) is int and records[0]["id"] == big + assert type(records[0]["number"]) is int + assert type(records[0]["tags"][0]) is int + await collection.upsert(records, generate_vectors=False) + pymongo_objects[2].bulk_write.assert_awaited_once() + + def test_object_id_response_is_not_implicitly_encoded(collection): with pytest.raises(TypeError, match="strings or signed"): collection._deserialize_store_models_to_dicts([{"_id": ObjectId()}]) From 52abe2987373c4d32e43bd98d612eaa5b2a822d3 Mon Sep 17 00:00:00 2001 From: NNIIOVO Date: Thu, 12 Nov 2026 15:49:31 -0600 Subject: [PATCH 2/2] Python: cover nested Int64 values in the DocumentDB round-trip test Signed-off-by: NNIIOVO --- .../tests/azure_documentdb/test_vector_store.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/packages/azure-documentdb/tests/azure_documentdb/test_vector_store.py b/python/packages/azure-documentdb/tests/azure_documentdb/test_vector_store.py index 78e31576364..3d24df78b3f 100644 --- a/python/packages/azure-documentdb/tests/azure_documentdb/test_vector_store.py +++ b/python/packages/azure-documentdb/tests/azure_documentdb/test_vector_store.py @@ -139,7 +139,7 @@ async def test_get_returns_plain_ints_for_bson_int64_values(definition_factory, "number": Int64(big), "ratio": 1.0, "flag": True, - "tags": [Int64(big)], + "tags": [Int64(big), [Int64(big)], {"nested": [Int64(big)]}], "contentVector": [1.0, 0.0, 0.0], } ]) @@ -148,7 +148,10 @@ async def test_get_returns_plain_ints_for_bson_int64_values(definition_factory, assert type(records[0]["id"]) is int and records[0]["id"] == big assert type(records[0]["number"]) is int - assert type(records[0]["tags"][0]) is int + tags = records[0]["tags"] + assert type(tags[0]) is int + assert type(tags[1][0]) is int + assert type(tags[2]["nested"][0]) is int await collection.upsert(records, generate_vectors=False) pymongo_objects[2].bulk_write.assert_awaited_once()