I hope this hasn't been suggested before, because it seems quite obvious. I searched et al, but found nothing.
Would it be possible to have semi- and megabeasts take up multiple tiles, like wagons? Semis could be 2x2 and megas 3x3
This would have numerous effects - first, you could protect yourself against dragons by simply having narrow entrances, although they would still be able to smash doors (with a swipe of tail/claws) and breathe fireballs down the corridors.
To balance this swing in favour of the fortress, however, it would also get rid of the "titan caught in a wooden cage" syndrome. Other traps would still be effective, of course, but cages would trigger against megabeasts, but always fail (and in the case of wooden ones, destroyed)
To prevent against self-sufficient fortresses just holing up and ignoring the beast outside (although this would be a valid short-term tactic), perhaps have nobles generate mandates to rid the fort of the megabeast. Of course, the fact that trade and immigrants would be somewhat curtailed/toasted would probably make this necessary anyway at some point.
Plus, it would make for great epicness when your beleaguered fortress slowly trains every carpenter, farmer, miner and weaver with a rag-tag collection of weapons and armour, then leads them out in a glorious do-or-die charge against the beast that has plagued the fortress for three whole years.