I'm probably only restating advice someone else already gave, but:
some part of your pathfinding (undirected graph search algorithm) is devoted to expanding on a candidate solution and queuing each of the 'child solutions' reached from that candidate. That is, perhaps you have a priority queue of candidate solutions and you pop off the best-looking solution and examine it. Based on that solution you create all possible "next step" solutions that go one step farther from the solution you have already. This is how undirected graph search algorithms work, and you're already doing this.
Find that part of your code. Some logic in that part must be devoted to identifying collisions and disallowing candidate solutions that cause collisions. For example if your candidate path is right next to a wall, you don't try to expand solutions that take you through the wall.
You should modify that part of your code and have it check for collisions which assume a larger body size. If you are on a grid of squares and your piece is 1x1 wide -- a case you've already solved -- you simply check for collisions in any of the four or eight neighboring squares, checking one square per candidate solution. If your piece is 3x3 wide, then for each candidate solution you must check the nine squares your piece will occupy. If 5x5 wide then for each candidate solution check the fifteen squares your piece will occupy.
That should automatically eliminate candidate solutions that try to take your piece through a gap too small to fit thorugh. Your search algorithm will start to consider those solutions, realize that there are no valid moves through the gap because the gap is too small to fit through, and will eventually settle on an optimal solution.