A homeless man with a history of mental illness randomly attacked a 6-year-old boy in front of his grandparent’s house in Queens on Thursday afternoon, seriously injuring the boy, police and sources said.

The child was sitting on the steps of his grandparent’s house on Metropolitan Avenue near 123rd Street in Kew Gardens at about 5 p.m. when the vagrant walked into their driveway, said Rabbi Naftali Portnoy, the boy’s grandfather.

The 35-year-old vagrant then grabbed the child, picked him up and threw him to the concrete, slamming his face on the ground, police said.

His brother rushed into the house and told his grandfather about the attack.

Portnoy called 911 and tailed the vagrant, who was shirtless, while he walked away from the scene down Metropolitan Avenue.

Cops arrived soon after and arrested the suspect, who sat down on the street and said, “I’m bipolar,” the grandfather explained.

The kid was rushed to Cohen Children’s Medical Center and treated for hemorrhaging to the brain and facial contusions, a source said.

“Please keep the child in your prayers,” Portnoy said after the attack.

A law enforcement source said the suspect has a history of mental illness and was arrested for assault in January of this year.

The suspect, who was not immediately identified, has at least three other arrests including two for criminal mischief in Queens Village in 2017, a source said.

The attack comes days after another vagrant beat four sleeping homeless men to death on the streets of Manhattan’s Chinatown.

In that attack, 24-year-old Rodriguez “Randy” Santos, allegedly used a 15-pound, 3-foot-long piece of metal to beat sleeping men near Chatham Square on the Bowery.

Mayor de Blasio pledged to increase homeless outreach after the slayings, but critics say the system needs an overhaul — and blasted the first lady’s “failure” of a mental-health initiative.

Additional reporting by Joe Marino