In a couple of the games I run I have had PCs look to have pets for their characters. Pets are a weird things when it comes to RPGs - especially combat heavy ones like D&D. Why are they weird? Because animals tend to be fairly squishy and easily killed, especially as PCs get stronger. This isn't normally a problem, except that the idea of a pet also implies an emotional attachment.
RAW it looks like a quick recipe for disaster and in character heart break. There's a reason in 5e your familiar is simply 'unsummoned' when reduced to 0 HPs, and the Ranger Beast Master companion gets extra HP and survivability.
So how do you handle it?
In both of my games I've made a simple deal with the players. Positioning in a fight is their responsibility. Beyond that, as long as they don't look to gain some kind of mechanical advantage with the pet in combat, I will not target it. This holds true for monsters and sentient creatures.
A mechanical advantage is more than just attacking. Helping fetch an item, blocking a square, threatening a square for an opportunity attack, getting an aura benefit. Riding it like a mount. All of these are examples of a mechanical benefit.
So far it has worked well. The pet can be there. The pet can do things. But as long as the Player doesn't involve the pet in combat, I also don't involve it in combat. It greatly reduces but doesn't remove the danger in combat. After all, if you're inside with your pet positioning can matter when people are throwing 20' radius AOE templates around. However, it does mean that I'm not going to just randomly obliterate someone's pet their character - and potentially player - is emotionally invested in.
Some people will feel different about this. Sam Riegel on Critical Role constantly jokes about killing the animal pets of Laura Bailey's characters. At the same time, the GM in that game also sees no reason to just randomly take away something the player loves for no reason. Ultimately it is a game, and unless you're going for a gritty, hardcore type game, why not let the creature alone until such time as it makes itself be noticed.