PDGGK opened a new pull request, #420:
URL: https://github.com/apache/hugegraph-ai/pull/420
## Purpose of the PR
The Python client cannot read, update or delete a vertex whose string id
contains non-ASCII text. With a `person` label keyed by `name`,
`graph.add_vertex("person", {"name": "张三"})` works and returns the id `1:张三`,
but `get_vertex_by_id`, `append_vertex`, `eliminate_vertex` and
`remove_vertex_by_id` on that id all fail with `requests.exceptions.RetryError:
... too many 500 error responses`. ASCII ids work. The same happens with a
customized string id such as `文档-1`, and with the released `hugegraph-python`
1.7.1.
`format_vertex_id()` builds the JSON literal for the id with the
`json.dumps()` defaults, which escape every non-ASCII character as `\uXXXX`. In
the URL path the backslash becomes `%5C`, so the request goes to
`/graph/vertices/%221%3A%5Cu5f20%5Cu4e09%22`, and the server answers HTTP 500
with an empty body for a path that contains an encoded backslash. The same id
percent-encoded as UTF-8, `%221%3A%E5%BC%A0%E4%B8%89%22`, returns the vertex.
The MCP tools that read vertices by id go through the same function. This is
the case flagged in the #329 review for `json.dumps` without
`ensure_ascii=False`.
## Main Changes
`format_vertex_id()` now passes `ensure_ascii=False`. The id keeps its
characters, and `RestResource.request()` percent-encodes them as UTF-8 in the
path and in query parameters. ASCII, numeric and UUID ids produce the same
string as before, and ids with quotes or backslashes are still escaped by JSON
as before.
Tests: `test_format_vertex_id_matches_hugegraph_json_literal_contract` gets
a `1:张三` case, and `test_graph_vertex_path_sends_non_ascii_ids_as_utf8` checks
the URLs that get, append, remove and `get_vertices_by_id` send for that id.
The integration test `test_vertex_by_id_with_non_ascii_id` creates a vertex
with a Chinese primary key, then reads, appends to and removes it on a live
server.
## Verifying these changes
- [ ] Trivial rework / code cleanup without any test coverage. (No Need)
- [ ] Already covered by existing tests, such as *(please modify tests
here)*.
- [x] Need tests and can be verified as follows:
- `uv run pytest hugegraph-python-client/src/tests -m "unit or
contract"`: 167 passed, 3 skipped.
- `uv run pytest hugegraph-python-client/src/tests -m "integration and
hugegraph"`: 71 passed against `hugegraph/hugegraph:latest` (server 1.8.0) and
against `hugegraph/hugegraph:1.7.0`.
- With only `id_format.py` reverted, the three new tests fail: the URL
assertion gets `graph/vertices/%221%3A%5Cu5f20%5Cu4e09%22`, the format test
gets the escaped string, and the integration test gets the `RetryError` above.
- A script that calls the four vertex methods for `1:张三`, `文档-1` and
`doc-1` fails 8 of 12 calls on `main` (all non-ASCII ones) and none with this
change, on both server versions.
- `ruff format --check .`, `ruff check .`, `ty check
hugegraph-python-client/src/pyhugegraph` and `python3 scripts/client_api.py
--check` pass. The `hugegraph-mcp` selection from its CI job passes (904
passed), as it does on `main`.
I ran everything on Python 3.12 with uv 0.13.6, not on 3.13 or 3.14. I did
not run the `hugegraph-llm` suites, since nothing in `hugegraph-llm` calls
these vertex methods or `format_vertex_id()`. Ids containing `/` still fail on
the server, which also rejects an encoded slash in the path. That is unchanged
here.
## Does this PR potentially affect the following parts?
- [ ] Dependencies
- [ ] Modify configurations
- [ ] The public API
- [ ] Other affects (typed here)
- [x] Nope
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]