2. Given the quadratic equation in $x$: $a(1+\mathrm{i}) x^{2}+\left(1+a^{2} \mathrm{i}\right) x+a^{2}+\mathrm{i}=0$ has real roots, find the value of the real number $a$.

 untranslated text:
已知关于 $x$ 的二次方程 $a(1+\mathrm{i}) x^{2}+\left(1+a^{2} \mathrm{i}\right) x+a^{2}+\mathrm{i}=0$ 有实根, 求实数 $a$ 的值.

 translated text:
Given the quadratic equation in $x$: $a(1+\mathrm{i}) x^{2}+\left(1+a^{2} \mathrm{i}\right) x+a^{2}+\mathrm{i}=0$ has real roots, find the value of the real number $a$.
2. Let the real root of the equation be $x_{0}$, then $a(1+\mathrm{i}) x_{0}^{2}+\left(1+a^{2} \mathrm{i}\right) x_{0}+a^{2}+\mathrm{i}=0$, which means $\left(a x_{0}^{2}+x_{0}+a^{2}\right)+\left(a x_{0}^{2}+a^{2} x_{0}+1\right) \mathrm{i}=0$.
According to the necessary and sufficient conditions for the equality of complex numbers, we get $\left\{\begin{array}{l}a x_{0}^{2}+x_{0}+a^{2}=0, \\ a x_{0}^{2}+a^{2} x_{0}+1=0 .\end{array}\right.$
Subtract (1) from (2) and rearrange, we get $\left(x_{0}-1\right)\left(a^{2}-1\right)=0$. Therefore, $x_{0}=1$ or $a^{2}=1$. When $x_{0}=1$, substituting into (1), we have $a^{2}+a+1=0$.
Clearly, this equation has no real roots, which contradicts $a \in \mathbf{R}$. Hence, $x_{0} \neq 1$.
When $a^{2}=1$, we have $a= \pm 1$. Similarly, $a=1$ is not suitable. Therefore, $a=-1$, at this time $x_{0}=\frac{1 \pm \sqrt{5}}{2}$. In summary, $a=-1$.