File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed
src/neo4j_graphrag/experimental/components
tests/unit/experimental/components Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -225,7 +225,7 @@ async def extract_for_chunk(
225
225
logger .debug (f"Invalid JSON: { llm_result .content } " )
226
226
result = {"nodes" : [], "relationships" : []}
227
227
try :
228
- chunk_graph = Neo4jGraph ( ** result )
228
+ chunk_graph = Neo4jGraph . model_validate ( result )
229
229
except ValidationError as e :
230
230
if self .on_error == OnError .RAISE :
231
231
raise LLMGenerationError ("LLM response has improper format" ) from e
Original file line number Diff line number Diff line change @@ -178,6 +178,24 @@ async def test_extractor_llm_invalid_json() -> None:
178
178
await extractor .run (chunks = chunks )
179
179
180
180
181
+ @pytest .mark .asyncio
182
+ async def test_extractor_llm_invalid_json_is_a_list () -> None :
183
+ """Test what happens when the returned JSON is a valid JSON list,
184
+ but it does not match the expected Pydantic model"""
185
+ llm = MagicMock (spec = LLMInterface )
186
+ llm .ainvoke .return_value = LLMResponse (
187
+ # missing "label" for entity
188
+ content = '[{"nodes": [{"id": 0, "entity_type": "Person", "properties": {}}], "relationships": []}]'
189
+ )
190
+
191
+ extractor = LLMEntityRelationExtractor (
192
+ llm = llm ,
193
+ )
194
+ chunks = TextChunks (chunks = [TextChunk (text = "some text" , index = 0 )])
195
+ with pytest .raises (LLMGenerationError ):
196
+ await extractor .run (chunks = chunks )
197
+
198
+
181
199
@pytest .mark .asyncio
182
200
async def test_extractor_llm_badly_formatted_json_gets_fixed () -> None :
183
201
llm = MagicMock (spec = LLMInterface )
You can’t perform that action at this time.
0 commit comments