As you’ve discovered, you’re not allowed to use normal Python control flow operators (if, else, for, while, try, etc) inside a PyMC model. These need special implementations that allow them to be represented on a computation graph (so that we can do reverse-mode autodiff).
The special function for conditions is pm.math.switch. The API docs for it aren’t very helpful, so here’s an example model that uses a switch to find a change point on discrete data. It should give you a pattern to follow for your own model.