it wasn't scripted but the director liked it so decided to leave it in the movie
Dustin Hoffman tells the story in a talk show interview of how the scene was filmed on NYC streets. The city did not allow a whole street to be closed, so they filmed between red lights. The police were supposed to stop traffic and designated cars seen in the film were supposed to be driven by people working on the film. A NYC cab got through during the scene and nearly ran over Dustin Hoffman and Jon Voight. Hoffman just continued with the scene. The cab driver in the scene was a cab driver real. Jon Voight confirms that this is true in another TV talk show interview.