Step 1: Observe the two inverse tangent terms.
The given integrand is
\[
\tan^{-1}\left(\frac{x}{x^2+1}\right)+\tan^{-1}\left(\frac{x^2+1}{x}\right)
\]
Let
\[
a=\frac{x}{x^2+1}
\]
Then,
\[
\frac{x^2+1}{x}=\frac{1}{a}
\]
So the integrand becomes
\[
\tan^{-1}a+\tan^{-1}\frac{1}{a}
\]
Step 2: Apply inverse tangent identity.
For \(a>0\),
\[
\tan^{-1}a+\tan^{-1}\frac{1}{a}=\frac{\pi}{2}
\]
For \(a<0\),
\[
\tan^{-1}a+\tan^{-1}\frac{1}{a}=-\frac{\pi}{2}
\]
Here,
\[
a=\frac{x}{x^2+1}
\]
Since
\[
x^2+1>0
\]
for all real \(x\), the sign of \(a\) depends only on \(x\).
Thus,
\[
a<0 \quad \text{when} \quad -1\leq x<0
\]
and
\[
a>0 \quad \text{when} \quad 0<x\leq 2
\]
Step 3: Split the integral at \(x=0\).
Therefore,
\[
\int_{-1}^{2}\left[\tan^{-1}\left(\frac{x}{x^2+1}\right)+\tan^{-1}\left(\frac{x^2+1}{x}\right)\right]dx
\]
\[
=
\int_{-1}^{0}\left(-\frac{\pi}{2}\right)dx
+
\int_{0}^{2}\left(\frac{\pi}{2}\right)dx
\]
Step 4: Evaluate the integrals.
\[
\int_{-1}^{0}\left(-\frac{\pi}{2}\right)dx
=
-\frac{\pi}{2}(0-(-1))
\]
\[
=-\frac{\pi}{2}
\]
Also,
\[
\int_{0}^{2}\left(\frac{\pi}{2}\right)dx
=
\frac{\pi}{2}(2-0)
\]
\[
=\pi
\]
Hence,
\[
I=-\frac{\pi}{2}+\pi
\]
\[
I=\frac{\pi}{2}
\]
Step 5: Final conclusion.
Therefore,
\[
\boxed{\frac{\pi}{2}}
\]