Germany's centre-left Social Democrats (SPD) have agreed to start formal coalition negotiations with Chancellor Angela Merkel's conservatives.
Ever since Germans went to the polls in September, the chancellor's ruling party has tried to form a government.
Initially the SPD ruled out going back into government with Mrs Merkel's centre-right CDU and its Bavarian sister party, the CSU.
But, after months of stalemate, they voted narrowly for formal talks.
In-depth negotiations could start on Monday and Mrs Merkel is keen for them to wrap by 12 February.
SPD leader Martin Schulz had at first refused to take part in a coalition but changed his mind when CDU/CSU coalition talks with the liberal Free Democrats (FDP) and Greens broke down.
The far-right Alternative for Germany (AfD) won 94 seats in parliament in September's vote and became the third biggest force in German politics.
Merkel's sigh of relief
Jenny Hill in Berlin
It's fair to assume Angela Merkel has permitted herself a sigh of relief.
Nevertheless the poker face was on for the cameras as she gave a brief statement welcoming the Social Democrats' decision to approve formal coalition talks. This is her last realistic shot at forming a government and avoiding fresh elections.
It is far from a done deal. Many Social Democrats blame Mrs Merkel for a poor election result in September and there is still significant opposition within the party to another four years as her junior coalition partner.
If and when talks produce a formal agreement, all 440,000 members will be asked - via a postal ballot - to approve the deal. Mrs Merkel isn't out of the woods just yet. But she is a step closer to delivering the government, the stability she promised.
Why has it taken so long?
This is Germany's longest post-war period of coalition-building.
The SPD has governed jointly with the CDU for eight of the last 12 years. But after historically poor poll results for both parties in September, Mr Schulz had vowed to take his party into opposition.
However, pressure mounted on the SPD since November, when Mrs Merkel failed to cobble together a coalition with the liberal FDP and the Greens.
The two parties held preparatory talks earlier this month to see if they could overcome key policy differences and establish a framework for how they would govern.
What were the sticking points?
Mrs Merkel has said she wants to win back voters who deserted her party for the anti-immigration AfD amid tensions over the influx of asylum seekers while the SPD stood for a more open immigration policy.
Earlier this month the parties agreed to limit asylum-seeker arrivals to about 200,000 annually and may also cap at 1,000 a month the number of migrants who will be allowed to join relatives living in Germany.
Mrs Merkel and Mr Schulz have both stressed the need to ensure Germany's "social cohesion".
German official figures show that 280,000 asylum seekers arrived in 2016, a drop of more than 600,000 on the total for 2015. Arrivals fell after EU countries and Turkey tightened border controls.
There have also been disagreements over issues such as taxation. German media reported that the parties had agreed to crack down on tax avoidance in the EU, plugging tax loopholes exploited by US tech giants and other big corporations
What happens if talks fail?
Mrs Merkel could form a less stable minority government with the Greens, with SPD support.
However, she has previously said she would prefer a fresh election to that eventuality.