Cardiff prison disproves clichés that inner-city Victorian jails are "places of squalor, violence and despair", the chief inspector of prisons has said.
It has also "bucked the national trend of rising violence" in other prisons and is safer now than three years ago.
But Peter Clarke issued a warning over the "extremely high figure" of 47% of inmates becoming homeless on release.
He called on the Welsh Government, prison and probation service to work together to find solutions.
On average more than 200 prisoners are released a month but nearly half do not have a home to go to, figures show.
Mr Clarke said there was a well established link between homelessness and re-offending and the issue at Cardiff was "striking".
"I've spoken to prisoners, who've explained how, after being released without proper accommodation, they end up on the streets and inevitably the risks and vulnerabilities are far greater," he said.
"I have therefore taken the unusual step of making a recommendation to both HM Prison and Probation Service (HMPPS) and the Welsh Government that they should work together to find solutions to this very serious problem."
The Welsh Government said tackling the issue was a priority.
Notable features found in inspection
65%had a mental health problem
38%arriving prisoners had a drug or alcohol problem
40%perpetrators or potential perpetrators of domestic violence
47%of released prisoners had no accommodation to go to
The report also revealed:
- The level of self-harm was more than three times higher than at a previous inspection
- The community rehabilitation company responsible for probation fails to adequately follow up with prisoners after their release
- Up to 44% of prisoners have a disability and 65% have a mental health problem and their negative survey responses need to be examined.
However, the inspectors also found:
- Few prisoners were locked in their cells during the day and many were involved in purposeful activities
- Only 44% of prisoners felt unsafe, compared with 61% at other local prisons
- The levels of violence were relatively low. There had been 129 violent incidents in the previous six months compared with 139 at the previous inspection in 2016. The latest figures for prisons in England and Wales as a whole show a jump of nearly 40% for the same period
- Prisoners are "safe from exposure" to substance misuse and effective drug supply reduction measures are in place.
Overall, Mr Clarke said: "HMP Cardiff disproves the clichés about inner-city Victorian prisons inevitably being places of squalor, violence and despair.
"The improvements since the last inspection were incredibly encouraging to see, and were testimony to the hard work that had brought them about.
"Importantly, the prison was relatively safe. We often see the corrosive impact of violence on many aspects of prison life when the prisoners themselves are living in fear.
"It is much to Cardiff's credit that while violence figures across the prison estate have generally been rising at an alarming rate in recent years, they had managed to buck the trend. Violence had not increased since the last inspection."
Much of the improvement is down to the "excellent relationships" between staff and prisoners, and despite overcrowding there have been improvements in living conditions too.
Illicit drugs remained a very real problem although the rate of positive drug testing had dropped. All mail was photocopied to stop drugs being hidden in post.
However, the lack of a body scanner was "a significant gap in their defences".
The Ministry of Justice said seven prisons - including Berwyn in Wrexham - would be getting body scanners as part of a £100m investment in prison security.
A spokesman said there was still "plenty of that budget left" but it could not confirm Cardiff would definitely get one, although it noted the prison had been doing a good job in keeping drug use under control.
Cardiff prison governor Helen Ryder said she was particularly pleased how the inspection had picked out the positive prisoner-staff relationships and culture - helped by an increase in staff numbers and with men having the chance to do something meaningful with their day.
"We have great opportunities for learning, skills, qualifications and work - men can access these every day. Officers spend more dedicated time with the men here and help meet their needs and deal with their issues," she said.
Amy Rees, HMPPS director general of probation and Wales, said: "The prison is in a much better shape than just a few years ago - there are better relationships between staff and offenders, good work to intercept drugs, and the introduction of the key worker scheme has helped to limit violence.
"The prison has now introduced a new strategy to reduce the number of prisoners self-harming and the Prison Service is working closely with the Welsh Government to address the issue of homelessness on release."
The Welsh Government said: "Tackling homelessness is a priority. We have put a number of measures in place to prevent people becoming homeless, including Housing First, which specifically supports prison leavers, helping to break the cycle of prison and homelessness."