Skip to content

Fix HuggingfaceEngine input kwargs defaults#10590

Draft
lxingy3 wants to merge 2 commits into
hiyouga:mainfrom
lxingy3:fix-hf-engine-input-kwargs-default
Draft

Fix HuggingfaceEngine input kwargs defaults#10590
lxingy3 wants to merge 2 commits into
hiyouga:mainfrom
lxingy3:fix-hf-engine-input-kwargs-default

Conversation

@lxingy3

@lxingy3 lxingy3 commented Jun 17, 2026

Copy link
Copy Markdown

Summary

Fixes the mutable default input_kwargs={} arguments in the HuggingFace chat engine helpers.

The affected helpers call .pop() on input_kwargs, so sharing the same default dictionary can leak state across calls that omit the argument. This changes those defaults to None and creates a fresh empty dictionary inside each method only when no kwargs are provided.

The explicit input_kwargs behavior 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.py
  • git diff --check

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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.

Comment thread src/llamafactory/chat/hf_engine.py Outdated
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

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

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.

Suggested change
input_kwargs = {} if input_kwargs is None else input_kwargs
input_kwargs = {} if input_kwargs is None else dict(input_kwargs)

Comment thread src/llamafactory/chat/hf_engine.py Outdated
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

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

To prevent mutating the dictionary passed by the caller, it is safer to create a copy of the dictionary instead of using it directly.

Suggested change
input_kwargs = {} if input_kwargs is None else input_kwargs
input_kwargs = {} if input_kwargs is None else dict(input_kwargs)

Comment thread src/llamafactory/chat/hf_engine.py Outdated
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

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

To prevent mutating the dictionary passed by the caller, it is safer to create a copy of the dictionary instead of using it directly.

Suggested change
input_kwargs = {} if input_kwargs is None else input_kwargs
input_kwargs = {} if input_kwargs is None else dict(input_kwargs)

Comment thread src/llamafactory/chat/hf_engine.py Outdated
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

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

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.

Suggested change
input_kwargs = {} if input_kwargs is None else input_kwargs
input_kwargs = {} if input_kwargs is None else dict(input_kwargs)

@lxingy3

lxingy3 commented Jun 17, 2026

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: mutable default argument input_kwargs={} in HuggingfaceEngine static methods

1 participant