bulk_write does not apply CodecOptions to upserted_ids result:
client = MongoClient(uuidRepresentation='standard') collection = client.test.test # normal insertion gives UUID representation result = collection.insert_one({'_id': uuid.uuid4()}) result.inserted_id >>> UUID('1ec97f53-b1ae-471b-9e0c-157ca9b02953') doc = {'_id': uuid.uuid4()} result = collection.replace_one(doc, doc, upsert=True) result.upserted_id >>> UUID('c03ba52b-e594-47ca-8f12-249ae8eabf05') # bulk gives binary representation (this is the bug) doc = {'_id': uuid.uuid4()} result = collection.bulk_write([ReplaceOne(doc, doc, upsert=True)]) result.upserted_ids >>> {0: Binary(b'\xb6\r%\xf2\xadOMo\x919\xc4\x98P\xe5\n\x02', 4)}
Reported here https://www.mongodb.com/community/forums/t/uuid-representation-mismatch-with-pymongo-4-0-1/143570