microsoft / microsoft/agent-framework
Python: [Bug]: Filtered vector delete tools reject MongoDB ObjectId keys
@eavanvalkenburg is already working on this.
Since Sep 18, 2026.
- Dominant language
- Python
- Stars
- 13.6k
- Forks
- 2.3k
- Avg merge
- 2d 45m
- Merged PRs (30d)
- 358
Description
### Description
`create_delete_tool` fails for a MongoDB collection with `ObjectId` keys when a fixed filter matches a record. The same key works without the filter.
The filtered path serializes the retrieved record through `_encode_vector_tool_record`, which turns its key into a JSON string. It then casts that string to `KeyT` and passes it to `collection.delete`. A cast does not restore the native key, so MongoDB rejects it before sending a delete request.
The expected behavior is to delete the matching records using native `ObjectId` keys and return their JSON strings in `processed_keys`. An empty match should remain a no-op.
### Code Sample
This isolates the problem without a database: database I/O is mocked; the delete tool and MongoDB key validation are real.
```python
import asyncio
from unittest.mock import AsyncMock
from agent_framework import (
Filter,
VectorStoreCollectionDefinition,
VectorStoreField,
create_delete_tool,
)
from agent_framework_mongodb import MongoDBCollection
from bson import ObjectId
from pymongo import AsyncMongoClient
async def main():
key = ObjectId()
client = AsyncMock(spec=AsyncMongoClient)
client.get_database.return_value.get_collection.return_value.delete_many = AsyncMock()
collection = MongoDBCollection(
dict,
definition=VectorStoreCollectionDefinition([
VectorStoreField("key", name="id", type_="ObjectId"),
VectorStoreField("data", name="tenant", type_="str", is_indexed=True),
]),
collection_name="documents",
database_name="vectors",
async_client=client,
)
collection.get = AsyncMock(return_value=[{"id": key, "tenant": "a"}])
delete_tool = create_delete_tool(collection, filter=Filter("tenant", "eq", "a"))
await delete_tool.invoke(arguments={"keys": [str(key)]}, skip_parsing=True)
asyncio.run(main())
```
### Error Messages / Stack Traces
```text
agent_framework.exceptions.IntegrationException: Error deleting records from collection 'documents': MongoDB key values must match the declared 'ObjectId' key type.
```
### Package Versions
- Source: `main` at `0c9944cc9f577d51277ac7c55dbc388b60a577af`
- agent-framework-core: 1.19.0
- agent-framework-mongodb: 1.0.0a260918
- pymongo: 4.18.1
### Python Version
Python 3.12.14 on macOS.
### Additional Context
I have a focused fix using the collection's existing `key_from_json` hook before deletion. The regression test exercises the real MongoDB collection with a mocked driver, including a matching subset, a mapped tenant field, and an empty match. It fails on the current source for the key-type error above; no live MongoDB integration test was run. I will submit the fix with this report.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.