COMMAND_ACK not received

I am trying to control my drone that is using a pixhawk 6c from a Jetson Orin Nano using pymavlink.
I am using this code:

from pymavlink import mavutil
import time

connection = mavutil.mavlink_connection('tcp:127.0.0.1:5762')
connection.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (connection.target_system, connection.target_component))

# Request data streams
connection.mav.request_data_stream_send(
    connection.target_system,
    connection.target_component,
    mavutil.mavlink.MAV_DATA_STREAM_ALL,  # Request all streams
    10,  # Frequency in Hz
    1  # Enable streaming
)

trigger = input("Press Enter to interrupt the mission: ")
if trigger == 'y':
    print("Mission interrupted")

# Set the Drone to GUIDED mode
mode_id = connection.mode_mapping()['GUIDED']
connection.mav.set_mode_send(
          connection.target_system,
          mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED,
          mode_id
      )
msg = connection.recv_match(type='COMMAND_ACK', blocking=True)
print('Received Mode ACK: %s' % msg)

delay = 5
while delay > 0:
    print("waiting")
    time.sleep(1)
    delay -= 1

# Set the Drone to AUTO mode
mode_id = connection.mode_mapping()['AUTO']
connection.mav.set_mode_send(
          connection.target_system,
          mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED,
          mode_id
      )
msg = connection.recv_match(type='COMMAND_ACK', blocking=True)
print('Received Mode ACK: %s' % msg)

I tested it in SITL first and then on my actual drone and in both instances, I could see the mode being switched to GUIDED on my MissionPlanner screen but I never got the COMMAND_ACK.
I created a seperate code that had the mode changing code 5 times and I got the ACK in that file. There is nothing different in this code besides the fact that the drone is in the air when the mode is changed.
Why am I not receiving COMMAND_ACK?

You can take a look at the source code to see, but my guess is that the vehicle needs to be armed in order to send ACKs.

Sorry I didn’t explain clearly in my original post.
I have tried two different codes. One of them sets the mode to guided when the drone is not armed and on the ground. This code does receive ACK.
The other code, the one I have put in my original post, changes the mode to guided, waits for 5 seconds, and then changes the mode back to auto. This mode is when the drone is flying and is in the middle of a mission that was sent to it from MissionPlanner. This code does not receive ACK.
What else might be the issue?

@amilcarlucas please help me