You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hugegraph.apache.org by "atharrva01 (via GitHub)" <gi...@apache.org> on 2026/10/15 07:12:38 UTC
[PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
atharrva01 opened a new pull request, #383:
URL: https://github.com/apache/hugegraph-ai/pull/383
I found that vertex IDs containing apostrophes, like `1:O'Brien`, break Gremlin queries and silently cause graph RAG to lose its graph context. The same issue can also cause `/rag/graph` to return a 500.
I added a `gremlin_str_literal()` helper to escape backslashes and apostrophes, and applied it wherever IDs or keywords are interpolated into Gremlin queries. Plain IDs behave as before.
**Tests:** Added regression tests for the operator, node, and `get_vertex_details`. All three fail on `main` and pass with this fix. Module tests pass and Ruff is clean.
**Note:** Live HugeGraph testing wasn't performed. Apostrophe removal during keyword extraction remains out of scope for this PR.
Before / After: screenshots below.
Before
<img width="942" height="317" alt="image" src="https://github.com/user-attachments/assets/ebbc0fe3-7276-4a51-9619-d4266ee02fcf" />
After
<img width="946" height="710" alt="image" src="https://github.com/user-attachments/assets/66ac076a-be5c-4577-9857-7604d6b40cae" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub) via issues" <is...@hugegraph.apache.org>.
atharrva01 commented on PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#issuecomment-6746451010
Rebased onto latest main. The only conflict was in `test_hugegraph_utils.py`, next to the new schema-backup tests. The code is unchanged. The llm unit suite passes locally on Python 3.14 (1304 passed) and ruff is clean. #385 is rebased on top of this too.
@imbajin, could you take a look when you have a moment?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#issuecomment-6244876445
Thanks for the thorough review! @imbajin @bitflicker64 , Agreed on the edge labels and property key, I'll do those in a separate PR so this one stays as is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "imbajin (via GitHub)" <gi...@apache.org>.
imbajin commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4292842852
##########
hugegraph-llm/src/tests/utils/mock.py:
##########
@@ -74,3 +79,34 @@ def __len__(self):
def search(self, query_vector, top_k=5):
# Simply return the first top_k documents.
return self.documents[: min(top_k, len(self.documents))]
+
+
+class FakeGremlinEndpoint:
+ """Stand-in for HugeGraph's /gremlin endpoint that rejects scripts whose g.V(...) ids don't parse.
+
+ Groovy and Python share the same rules for single-quoted literals with \\' and \\\\ escapes,
+ so ast.literal_eval acts as the parser. Vertex lookups return matching entries from `vertices`;
+ traversals (anything with `.repeat(`) return the `paths` that start at one of the requested ids.
+ """
+
+ def __init__(self, vertices, paths=None):
+ self.vertices = vertices
+ self.paths = paths or []
+ self.scripts = []
+
+ def exec(self, gremlin):
+ self.scripts.append(gremlin)
+ match = re.match(r"g\.V\((.*?)\)\s*[.\n]", gremlin, re.S)
Review Comment:
⚠️ Important. Blocking: no. Summary: This pattern stops at the first `)` followed by `.` even inside a quoted ID, so `g.V('x).y').limit(8)` is truncated to `'x` and rejected by `ast.literal_eval`. Please scan the call boundary while respecting quoted and escaped content, and cover this valid ID. Evidence: static inspection of `FakeGremlinEndpoint.exec` at this head.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4283068254
##########
hugegraph-llm/src/hugegraph_llm/utils/hugegraph_utils.py:
##########
@@ -34,6 +34,11 @@
BACKUP_DIR = str(os.path.join(resource_path, "backup-graph-data-4020", huge_settings.graph_name))
+def gremlin_str_literal(value) -> str:
Review Comment:
Good catch, thanks! I accidentally committed the main versions of those three files in the last commit. Fixed now, all three call sites use the helper again and the tests pass.
I kept the list handling in the fake endpoint so the node test fails for the right reason on main.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4302688706
##########
hugegraph-llm/src/tests/utils/mock.py:
##########
@@ -74,3 +79,34 @@ def __len__(self):
def search(self, query_vector, top_k=5):
# Simply return the first top_k documents.
return self.documents[: min(top_k, len(self.documents))]
+
+
+class FakeGremlinEndpoint:
+ """Stand-in for HugeGraph's /gremlin endpoint that rejects scripts whose g.V(...) ids don't parse.
+
+ Groovy and Python share the same rules for single-quoted literals with \\' and \\\\ escapes,
+ so ast.literal_eval acts as the parser. Vertex lookups return matching entries from `vertices`;
+ traversals (anything with `.repeat(`) return the `paths` that start at one of the requested ids.
+ """
+
+ def __init__(self, vertices, paths=None):
+ self.vertices = vertices
+ self.paths = paths or []
+ self.scripts = []
+
+ def exec(self, gremlin):
+ self.scripts.append(gremlin)
+ match = re.match(r"g\.V\((.*?)\)\s*[.\n]", gremlin, re.S)
Review Comment:
Good point, fixed. The fake now scans the g.V(...) call and skips over quoted text and escapes, so ids like 1:x).y work. Added a test with a few ids like that (parens, brackets, quotes) going through get_vertex_details.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "bitflicker64 (via GitHub)" <gi...@apache.org>.
bitflicker64 commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4286733809
##########
hugegraph-llm/src/hugegraph_llm/nodes/hugegraph_node/graph_query_node.py:
##########
@@ -364,7 +365,7 @@ def _subgraph_query(self, context: Dict[str, Any]) -> Dict[str, Any]:
# TODO: use generator or asyncio to speed up the query logic
for matched_vid in matched_vids:
gremlin_query = VID_QUERY_NEIGHBOR_TPL.format(
- keywords=f"'{matched_vid}'",
+ keywords=gremlin_literal(matched_vid),
Review Comment:
Important: with numeric ids kept numeric, this neighbor query finds numeric-id vertices for the first time, and the path formatter cannot handle them. On a numeric-id graph, graph RAG now loses all graph context instead of returning the vertex that `main` returns. In `_process_vertex`, `matched_str` is `item["id"]`, an `int` here. When that id is already in `v_cache`, line 197 sets `node_str = matched_str`, and `flat_rel += node_str` raises `TypeError: can only concatenate str (not "int") to str`. Any start vertex with two or more paths hits this: every path begins with the start vertex, so the second path finds it in `v_cache`. `operator_schedule` catches the error and returns without `graph_result`. Evidence: against HugeGraph 1.7.0 (RocksDB), I added a `customize_number` vertex `123` linked to two person vertices. At this head, `GraphQueryNode.operator_schedule({"query": "who is n123", "match_vids": [123]})` returns `graph_result_flag=-1` and no `graph_result`. The same call wi
th `graph_query_node.py` from 84ee9c4 returns `['123{name: n123}']`, because `g.V('123')` found no paths there. `test_subgraph_query_keeps_numeric_vertex_ids_numeric` misses this because `FakeGremlinEndpoint` returns no paths for `.repeat(` queries. Please make `_process_vertex` build a string, for example `node_str = str(matched_str)`. Please also extend the numeric test so that the fake returns at least two paths starting at `123`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#issuecomment-6192337455
hi @bitflicker64, I accidentally committed the main versions of those three files in the last commit. Fixed now, all three call sites use the helper again and the tests pass.
I kept the list handling in the fake endpoint so the node test fails for the right reason on main.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4291237205
##########
hugegraph-llm/src/hugegraph_llm/nodes/hugegraph_node/graph_query_node.py:
##########
@@ -364,7 +365,7 @@ def _subgraph_query(self, context: Dict[str, Any]) -> Dict[str, Any]:
# TODO: use generator or asyncio to speed up the query logic
for matched_vid in matched_vids:
gremlin_query = VID_QUERY_NEIGHBOR_TPL.format(
- keywords=f"'{matched_vid}'",
+ keywords=gremlin_literal(matched_vid),
Review Comment:
Thanks for testing this against a real server, good catch. Fixed: _process_vertex now uses str(matched_str) for the cached case. I also let the fake endpoint return paths, and the numeric test now has two paths starting at 123, so it hits that branch. It fails with the same TypeError without the fix.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#issuecomment-6186649732
hi @imbajin , this pr fixed Gremlin query failures caused by apostrophes in vertex IDs by adding proper string escaping and regression tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "bitflicker64 (via GitHub)" <gi...@apache.org>.
bitflicker64 commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4282003844
##########
hugegraph-llm/src/hugegraph_llm/utils/hugegraph_utils.py:
##########
@@ -34,6 +34,11 @@
BACKUP_DIR = str(os.path.join(resource_path, "backup-graph-data-4020", huge_settings.graph_name))
+def gremlin_str_literal(value) -> str:
Review Comment:
Important: nothing calls this helper at the PR head, so the fix is not in the PR. The last commit, baf1bcc, reverts all three call sites that 3f7b54c changed:
- `semantic_id_query.py` `_exact_match_vids`: `vids_str = ",".join([f"'{vid}'" for vid in possible_vids])`
- `graph_query_node.py` `_subgraph_query`: `VERTEX_QUERY_TPL.format(keywords=matched_vids)`, `keywords=f"'{matched_vid}'"` for the neighbor query, and `"'" + kw + "'"` on the property path
- `graph_index_utils.py` `get_vertex_details`: `f"'{vid}'"`
The diff against `main` therefore only adds this function and the tests. A keyword or vertex id with an apostrophe still produces `g.V('1:O'Brien')`, which is the failure the PR description reports. The three new regression tests fail at this head: running `test_graph_query_node.py`, `test_graph_index_utils.py` and `test_semantic_id_query.py` gives 3 failed, each with `ServerError: startup failed: unterminated string literal` raised by `FakeGremlinEndpoint`. With the `hugegraph_llm` sources from 3f7b54c and the tests from this head, all 16 tests pass. Please restore the `gremlin_str_literal(...)` calls in those three files (and the imports), and keep the list-form handling in `FakeGremlinEndpoint` only if a test still needs it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#issuecomment-6358383978
hi @imbajin, gentle ping on this one. bitflicker64 approved it and CI is green. Is there anything else you'd like me to change before it can be merged?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4283068254
##########
hugegraph-llm/src/hugegraph_llm/utils/hugegraph_utils.py:
##########
@@ -34,6 +34,11 @@
BACKUP_DIR = str(os.path.join(resource_path, "backup-graph-data-4020", huge_settings.graph_name))
+def gremlin_str_literal(value) -> str:
Review Comment:
Good catch, thanks! I accidentally committed the main versions of those three files in the last commit. Fixed now, all three call sites use the helper again and the tests pass.
I kept the list handling in the fake endpoint so the node test fails for the right reason on main.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "atharrva01 (via GitHub)" <gi...@apache.org>.
atharrva01 commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4283640546
##########
hugegraph-llm/src/hugegraph_llm/nodes/hugegraph_node/graph_query_node.py:
##########
@@ -355,7 +356,7 @@ def _subgraph_query(self, context: Dict[str, Any]) -> Dict[str, Any]:
if not matched_vids:
return context
- gremlin_query = VERTEX_QUERY_TPL.format(keywords=matched_vids)
+ gremlin_query = VERTEX_QUERY_TPL.format(keywords=",".join(gremlin_str_literal(vid) for vid in matched_vids))
Review Comment:
You're right, the vertex query used to keep these numeric and my change broke that. Fixed in fbd4743: ints now go out as plain numbers (`g.V(123)`), only strings get quoted. Added a test for a numeric id too.
##########
hugegraph-llm/src/hugegraph_llm/utils/hugegraph_utils.py:
##########
@@ -34,6 +34,11 @@
BACKUP_DIR = str(os.path.join(resource_path, "backup-graph-data-4020", huge_settings.graph_name))
+def gremlin_str_literal(value) -> str:
+ """Quote a value as a Groovy single-quoted string literal (no `$` interpolation), e.g. 1:O'Brien -> '1:O\\'Brien'."""
+ return "'" + str(value).replace("\\", "\\\\").replace("'", "\\'") + "'"
Review Comment:
Fixed in fbd4743. Newline, CR, tab, \b and \f now get escaped, and any other control char goes out as \uXXXX. Added tests for these, including one that checks the escaped string decodes back to the original.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org
Re: [PR] fix(llm): escape vertex IDs in Gremlin queries [hugegraph-ai]
Posted by "imbajin (via GitHub)" <gi...@apache.org>.
imbajin commented on code in PR #383:
URL: https://github.com/apache/hugegraph-ai/pull/383#discussion_r4283363582
##########
hugegraph-llm/src/hugegraph_llm/nodes/hugegraph_node/graph_query_node.py:
##########
@@ -355,7 +356,7 @@ def _subgraph_query(self, context: Dict[str, Any]) -> Dict[str, Any]:
if not matched_vids:
return context
- gremlin_query = VERTEX_QUERY_TPL.format(keywords=matched_vids)
+ gremlin_query = VERTEX_QUERY_TPL.format(keywords=",".join(gremlin_str_literal(vid) for vid in matched_vids))
Review Comment:
⚠️ Important. Blocking: yes. Summary: This formats every matched ID through `gremlin_str_literal()`, which calls `str()` and turns numeric vertex IDs into string IDs. `FetchGraphData` obtains IDs from `g.V().id().toList()`, and the semantic index stores and returns those values unchanged, so a numeric ID such as `123` now generates `g.V('123')` instead of `g.V(123)` and can miss the vertex. Please preserve numeric IDs as numeric Gremlin literals and quote/escape only string IDs. Evidence: the exact-head `FetchGraphData`, `BuildSemanticIndex`, and `_fuzzy_match_vids` paths preserve index ID values; HugeGraph maps `Number` and `String` IDs to distinct ID types.
##########
hugegraph-llm/src/hugegraph_llm/utils/hugegraph_utils.py:
##########
@@ -34,6 +34,11 @@
BACKUP_DIR = str(os.path.join(resource_path, "backup-graph-data-4020", huge_settings.graph_name))
+def gremlin_str_literal(value) -> str:
+ """Quote a value as a Groovy single-quoted string literal (no `$` interpolation), e.g. 1:O'Brien -> '1:O\\'Brien'."""
+ return "'" + str(value).replace("\\", "\\\\").replace("'", "\\'") + "'"
Review Comment:
⚠️ Important. Blocking: no. Summary: Raw newline and other control characters are left inside this single-quoted Groovy literal, so a valid string ID containing a line break makes the Gremlin script fail to parse. Please encode Groovy control characters (including newline and carriage return) before quoting and cover them in a regression test. Evidence: this helper only escapes backslashes and apostrophes; Groovy single-quoted literals require escape sequences such as `\n` and `\r` for those characters.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org