The point is your composition is probably ok, but you let the mech become too strong. Against mech you need to constantly put pressure on, especially deny gas wherever possible to slow him down and keep the gas heavy unit count lower then he would like, and most important of all use the biggest weakness of mech : his slowness. For example do an attack at the left side to lure his troopps there, then do a worse attack at the right side and do damage 30 seconds hardly molested. Constantly switch sides to attack, give him the run around
Generally you need colossus and tempest as damage dealer (tempests have more range then siege tanks even, use them to break tank lines), imortals to soak up damage, lot's of blink stalkers and perhaps a few carriers to protect your damage dealers should anything come back there(like flank you or something
Also a really deadly combo to mech is Tempest/Carrier, if you have like 6 of each with good upgrades, he needs one hell of a lot of units to deal with that. Just have a few immortals and mass blink stalkers to keep back his troops, and a few observers to find targets for your tempests. That is extremely annoying to mech
What I often do is build a nice ground army (colossus and blink stalers mostnly, with a few immortals and sentries for good meassure) and while that walks ahead, and dioes as much damagwe as it possibly can, I build my tempest&carrier fleet, but keep them back till your ground army has lost at least half it*s troops or you have 6 each (ideally build 6 star ports, 4 are also O.K.). You do not really need the ground army any longer (youz even need to get rid of at löeastn half to have enough food left for carriers and tempests), but do not waste it.
Extremely important is to do the air upgrades early on, and I: also do the shield upgrades, si.nce both air and ground profi.t from them). You see he now adapts to your ground troops (or has already, since tat is all he saw from you all game, hide your star ports as good as possible, I usually spread them around several bases, so he may see one but, which makes sense since a few void raids really complement your ground force, but he will probably never suspect 4-6)and your next attack is pretty much the opposite, so I often catch him unaware.
Even if he goes mass vikings (like 15 or so)it is not that easy to deal with 6 carriers and 6-8 tempests, and why should you build THAT much if you where expecting a ground force. What is mech gonna do now ? Neither his tanks nor his hellbats can shoot up, he is mech so no mass marines either, he just got his vikings, and I really doubt he has enough to deal with that, especially if you micro well and get em down asap, directly aim at one after the next. Till he is near enough to even shoot at the actual targets, he should have lost at least like 4-6 already, depending how much health they had left, so he has like what 6-8 left, and you of course keep pumping carriers and tempests out, perbhaps a few voidrays for good meassure if you need to actually hunt something down. Of course do no-t forget your observers, they are cruicial, so cruicial YOUZ SHOULD ALWAYS HAVE 2-3 AT THE BACK TO REPLACE THE 1-2 AT THE FRONT !!!