bitflicker64 opened a new pull request, #402:
URL: https://github.com/apache/hugegraph-ai/pull/402

   ## Purpose
   
   `SchemaValidationError` and `LLMOutputTruncatedError` break when they are 
copied or pickled, for example when an error comes back from a 
`ProcessPoolExecutor` worker. Both build their message in `__init__`, so the 
default `BaseException.__reduce__` calls the constructor again with that 
message as its argument:
   
   - `SchemaValidationError`: `copy.copy`, `copy.deepcopy` and `pickle` all 
raise `AttributeError: 'str' object has no attribute 'path'`, because the 
message string is read as the list of issues.
   - `LLMOutputTruncatedError`: the copy's message is wrapped twice, as `LLM 
output was truncated (LLM output was truncated (finish_reason=length).).`
   
   ## Main changes
   
   - Add `__reduce__` to both classes. It rebuilds the error from `issues` or 
`reason` and passes `__dict__` as state, the same three-part shape 
`BaseException.__reduce__` returns, so attributes and `__notes__` survive. 
`str()`, `repr()`, `args` and the constructors do not change. Traceback, cause 
and context are dropped on copy, as for any built-in exception.
   - `tests/test_errors.py`: round-trips both errors through `copy.copy`, 
`copy.deepcopy`, pickle protocol 0 and the highest pickle protocol.
   - `tests/test_graph_utils.py`: direct cases for `primary_key_id` escaping of 
`!` and `:` with text keys, and the cases that return `None`. Until now only 
hugegraph-llm tested this.
   - `tests/test_diagnostics.py`: `token_usage` keeps non-negative integer 
counters only, fills in a missing total, and accepts a mapping or an object.
   
   The new tests import only the installed package, so they also run in the CI 
job that tests the built wheel without extras.
   
   ## Verification
   
   Run on Linux with Python 3.14 through uv 0.13.5, at commit `1a8ff1d` on top 
of `apache/main` `a90b1c4`:
   
   ```bash
   uv sync --all-extras
   uv run --extra dev ruff format --check .      # 584 files already formatted
   uv run --extra dev ruff check .               # All checks passed!
   uv run --extra dev ty check hugegraph-llm-sdk/src/hugegraph_llm_sdk   # 9 
diagnostics, the same 9 as on main
   uv run pytest hugegraph-llm-sdk/tests -q
   ```
   
   | Run | Result |
   |---|---|
   | `main` | 673 passed, 19 skipped |
   | This PR | 713 collected: 694 passed, 19 skipped |
   | This PR's tests with `errors.py` reverted | 8 failed (all of 
`test_errors.py`), 686 passed |
   
   The 19 skips are the LiteLLM adapter tests, which need the `litellm` extra. 
The `hugegraph-llm` unit split from `hugegraph-llm/AGENTS.md` (with 
`SKIP_EXTERNAL_SERVICES=true`) gives the same result on `main` and on this 
branch: 519 passed, 14 failed. All 14 failures are in `test_config.py` and 
`test_nltk_helper.py` and come from NLTK corpora that CI downloads and this 
machine does not have. `hugegraph-llm` re-exports `LLMOutputTruncatedError` and 
only raises it and catches it by type.
   
   ## Does this PR potentially affect the following parts?
   
   - [ ] Dependencies
   - [ ] Modify configurations
   - [ ] The public API
   - [ ] Other affects
   - [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]

Reply via email to