4. As shown in Figure 4, in $\triangle A B C$, $B C=a, C A=b, A B=$ $\sqrt{a^{2}+b^{2}}, C D \perp$ $A B$. If the incenter of $\triangle A C D$ and $\triangle B C D$ are $I_{1}$ and $I_{2}$ respectively, then $I_{1} I_{2}=$ $\qquad$ (express the answer in terms of $a$ and $b$).
4. $\frac{\sqrt{2}}{2}\left(a+b-\sqrt{a^{2}+b^{2}}\right)$.

Let the inradii of $\triangle A B C$, $\triangle A C D$, and $\triangle B C D$ be $r$, $r_{1}$, and $r_{2}$, respectively, then $r=\frac{a+b-\sqrt{a^{2}+b^{2}}}{2}$.
It is easy to see that $\angle I_{2} D I_{1}=90^{\circ}$.
By the similarity of right triangles $\triangle A B C \backsim \triangle A C D \backsim \triangle C B D$, we get $\frac{r_{1}}{r}=\frac{b}{\sqrt{a^{2}+b^{2}}}$, $\frac{r_{2}}{r}=\frac{a}{\sqrt{a^{2}+b^{2}}}$.
Thus, $r_{1}^{2}+r_{2}^{2}=r^{2}$.
By the Pythagorean theorem, we have
$$
\begin{array}{l}
I_{1} I_{2}=\sqrt{I_{1} D^{2}+I_{2} D^{2}}=\sqrt{\left(\sqrt{2} r_{1}\right)^{2}+\left(\sqrt{2} r_{2}\right)^{2}} \\
=\sqrt{2} \cdot \sqrt{r_{1}^{2}+r_{2}^{2}}=\sqrt{2} r=\frac{\sqrt{2}}{2}\left(a+b-\sqrt{a^{2}+b^{2}}\right) .
\end{array}
$$