O'Leary was first elected to Dáil Éireann as a Labour Party TD for Dublin North-Central at the 1965 general election. His agent was Bob Mitchell, Chairman of Dublin University Fabian Society, who could claim credit in a dirty campaign for picking up transfers to squeeze out the Labour Party front-runner on the 11th recount. When he was first elected to the Dáil, O'Leary encouraged the Labour Party to take a more left-wing stance in its policies. He was initially strongly opposed to the idea of a coalition with Fine Gael, but following the 1969 general election he believed that there was a need for a new approach. When the Labour Party and Fine Gael formed the National Coalition government following the 1973 general election he was appointed Minister for Labour. In 1977, he was narrowly defeated by Frank Cluskey for the leadership of the party. O'Leary was elected to the European Parliament for the Dublin constituency in 1979. Cluskey resigned as Labour Party leader when he lost his Dáil seat at the 1981 general election and O'Leary was elected unanimously to succeed him. In the short-lived Fine Gael–Labour Party government of 1981 to 1982, O'Leary became Tánaiste and Minister for Energy. After the government's defeat at the February 1982 general election he remained as leader until he suddenly resigned both the leadership and his party membership on October 28 in the aftermath of a party conference vote on a potential coalition with Fine Gael. On November 2 he joined Fine Gael, subsequently being elected a TD for that party in the Dublin South-West constituency at the November 1982 general election. He was kept out of cabinet office by his former Labour colleagues. In 1985, O'Leary introduced a private member's bill on divorce which forced the government into holding the 1986 divorce referendum. When the Progressive Democrats were formed in 1985 he considered joining, but remained loyal to Fine Gael and TaoiseachGarret FitzGerald. He did not contest the 1987 general election and afterwards he moved back to Cork and practised as a barrister. He was elected as a Fine Gael member of Cork City Council at the 1991 local elections. He unsuccessfully contested the 1992 general election in Cork North-Central and received about 2% of the valid poll. He was appointed a District Court judge in 1997 by the Fine Gael–Labour Party–Democratic Left coalition government.
Death
O'Leary died in France in May 2006, following a drowning accident in a swimming pool. He was on holiday, having retired as a judge just days earlier.