From 2b50f39d5d6ca2ed5e551026bab2b975632928d4 Mon Sep 17 00:00:00 2001 From: Md_Mushfiqur Rahim <20mahin20201@gmail.com> Date: Wed, 27 May 2026 04:10:23 +0000 Subject: [PATCH] fix: batch Zep set_ontology calls to respect 10-item API limit Zep Graph API accepts a maximum of 10 entity types or relation types per set_ontology request. When the LLM extracts more than 10 types, the backend sends them all at once, causing a 400 Bad Request. The fix batches entity_types and edge_definitions into chunks of 10 and calls set_ontology multiple times. Fixes #631 --- backend/app/services/graph_builder.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/backend/app/services/graph_builder.py b/backend/app/services/graph_builder.py index 37c9969c..d8863e55 100644 --- a/backend/app/services/graph_builder.py +++ b/backend/app/services/graph_builder.py @@ -284,12 +284,28 @@ class GraphBuilderService: edge_definitions[name] = (edge_class, source_targets) # 调用Zep API设置本体 + # Zep API 限制每批次最多10个实体/关系类型 + BATCH_SIZE = 10 + if entity_types or edge_definitions: - self.client.graph.set_ontology( - graph_ids=[graph_id], - entities=entity_types if entity_types else None, - edges=edge_definitions if edge_definitions else None, + entity_items = list(entity_types.items()) + edge_items = list(edge_definitions.items()) + + max_batches = max( + (len(entity_items) + BATCH_SIZE - 1) // BATCH_SIZE if entity_items else 0, + (len(edge_items) + BATCH_SIZE - 1) // BATCH_SIZE if edge_items else 0, + 1, ) + + for i in range(max_batches): + batch_entities = dict(entity_items[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]) if entity_items else None + batch_edges = dict(edge_items[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]) if edge_items else None + + self.client.graph.set_ontology( + graph_ids=[graph_id], + entities=batch_entities if batch_entities else None, + edges=batch_edges if batch_edges else None, + ) def add_text_batches( self,