POLAND’S president Andrzej Duda has said he will veto two contentious bills which were widely seen as assaults on the independence of the judicial system by the ruling party and which sparked days of nationwide protests.

The decision marks the first time Duda has broken openly with Jaroslaw Kaczynski, the powerful leader of the ruling Law and Justice party.

Duda was hand-picked by Kaczynski as the party’s presidential candidate in 2015 and has loyally supported their conservative, nationalist agenda – until now. Duda appeared to take party leaders by surprise with a move that, for now, halts the party’s attempts to consolidate its power.

The party’s moves over the past two years, including a successful neutralising of the constitutional court, have raised concerns about rule of law in a country long considered a model of democratic transition.

As Kaczynski arrived at his office for an emergency party meeting following Duda’s announcement, he refused to answer reporters’ questions and appeared tense. Mateusz Morawiecki, the deputy prime minister and one of the most prominent figures in the party and government, said he was “surprised and disillusioned” by the move.

Lech Walesa – the Nobel Peace Prize laureate, democracy leader and ex-president who led the Solidarity movement that helped overthrow communism – praised Duda’s step, calling it “a difficult and a courageous decision”.

Crowds outside the presidential palace chanted: “We thank you”, and chanted the names of Polish towns where protests had erupted repeatedly over more than a week.

The president said he would veto the two most controversial bills out of three recently passed by MPs aimed at overhauling the judicial system. One would have put the Supreme Court under the political control of the ruling party, giving the justice minister, who is also prosecutor general, power to appoint judges.

Duda said the country’s justice system as it works now needs reform, but he said the planned overhaul threatened to create an oppressive system and that the protests of recent days show that the changes would divide society. He said that there is no tradition in Poland for a prosecutor general to have such large powers and he would not agree to that now.

The president added that he consulted many experts before making his decision, including lawyers, sociologists, politicians and even philosophers. He did not mention having consulted with either Kaczynski or the prime minister Beata Szydlo, in what might signify a rift with party leaders.

He also noted that he had not been consulted by the ruling party on the legislation, a break with procedure, adding that the person who influenced him most was Zofia Romaszewska, a leading anti-communist dissident in the 1970s and 1980s.

He said Romaszewska told him: “Mr President, I lived in a state where the prosecutors general had an unbelievably powerful position and could practically do everything. I would not like to go back to such a state.”

Duda said he was also vetoing a bill changing the functioning of the National Council of the Judiciary. The change would have given MPs greater power over the courts. He will now present new draft laws reforming the Supreme Court and the Council within two months after wide consultations with experts.

However, he said he would sign a third bill which reorganises the functioning of the lower courts.

Duda’s step mostly won the praise of members of the political opposition who had been urging him to veto the bills.

Kamila Gasiuk-Pihowicz, a leading member of the opposition party Modern, called it a step in the right direction and an “act of courage”. She said Duda’s decision also shows the power of civic protests.

The move by the Law and Justice party sparked protests last week in which thousands of Poles took to the streets. Though some people have since expressed disappointment that Duda accepted the third bill.

The party was elected with a narrow majority in 2015, and started almost immediately dismantling some of Poland’s state machinery.

Supporters of the party now control public television and radio, the secret services, and the Constitutional Tribunal, which rules on the constitutionality of legislation and the actions of state bodies. The government has also imposed restrictions on public meetings.