@@ -186,6 +186,38 @@ def test_run_with_documents_with_reference_pattern_and_no_match(self, caplog):
186186 assert len (answers [0 ].documents ) == 0
187187 assert "Document index '3' referenced in Generator output is out of range." in caplog .text
188188
189+ def test_run_with_documents_with_zero_reference (self , caplog ):
190+ # References are 1-based, so [0] is out of range and must not silently
191+ # resolve to the last document via a negative index.
192+ component = AnswerBuilder (reference_pattern = "\\ [(\\ d+)\\ ]" )
193+ with caplog .at_level (logging .WARNING ):
194+ output = component .run (
195+ query = "test query" ,
196+ replies = ["Answer: AnswerString[0]" ],
197+ meta = [{}],
198+ documents = [Document (content = "test doc 1" ), Document (content = "test doc 2" )],
199+ )
200+ answers = output ["answers" ]
201+ assert len (answers ) == 1
202+ assert len (answers [0 ].documents ) == 0
203+ assert "Document index '0' referenced in Generator output is out of range." in caplog .text
204+
205+ def test_run_with_zero_reference_in_expanded_range (self , caplog ):
206+ component = AnswerBuilder (reference_pattern = "\\ [([\\ d\\ s,-]+)\\ ]" , expand_reference_ranges = True )
207+ with caplog .at_level (logging .WARNING ):
208+ output = component .run (
209+ query = "test query" ,
210+ replies = ["Answer: AnswerString[0-1]" ],
211+ meta = [{}],
212+ documents = [Document (content = "test doc 1" ), Document (content = "test doc 2" )],
213+ )
214+ answers = output ["answers" ]
215+ assert len (answers ) == 1
216+ assert len (answers [0 ].documents ) == 1
217+ assert answers [0 ].documents [0 ].content == "test doc 1"
218+ assert answers [0 ].documents [0 ].meta ["referenced" ] is True
219+ assert "Document index '0' referenced in Generator output is out of range." in caplog .text
220+
189221 def test_run_with_reference_pattern_set_at_runtime (self ):
190222 component = AnswerBuilder (reference_pattern = "unused pattern" )
191223 output = component .run (
0 commit comments