Repository navigation
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.
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
agent_framework.exceptions.IntegrationException: Error deleting records from collection 'documents': MongoDB key values must match the declared 'ObjectId' key type.
Package Versions
- Source:
mainat0c9944cc9f577d51277ac7c55dbc388b60a577af - 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.
Metadata
Metadata
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
github-actions commentedon Sep 18, 2026
🤖 Automated triage reproduction notes (agent-authored — trust but verify)
Agent analysis
Repro:
python/packages/core/agent_framework/_vectors.py::create_delete_toollines 2391-2406 passes keys serialized by_encode_vector_tool_recordintocollection.deletewhen a fixed filter returns records. MongoDB_prepare_keythen rejects the string because the declared native key type isObjectId, before driver I/O. Minimal repro: mockMongoDBCollection.getto return a record containing a native ObjectId, create a filtered delete tool, and invoke it with the key’s JSON string.python/packages/mongodb/tests/mongodb/test_vector_store.py::test_filtered_delete_tool_preserves_object_id_keysagent-framework-core 1.19.0; agent-framework-mongodb 1.0.0a260918agent-framework-core 1.19.0; agent-framework-mongodb 1.0.0a260918