The length of time you should have a personal trainer will depend on your individual goals. If you're new to exercise and need guidance on getting started, you might benefit from a few months of sessions. If you have a specific goal in mind, like running a marathon or losing a certain amount of weight, you might need a trainer for a longer period.
That said, a good trainer will aim to make you independent over time. They should equip you with the knowledge and skills to manage your own fitness program. Ultimately, the goal is for you to feel confident and capable in your ability to maintain your fitness independently.