{"record":{"id":"dea4fcd0d786a4fb","repo":"locustio/locust","slug":"you-need-to-install-pymongo-or-at-least-bson-to-be","errorCode":null,"errorMessage":"You need to install pymongo or at least bson to be able to send/receive ObjectIds","messagePattern":"You need to install pymongo or at least bson to be able to send/receive ObjectIds","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"locust/rpc/protocol.py","lineNumber":13,"sourceCode":"from __future__ import annotations\n\nimport datetime\n\nimport msgpack\n\ntry:\n    from bson import ObjectId\nexcept ImportError:\n\n    class ObjectId:  # type: ignore\n        def __init__(self, s):\n            raise Exception(\"You need to install pymongo or at least bson to be able to send/receive ObjectIds\")\n\n\ndef decode(obj):\n    if \"__datetime__\" in obj:\n        obj = datetime.datetime.strptime(obj[\"as_str\"], \"%Y%m%dT%H:%M:%S.%f\")\n    elif \"__ObjectId__\" in obj:\n        obj = ObjectId(obj[\"as_str\"])\n    return obj\n\n\ndef encode(obj):\n    if isinstance(obj, datetime.datetime):\n        return {\"__datetime__\": True, \"as_str\": obj.strftime(\"%Y%m%dT%H:%M:%S.%f\")}\n    elif isinstance(obj, ObjectId):\n        return {\"__ObjectId__\": True, \"as_str\": str(obj)}\n    return obj\n\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/rpc/protocol.py#L1-L31","documentation":"The msgpack RPC protocol supports bson ObjectId fields for distributed-mode messages, but bson is an optional dependency. If the import failed, the fallback ObjectId class raises this Exception when instantiated, telling the user to install pymongo or bson.","triggerScenarios":"Running distributed locust (master/worker) with msgpack payloads containing ObjectIds while neither pymongo nor bson is installed; message decode/encode path hits the placeholder class.","commonSituations":"Minimal installs of locust without the pymongo extra; slim Docker images used for master or worker nodes; environments where bson was uninstalled during dependency cleanup.","solutions":["`pip install pymongo` (or `pip install bson`) in the environment running locust distributed mode","Install locust with the appropriate extra if available (e.g. locust[pymongo]-style extras or add to requirements)","Ensure master AND worker nodes both have bson installed"],"exampleFix":"// before\npip install locust  # no bson; distributed ObjectId messages fail\n// after\npip install locust pymongo\n# or add to requirements.txt: pymongo>=4.0","handlingStrategy":"validation","validationCode":"try:\n    import bson  # noqa\nexcept ImportError:\n    raise SystemExit(\"Install pymongo or bson for distributed-mode ObjectId support: pip install pymongo\")","typeGuard":"def bson_available() -> bool:\n    try:\n        import bson\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    master = Environment(...)\n    master.create_master_runner()\nexcept Exception as e:\n    if \"pymongo or at least bson\" in str(e):\n        raise RuntimeError(\"pip install pymongo on master and workers\") from e\n    raise","preventionTips":["Add pymongo/bson to requirements for distributed deployments","Install the dependency on ALL nodes (master and workers)","Keep it in Dockerfiles for locust images used in distributed mode"],"tags":["distributed","dependency","bson","msgpack"],"backgroundTag":"missing-optional-dependency","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}