Fix HuggingfaceEngine input kwargs defaults#10590
Conversation
There was a problem hiding this comment.
Code Review
This pull request correctly replaces mutable default dictionary arguments (input_kwargs) with None in several methods of HuggingfaceEngine to avoid shared state issues, and adds an AST-based test to enforce this. The reviewer feedback is highly valuable, pointing out that to prevent mutating the caller's dictionary (especially where .pop() is used), it is safer to create a shallow copy of the dictionary using dict(input_kwargs) instead of referencing it directly.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| input_kwargs: Optional[dict[str, Any]] = {}, | ||
| input_kwargs: Optional[dict[str, Any]] = None, | ||
| ) -> tuple[dict[str, Any], int]: | ||
| input_kwargs = {} if input_kwargs is None else input_kwargs |
There was a problem hiding this comment.
To prevent mutating the dictionary passed by the caller (since .pop() is called on input_kwargs later in this method), it is safer to create a copy of the dictionary instead of using it directly.
| input_kwargs = {} if input_kwargs is None else input_kwargs | |
| input_kwargs = {} if input_kwargs is None else dict(input_kwargs) |
| input_kwargs: Optional[dict[str, Any]] = {}, | ||
| input_kwargs: Optional[dict[str, Any]] = None, | ||
| ) -> list["Response"]: | ||
| input_kwargs = {} if input_kwargs is None else input_kwargs |
There was a problem hiding this comment.
| input_kwargs: Optional[dict[str, Any]] = {}, | ||
| input_kwargs: Optional[dict[str, Any]] = None, | ||
| ) -> Callable[[], str]: | ||
| input_kwargs = {} if input_kwargs is None else input_kwargs |
There was a problem hiding this comment.
| input_kwargs: Optional[dict[str, Any]] = {}, | ||
| input_kwargs: Optional[dict[str, Any]] = None, | ||
| ) -> list[float]: | ||
| input_kwargs = {} if input_kwargs is None else input_kwargs |
There was a problem hiding this comment.
To prevent mutating the dictionary passed by the caller (since .pop() is called on input_kwargs later in this method), it is safer to create a copy of the dictionary instead of using it directly.
| input_kwargs = {} if input_kwargs is None else input_kwargs | |
| input_kwargs = {} if input_kwargs is None else dict(input_kwargs) |
|
Updated the HF engine paths to copy input_kwargs before any local mutation, so caller-owned dictionaries are left unchanged. I also added an AST regression check for that pattern alongside the mutable-default check. |
Summary
Fixes the mutable default
input_kwargs={}arguments in the HuggingFace chat engine helpers.The affected helpers call
.pop()oninput_kwargs, so sharing the same default dictionary can leak state across calls that omit the argument. This changes those defaults toNoneand creates a fresh empty dictionary inside each method only when no kwargs are provided.The explicit
input_kwargsbehavior is unchanged for callers that pass a dictionary.Fixes #10476
Tests
.venv\Scripts\python.exe -m ruff check src\llamafactory\chat\hf_engine.py tests\chat\test_hf_engine.py.venv\Scripts\python.exe -m pytest --noconftest tests\chat\test_hf_engine.pygit diff --check