Lay your cards on the table and have at least 2 segways out of the convo, preferably "positive" ones.
"Ya know I feel kinda bad talking about soandso with them not being here. Almost as if I was gossiping or something".
"Ya know, that reminds me I've been meaning to tell you, I try not to get involved with office politics, I'd much rather focus on what I can do/control".
This is where the segway comes in.
Remember, the person who is talking to you may feel rebuffed and you may just become the target of their gossiping as well. You want to keep them neutral while distancing conversations away from gossip, not antagonize them. Also, never comment on their gossip, it just feeds them. Kinda like a vampire or a troll. Unless you plan on making "a stand" on behalf of the other person.
Save the drama for yo mama.