Skip to content

Python: [Bug]: Filtered vector delete tools reject MongoDB ObjectId keys #8534

Closed

Description

@Shy7777

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: 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.

Activity

  1. added
    pythonUsage: [Issues, PRs], Target: Python
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Sep 18, 2026
  2. added
    reproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow
    on Sep 18, 2026
  3. github-actions commented on Sep 18, 2026

    @github-actions
    Contributor

    🤖 Automated triage reproduction notes (agent-authored — trust but verify)

    Agent analysis

    Repro: python/packages/core/agent_framework/_vectors.py::create_delete_tool lines 2391-2406 passes keys serialized by _encode_vector_tool_record into collection.delete when a fixed filter returns records. MongoDB _prepare_key then rejects the string because the declared native key type is ObjectId, before driver I/O. Minimal repro: mock MongoDBCollection.get to return a record containing a native ObjectId, create a filtered delete tool, and invoke it with the key’s JSON string.

    • Failing test: python/packages/mongodb/tests/mongodb/test_vector_store.py::test_filtered_delete_tool_preserves_object_id_keys
    • Files examined: python/packages/core/agent_framework/_vectors.py, python/packages/core/tests/core/test_vectors.py, python/packages/mongodb/agent_framework_mongodb/_vector_store.py, python/packages/mongodb/tests/mongodb/test_vector_store.py, python/packages/core/pyproject.toml, python/packages/mongodb/pyproject.toml
    • Tests run: test_object_id_keys_round_trip_through_crud_tools, test_filtered_delete_tool_with_empty_match_is_nop, test_filtered_delete_tool_preserves_object_id_keys
    • Reported version: agent-framework-core 1.19.0; agent-framework-mongodb 1.0.0a260918
    • Current version: agent-framework-core 1.19.0; agent-framework-mongodb 1.0.0a260918
  4. added
    agentsUsage: [Issues, PRs], Target: Single agent
    and removed
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

agentsUsage: [Issues, PRs], Target: Single agentpythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions