@@ -564,6 +564,74 @@ async def test_extractor_schema_enforcement_inverted_relation_direction() -> Non
564
564
assert result .relationships [0 ].end_node_id .split (":" )[1 ] == "2"
565
565
566
566
567
+ @pytest .mark .asyncio
568
+ async def test_extractor_schema_enforcement_none_relationships_in_schema () -> None :
569
+ llm = MagicMock (spec = LLMInterface )
570
+ llm .ainvoke .return_value = LLMResponse (
571
+ content = '{"nodes":[{"id":"1","label":"Person","properties":'
572
+ '{"name":"Alice"}},{"id":"2","label":"Person","properties":'
573
+ '{"name":"Bob"}}],'
574
+ '"relationships":[{"start_node_id":"1","end_node_id":"2",'
575
+ '"type":"FRIENDS_WITH","properties":{}}]}'
576
+ )
577
+
578
+ extractor = LLMEntityRelationExtractor (
579
+ llm = llm , create_lexical_graph = False , enforce_schema = SchemaEnforcementMode .STRICT
580
+ )
581
+
582
+ schema = SchemaConfig (
583
+ entities = {
584
+ "Person" : {
585
+ "label" : "Person" ,
586
+ "properties" : [{"name" : "name" , "type" : "STRING" }],
587
+ }
588
+ },
589
+ relations = None ,
590
+ potential_schema = None ,
591
+ )
592
+
593
+ chunks = TextChunks (chunks = [TextChunk (text = "some text" , index = 0 )])
594
+
595
+ result : Neo4jGraph = await extractor .run (chunks , schema = schema )
596
+
597
+ assert len (result .nodes ) == 2
598
+ assert len (result .relationships ) == 1
599
+ assert result .relationships [0 ].type == "FRIENDS_WITH"
600
+
601
+
602
+ @pytest .mark .asyncio
603
+ async def test_extractor_schema_enforcement_empty_relationships_in_schema () -> None :
604
+ llm = MagicMock (spec = LLMInterface )
605
+ llm .ainvoke .return_value = LLMResponse (
606
+ content = '{"nodes":[{"id":"1","label":"Person","properties":'
607
+ '{"name":"Alice"}},{"id":"2","label":"Person","properties":'
608
+ '{"name":"Bob"}}],'
609
+ '"relationships":[{"start_node_id":"1","end_node_id":"2",'
610
+ '"type":"FRIENDS_WITH","properties":{}}]}'
611
+ )
612
+
613
+ extractor = LLMEntityRelationExtractor (
614
+ llm = llm , create_lexical_graph = False , enforce_schema = SchemaEnforcementMode .STRICT
615
+ )
616
+
617
+ schema = SchemaConfig (
618
+ entities = {
619
+ "Person" : {
620
+ "label" : "Person" ,
621
+ "properties" : [{"name" : "name" , "type" : "STRING" }],
622
+ }
623
+ },
624
+ relations = {},
625
+ potential_schema = None ,
626
+ )
627
+
628
+ chunks = TextChunks (chunks = [TextChunk (text = "some text" , index = 0 )])
629
+
630
+ result : Neo4jGraph = await extractor .run (chunks , schema = schema )
631
+
632
+ assert len (result .relationships ) == 0
633
+
634
+
567
635
def test_fix_invalid_json_empty_result () -> None :
568
636
json_string = "invalid json"
569
637
0 commit comments