PyMongo 4.16.0 has been released!

PyMongo 4.16.0 released

PyMongo 4.16.0 is now available on PyPI. This release includes several security, compatibility, and feature updates.

Highlights

Safer InvalidDocument error handling

To reduce the risk of leaking sensitive application data in exception messages:

  • Invalid documents are no longer embedded in bson.errors.InvalidDocument error messages.
  • Instead, the offending document is now available via:
    from bson.errors import InvalidDocument
    
    try:
        collection.insert_one(bad_doc)
    except InvalidDocument as exc:
        # Access the invalid document programmatically
        bad = exc.document
    

This improves security while still allowing programmatic inspection and debugging.

Updated dnspython requirement

PyMongo now requires:

  • dnspython >= 2.6.1

Older dnspython 1.x versions are no longer maintained, and 2.6.1 or newer is required to address CVE-2023-29483. If you pin dnspython in your environment, ensure it meets this minimum version.

Eventlet support removed

Eventlet support has been removed:

  • Eventlet is being sunset by its maintainers.
  • It also has compatibility issues with PyMongo’s dnspython dependency.

If you previously relied on Eventlet monkey-patching with PyMongo, you should migrate to a supported concurrency model (for example, asyncio with PyMongo’s asynchronous API, or gevent).

Zstandard compression updates

Zstandard (ZSTD) support has been modernized:

  • On Python 3.14+, PyMongo uses Zstandard support from the standard library.
  • On older Python versions, PyMongo uses the backports.zstd package.

If you use Zstandard compression, you should ensure backports.zstd is available in environments running Python < 3.14.

Typing improvements for find_one_and_*

Type hints have been corrected for:

  • find_one_and_* methods on:
    • pymongo.asynchronous.collection.AsyncCollection
    • pymongo.synchronous.collection.Collection

The return type now correctly includes None, reflecting that no document may be found:

from typing import Optional, Dict, Any
from pymongo.collection import Collection

coll: Collection[Dict[str, Any]]
doc: Optional[Dict[str, Any]] = coll.find_one_and_update({"x": 1}, {"$set": {"y": 2}})

This improves static type-checking and IDE support.

NumPy 1D array support in BinaryVector

bson.binary.BinaryVector now supports NumPy 1D arrays:

  • You can pass a 1D NumPy array directly when constructing or encoding vector data.
  • This simplifies integration with numerical and ML workloads that use NumPy.

Example:

import numpy as np
from bson.binary import BinaryVector

arr = np.array([1.0, 2.0, 3.0], dtype="float32")
vec = BinaryVector(arr)
collection.insert_one({"embedding": vec})

ClientEncryption crypt shared library loading changes

For client-side encryption:

  • pymongo.encryption.ClientEncryption no longer loads the crypt_shared library automatically if another instance is already loaded by the application.
  • This avoids errors of the form:

    MongoCryptError: An existing crypt_shared library is loaded by the application

The crypt shared library will only be loaded when the linked library search path is correctly configured, preventing conflicts with other components that may already be using crypt_shared.


As always, we recommend reviewing your dependency constraints and testing against this release in a staging environment before rolling out to production. If you encounter issues, please open a ticket in the MongoDB JIRA under the PYTHON project.