Within various scripture texts can be found declarations that, because the written words are God's words to humanity, they are inspired of God, who cannot lie (Titus 1:2 & Hebrews 6:18). A few examples:
"...that he might make thee know that man doth not live by bread only,
but by every word that proceedeth out of the mouth of the Lord doth
man live." Deuteronomy 8:3 A.V.
Jesus quoted those very words to thwart the Devil's temptation to disbelieve what God had said. The Devil was trying to get Jesus to disbelieve God having declared him to be his beloved Son, only 40 days previously. Had Jesus begun to doubt the very words of God to him, personally, he would then have disobeyed God by doing what the Devil suggested. The Devil tried that tactic a second time, and (a second time) Jesus defeated his corrupted use of Old Testament scripture by saying "It is written..." For the third temptation, Jesus retorted, "Get thee hence, Satan: for it is written..." (Matthew 4:1-10)
Notice how the very words of God are written down? This was before any of the New Testament scriptures were written. Yet Jesus verified the truth and purity of that ancient written word, using it to live by the words of God, which is life. Isaiah had earlier shown the same fact. He wrote:
"...the word of our God shall stand forever." "For as the heavens are
higher than the earth, so are my ways higher than your ways, and my
thoughts than your thoughts. For as the rain cometh down, and the snow
from heaven, and returneth not thither, but watereth the earth, and
maketh it bring forth and bud, that it may give seed to the sower and
bread to the eater: so shall my word be that goeth forth out of my
mouth: it shall not return unto me void, but it shall accomplish that
which I please, and it shall prosper in the thing whereto I sent it."
Isaiah 40:8 & 55:9-11 A.V.
Jesus also said, "Heaven and earth shall pass away, but my words shall
not pass away." And, "Is it not written in your law... [quoting Psalm 82:6] ...unto whom the word of God came, and the scripture cannot be broken." Matthew 24:35 & John 10:34-35 A.V.
By calling that Psalm unbreakable, Jesus called the whole of the Hebrew Scriptures the word of God, which cannot be broken; the entirety of them are infallible - unbreakable. Men might try to break them, by disbelief or distortion, but God's words stand fast to eternity, and those words are the very words the Holy Spirit inspired some 40 men to write, over a period 1,600 years, to form a harmonious whole. Nothing has erased them, and for those who believe in the divine preservation of Scripture (as well as their divine inspiration), their faith is that which Paul spoke of here:
"So then faith cometh by hearing, and hearing by the word
of God." Romans 10:17 A.V.
Hearing God's words verbally though preaching the gospel, or reading those words in scripture - either way, God uses his words to impart life. We have those words of God and Christ written down throughout the entire Holy Bible, and all the writers testify to that, living their lives by the written and spoken word of God. We have that written word to this day, and right from the earliest times of the Church, its leaders have vouched for its utter truthfulness.
But you ask for the time of "a formal declaration". Some examples of such statements from the end of the first century have already been given in an answer here. You also ask "why was inerrancy/infallibility formally declared?" That was due to rapid attacks by disbelievers or even those who claimed to believe the truth of scripture but who twisted what it said to come up with false doctrines. Even before the canon of scripture was closed, some of the New Testament apostles had written down warnings about false teachings threatening to lead Christians away from what the scriptures said about Christ and the gospel (the Gnostics, for instance - 1 John 3:11 & 4:1-3.) And Jude spoke of wicked men having crept into gatherings, to lead others astray (for financial gain and for carnal lusts - Jude verses 4 to 19.)
Once doubts are cast on the divine inspiration of Scripture, ("Hath God really said...?") it's not long till people think that the word of God is contained in Scripture, but that not all of it is actually the word of God. Then comes the inevitable doubting whether even some of the things Jesus is recorded as saying are factual.
"As B.B. Warfield argued powerfully against Marcus Dods [1834-1909],
if we do not receive the Christ who is made known to us in Scripture -
the Christ who said of Scripture, 'It cannot be broken' - then the
alternative is another Christ, or, indeed, many Christs, all
modified according to personal opinion." Evangelical Holiness, p.56
Iain H. Murray, The Banner of Truth Trust, 2013