Skip to content
Open
1 change: 1 addition & 0 deletions hugegraph-llm/src/hugegraph_llm/api/graph_extract_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def extract_sync(req: GraphExtractRequest) -> GraphExtractResponse:
req.extract_type,
language=req.language,
split_type=req.split_type,
graph_extract_max_workers=req.graph_extract_max_workers,
client_config=req.client_config,
)
raw = json.loads(result_str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from fastapi import Query
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator

from hugegraph_llm.utils.graph_extract_config import MAX_GRAPH_EXTRACT_WORKERS


class GraphExtractClientConfig(BaseModel):
model_config = ConfigDict(extra="forbid")
Expand All @@ -44,6 +46,12 @@ class GraphExtractRequest(BaseModel):
extract_type: Literal["property_graph"] = Query("property_graph", description="Extraction type.")
language: Literal["zh", "en"] = Query("zh", description="Language for chunk splitting.")
split_type: Literal["document", "paragraph", "sentence"] = Query("document", description="Chunk split granularity.")
graph_extract_max_workers: int = Query(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 REST validation coerces some raw inputs before the shared validator can enforce its contract. A direct probe shows graph_extract_max_workers=true and "1.0" are both accepted here as integer 1, while validate_graph_extract_max_workers() rejects those same raw values. This makes REST behavior inconsistent with the flow/helper entry points and can silently turn malformed input into a valid request. Please add a mode="before" field validator (or equivalent strict raw-input validation) that applies the shared rules, and cover boolean and decimal-string request values with regression tests.

1,
ge=1,
le=MAX_GRAPH_EXTRACT_WORKERS,
description="Maximum parallel chunk extraction calls.",
)
include_meta: bool = Query(False, description="Include vertex/edge/text counts in the response.")
client_config: Optional[GraphExtractClientConfig] = Field(None, description="Request-scoped HugeGraph connection.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class BasePromptConfig:
gremlin_generate_prompt: str = ""
doc_input_text: str = ""
graph_extract_split_type: str = "document"
graph_extract_max_workers: int = 1
schema_generator_query_examples: str = ""
schema_generator_few_shot_examples: str = ""
_language_generated: str = ""
Expand Down Expand Up @@ -140,6 +141,7 @@ def to_literal(val):
"gremlin_generate_prompt": to_literal(self.gremlin_generate_prompt),
"doc_input_text": to_literal(self.doc_input_text),
"graph_extract_split_type": to_literal(self.graph_extract_split_type),
"graph_extract_max_workers": self.graph_extract_max_workers,
"schema_generator_query_examples": to_literal(self.schema_generator_query_examples),
"schema_generator_few_shot_examples": to_literal(self.schema_generator_few_shot_examples),
"_language_generated": str(self.llm_settings.language).lower().strip(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,20 @@ def store_prompt(
schema,
example_prompt,
graph_extract_split_type="document",
graph_extract_max_workers=1,
):
if (
prompt.doc_input_text != doc
or prompt.graph_schema != schema
or prompt.extract_graph_prompt != example_prompt
or prompt.graph_extract_split_type != graph_extract_split_type
or prompt.graph_extract_max_workers != int(graph_extract_max_workers)
):
prompt.doc_input_text = doc
prompt.graph_schema = schema
prompt.extract_graph_prompt = example_prompt
prompt.graph_extract_split_type = graph_extract_split_type
prompt.graph_extract_max_workers = int(graph_extract_max_workers)
prompt.update_yaml_file()


Expand Down Expand Up @@ -405,6 +408,14 @@ def create_vector_graph_block():
label="Graph Extraction Split Type",
info=("document keeps the current behavior; paragraph/sentence split long docs before extraction."),
)
graph_extract_max_workers = gr.Slider(
minimum=1,
maximum=8,
value=prompt.graph_extract_max_workers,
step=1,
label="Graph Extraction Chunk Concurrency",
info="Maximum parallel chunk extraction calls. 1 keeps serial behavior.",
)
graph_extract_bt = gr.Button("Extract Graph Data (1)", variant="primary")
graph_loading_bt = gr.Button("Load into GraphDB (2)", interactive=True)
graph_index_rebuild_bt = gr.Button("Update Vid Embedding")
Expand Down Expand Up @@ -440,6 +451,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
vector_index_btn1.click(clean_vector_index).then(
Expand All @@ -449,6 +461,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
vector_import_bt.click(build_vector_index, inputs=[input_file, input_text], outputs=out).then(
Expand All @@ -458,6 +471,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
graph_index_btn0.click(get_graph_index_info, outputs=out).then(
Expand All @@ -467,6 +481,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
graph_index_btn1.click(clean_all_graph_index).then(
Expand All @@ -476,6 +491,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
graph_data_btn0.click(clean_all_graph_data).then(
Expand All @@ -485,6 +501,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)
graph_index_rebuild_bt.click(update_vid_embedding, outputs=out).then(
Expand All @@ -494,6 +511,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)

Expand All @@ -506,6 +524,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
outputs=[out],
).then(
Expand All @@ -515,6 +534,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)

Expand All @@ -527,6 +547,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
],
)

Expand All @@ -542,6 +563,7 @@ def create_vector_graph_block():
input_schema,
info_extract_template,
graph_split_type,
graph_extract_max_workers,
], # Persist the updated schema-generator examples
)

Expand Down
5 changes: 5 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/flows/graph_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
VALID_SPLIT_TYPES,
)
from hugegraph_llm.state.ai_state import WkFlowInput, WkFlowState
from hugegraph_llm.utils.graph_extract_config import validate_graph_extract_max_workers
from hugegraph_llm.utils.log import log


Expand All @@ -44,6 +45,7 @@ def prepare(
extract_type,
split_type=SPLIT_TYPE_DOCUMENT,
language="zh",
graph_extract_max_workers=1,
**kwargs,
):
# prepare input data
Expand All @@ -53,6 +55,7 @@ def prepare(
raise ValueError("split_type must be document, paragraph, or sentence")

prepared_input.split_type = split_type
prepared_input.graph_extract_max_workers = validate_graph_extract_max_workers(graph_extract_max_workers)
prepared_input.example_prompt = example_prompt
prepared_input.schema = schema
prepared_input.extract_type = extract_type
Expand All @@ -76,6 +79,7 @@ def build_flow(
extract_type,
split_type=SPLIT_TYPE_DOCUMENT,
language="zh",
graph_extract_max_workers=1,
**kwargs,
):
pipeline = GPipeline()
Expand All @@ -89,6 +93,7 @@ def build_flow(
extract_type,
split_type,
language,
graph_extract_max_workers,
**kwargs,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ def node_init(self):
if extract_type == "triples":
self.info_extract = InfoExtract(llm, example_prompt)
elif extract_type == "property_graph":
self.property_graph_extract = PropertyGraphExtract(llm, example_prompt)
self.property_graph_extract = PropertyGraphExtract(
llm,
example_prompt,
max_workers=self.wk_input.graph_extract_max_workers,
)
else:
return CStatus(-1, f"Unsupported extract_type: {extract_type}")
return super().node_init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import json
import re
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any, Dict, List

from hugegraph_llm.config import prompt
from hugegraph_llm.document.chunk_split import ChunkSplitter
from hugegraph_llm.models.llms.base import BaseLLM
from hugegraph_llm.utils.graph_extract_config import validate_graph_extract_max_workers
from hugegraph_llm.utils.log import log

# TODO: It is not clear whether there is any other dependence on the SCHEMA_EXAMPLE_PROMPT variable.
Expand Down Expand Up @@ -78,9 +80,15 @@ def filter_item(schema, items) -> List[Dict[str, Any]]:


class PropertyGraphExtract:
def __init__(self, llm: BaseLLM, example_prompt: str = prompt.extract_graph_prompt) -> None:
def __init__(
self,
llm: BaseLLM,
example_prompt: str = prompt.extract_graph_prompt,
max_workers: int = 1,
) -> None:
self.llm = llm
self.example_prompt = example_prompt
self.max_workers = validate_graph_extract_max_workers(max_workers)
self.NECESSARY_ITEM_KEYS = {"label", "type", "properties"} # pylint: disable=invalid-name

def run(self, context: Dict[str, Any]) -> Dict[str, List[Any]]:
Expand All @@ -90,26 +98,86 @@ def run(self, context: Dict[str, Any]) -> Dict[str, List[Any]]:
context["vertices"] = []
if "edges" not in context:
context["edges"] = []

items = []
for chunk in chunks:
proceeded_chunk = self.extract_property_graph_by_llm(schema, chunk)
log.debug(
"[LLM] %s input: %s \n output:%s",
self.__class__.__name__,
chunk,
proceeded_chunk,
)
items.extend(self._extract_and_filter_label(schema, proceeded_chunk))
if self.max_workers == 1 or len(chunks) <= 1:
chunk_results = [
self._extract_chunk_items(schema, chunk, index, len(chunks)) for index, chunk in enumerate(chunks)
]
else:
chunk_results = self._extract_chunks_concurrently(schema, chunks)

for chunk_items in chunk_results:
items.extend(chunk_items)

items = filter_item(schema, items)
for item in items:
if item["type"] == "vertex":
context["vertices"].append(item)
elif item["type"] == "edge":
context["edges"].append(item)

context["call_count"] = context.get("call_count", 0) + len(chunks)
return context

def _extract_chunks_concurrently(self, schema, chunks):
worker_count = min(self.max_workers, len(chunks))
chunk_results = [None] * len(chunks)
with ThreadPoolExecutor(max_workers=worker_count) as executor:
future_to_index = {
executor.submit(
self._extract_chunk_items,
schema,
chunk,
index,
len(chunks),
): index
for index, chunk in enumerate(chunks)
}
for future in as_completed(future_to_index):
index = future_to_index[future]
chunk_results[index] = future.result()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ A failed chunk does not stop queued LLM calls. Every chunk is submitted above, and when future.result() raises, leaving the ThreadPoolExecutor context waits for running work while queued calls may still start, delaying the error and consuming unnecessary rate limit or paid requests. Please cancel futures that have not started on the first failure, define the policy for already-running calls, and add a regression test proving later queued chunks are not invoked after an early failure.

return chunk_results

def _extract_chunk_items(self, schema, chunk, chunk_index, chunk_count):
try:
proceeded_chunk = self.extract_property_graph_by_llm(schema, chunk)
self._extract_property_graph_json(proceeded_chunk)
self._extract_property_graph_json(proceeded_chunk)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 The same response is parsed and validated twice consecutively, and both return values are discarded. This doubles the regex and JSON-decoding work for every chunk without adding coverage. Please remove the duplicate call or parse once and reuse the result.

except Exception as exc:
raise RuntimeError(f"Graph extraction failed for chunk {chunk_index + 1}/{chunk_count}: {exc}") from exc

log.debug(
"[LLM] %s input: %s \noutput:%s",
self.__class__.__name__,
chunk,
proceeded_chunk,
)
return self._extract_and_filter_label(schema, proceeded_chunk)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still treats malformed model output as a successful empty chunk in several cases. _extract_chunk_items only wraps exceptions raised by extract_property_graph_by_llm, but _extract_and_filter_label returns [] when no JSON is found, when the graph shape is invalid, or when JSON decoding fails. With multiple chunks, one bad chunk can therefore be silently dropped and the overall extraction still returns success, which loses the chunk-level failure visibility described in the PR. Please make parse/format failures distinguishable from a valid empty extraction and raise or report them with the chunk index.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I updated the chunk extraction path so malformed LLM responses are no longer silently treated as empty results.

Each chunk response is now validated for JSON extraction and expected property-graph shape before filtering. If a chunk returns malformed JSON or an invalid graph format, extraction fails with chunk context such as Graph extraction failed for chunk 2/5: ....

I also added regression tests for malformed chunk output, backend concurrency cap validation, and REST request validation.


@staticmethod
def _extract_property_graph_json(text):
text = re.sub(r"```\w*\n?", "", text)
text = re.sub(r"```", "", text)
text = text.strip()

json_match = re.search(r"(\{.*\}|\[.*\])", text, re.DOTALL)
if not json_match:
raise ValueError("No JSON found in property graph extraction response")

json_str = json_match.group(1).strip()
try:
property_graph = json.loads(json_str)
except json.JSONDecodeError as exc:
raise ValueError(f"Invalid property graph JSON: {exc.msg}") from exc

if isinstance(property_graph, list):
return property_graph

if not (isinstance(property_graph, dict) and "vertices" in property_graph and "edges" in property_graph):
raise ValueError("Invalid property graph format; expecting 'vertices' and 'edges'")

return property_graph

def extract_property_graph_by_llm(self, schema, chunk):
prompt = generate_extract_property_graph_prompt(chunk, schema)
if self.example_prompt is not None:
Expand Down
2 changes: 2 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/state/ai_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class WkFlowInput(GParam):
texts: Optional[Union[str, List[str]]] = None # texts input used by ChunkSplit Node
language: Optional[str] = None # language configuration used by ChunkSplit Node
split_type: Optional[str] = None # split type used by ChunkSplit Node
graph_extract_max_workers: int = 1 # bounded chunk extraction concurrency
example_prompt: Optional[str] = None # need by graph information extract
schema: Optional[str] = None # Schema information requeired by SchemaNode
# Request-scoped HugeGraph connection; None falls back to global huge_settings.
Expand Down Expand Up @@ -86,6 +87,7 @@ def reset(self, _: CStatus) -> None:
self.texts = None
self.language = None
self.split_type = None
self.graph_extract_max_workers = 1
self.example_prompt = None
self.schema = None
self.graph_client_config = None
Expand Down
30 changes: 30 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/utils/graph_extract_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

MAX_GRAPH_EXTRACT_WORKERS = 8


def validate_graph_extract_max_workers(value) -> int:
try:
workers = int(value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: Reject fractional worker counts instead of truncating them

hugegraph-llm/src/hugegraph_llm/utils/graph_extract_config.py:21

Evidence

  • int(value) converts 1.9 to 1 and 8.9 to 8, while the REST model rejects fractional values.

Impact

  • Internal flow and demo callers can execute with a different concurrency than requested, producing inconsistent validation across entry points.

Requested fix

  • Reject booleans and non-integral numeric values before conversion, while accepting integer-valued slider output; add regression tests for fractional values.

except (TypeError, ValueError) as exc:
raise ValueError(
f"graph_extract_max_workers must be an integer between 1 and {MAX_GRAPH_EXTRACT_WORKERS}"
) from exc

if workers < 1 or workers > MAX_GRAPH_EXTRACT_WORKERS:
raise ValueError(f"graph_extract_max_workers must be an integer between 1 and {MAX_GRAPH_EXTRACT_WORKERS}")

return workers
Loading
Loading