Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,6 +125,37 @@ 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), [Int64(big)], {"nested": [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
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()


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()}])
Expand Down
Loading