Last no helmet ride in the Tour de France
Helmets were entirely optional until 5 May 2003.
On 5 May 2003 the following rules were introduced https://web.archive.org/web/20160304090208/http://oldsite.uci.ch/english/news/news_2002/20030502i.pdf

During competitions on the road, a hard shell crash helmet shall be
worn. Except where there are legal provisions to the contrary, riders
taking part in international events for elite men of class 4 and above
may, at their own risk, remove their helmet during the final climb
when the finish of the event or stage is on or at the summit of that
climb, subject to the following conditions: 1) the climb in question
is at least 5 km in length, 2) the helmet may not be removed before
the start of the climb,
3.2 Rider taking the start without the required helmet

Elite men's road races in class 4 and above 200 to 10 000 [CHF fine], plus 50 points deducted from the UCI individual classification [a TdF win is 1000 points]
Other cases: start refused

3.3 Rider removing the obligatory helmet during an event

Elite men's road races in class 4 and above 200 to 10 000 [CHF fine], plus 50 points deducted from the UCI individual classification
Other cases: expulsion from the race and 100


Therefore helmets were mostly compulsory, but elite riders could remove the helmets legitimately on a climb, however they had to be retrieved immediately by a team member.
This rule was certainly in place for 2003 &amp; 2004 https://web.archive.org/web/20040804001045/http://www.letour.fr/2004/docs/reglement_us.pdf
This was changed either on 1 August 2004 (post 2004 TDF) or 1 January 2005
https://one2onecad.files.wordpress.com/2013/02/uci-bike_-equip_-regs_.pdf

Except where legal provisions determine otherwise, riders taking part
in UCI WorldTour events may, at their own risk, refrain from wearing a
helmet during individual time trial races taking place entirely on a
mountainous course. Every discussion regarding the qualification
«entirely on a mountainous course» will be decided by the
commissaires’ panel.

This meant that organisers COULD designate a climb where helmets were not required, but this was not necessarily the case.
It appears from video evidence that for 2005 helmets were optional on certain climbs, but from 2006 onwards this was no longer the case.
The provision was deleted entirely from 1 January 2015
http://web.archive.org/web/20180716223921/http://www.uci.ch/mm/Document/News/Rulesandregulation/16/80/73/1-GEN-20150313-E_English.pdf