Most mid-drive bikes that house the battery in the down tube like yours have an internal battery bracket that consists of an upper and lower part. Bosch and Shimano are both like this and it's likely the Yamaha ST system on your bike is too, but I couldn't find any public support documents for it that have pertinent diagrams or instructions.
On most bikes like this, one or both of the upper and lower battery brackets are independently affixed to the frame, and one or both typically have some kind of sliding/pivoting type capability to the mounting hardware so that the spatial relationship between them can be set precisely to hold the battery without any slop. Problems like you're having are usually the result of the hardware that holds this adjustment coming loose. Again it would be easier to say an exact procedure if Yamaha published real documentation, but usually to solve a problem like you're having, one must find the battery bracket hardware and use it to adjust the position of the movable bracket(s) to be just loose enough to allow the battery to be inserted and no looser. Any looser and the other battery bracket related issue can happen, which is intermittent connectivity problems.
Edit in response to pictures:
It looks like this bolt (or pair of them if there's another behind it) may be what sets the height/distance between the two brackets. If it moves the whole upper assembly, it also may be capable of misaligning the key with its hole in the frame, so you'll need to be careful about that.