How to display mathematics equation from API output

It seems that ChatGPT use KaText to render math, but when I paste API output to KaText Demo website. It goes error.

So How should I display it in frontend?

I do not use KaText but did bump into it because of the ChatGPT output.

It may be that the site you are using does not expect the bookends.

Using KaTeX Live Demo with Chrome this is what I see

\nabla \cdot \mathbf {E} = \frac {\rho} {\varepsilon_0}

Using with Chrome

With bookends

Without bookends