West Lothian Council has lost a legal bid to gain parental rights over a child whose parents have learning difficulties.
The child's parents appealed to the UK Supreme Court after Scotland's highest court upheld a decision to grant the council a permanence order for her.
Born in 2013, the girl, referred to as EV, has been in care since her birth.
West Lothian Council applied for the permanence order following concern about her father.
He was charged in 2010 with criminal sexual conduct with another learning disabled person of a similar age, although the charges were dropped.
In March 2016, the Lord Ordinary at the Court of Session granted the council a permanence order which extinguished the parental rights and responsibilities on the parents and vested them in the local authority.
The order also specified there should be no contact between EV and the parents and included an order granting authority to adopt.
The parents appealed against the decision but the Inner House of the Court of Session upheld the permanence order, though it quashed the authority to adopt and removed the prohibition on contact.
The mother and father then further appealed to the Supreme Court, which on Wednesday issued its judgment refusing the petition for a permanence order.
The judges found the Lord Ordinary's decision was "deficient in a number of respects" and that he did not determine the threshold issue which must be considered before making a permanence order.
According to the test, the court must not make an order unless it considers it would be better for the child that the order be made than it is not.
It must also be satisfied, in relation to each of the parents, that the child's residence with that person is likely to be "seriously detrimental" to her welfare.
They said: "Although much was said about the local authority's concerns about the father's behaviour years earlier, nothing was said, for example, about how the child's current foster care arrangements were working, or about the prospects of a suitable adoptive placement being found.
"There was no analysis of the merits of her living with a foster carer, who has no intention of adopting her, as compared with her living with her parents.
"At the most basic level, the possibility of her parents being able to offer her a permanent home might have been a relevant factor, particularly if the prospects of her being adopted were poor, to set against the negative factors."
The Lord Ordinary heard evidence from social workers that, even with their extensive support, the mother - who has two children by a previous partner - "might only even master physical care tasks, and not manage the more complex tasks of meeting EV's emotional and social needs".
Social workers were concerned about the father's ability to acquire parenting skills and to co-operate with them.
They said it was apparent the local authority still considered adoption as the best option for the child.
The judges concluded: "In these circumstances, the most sensible way forward is for this court to allow the appeals and refuse the petition, leaving it to the local authority to commence fresh proceedings as and when that may be appropriate."