#48030·transformers

HfArgumentParser cannot parse dict-typed dataclass fields from the CLI (registers them with type=dict)

Author: sergiopaniegoCreated Aug 17, 2026Updated Sep 17, 2026
Labelsbug

System Info

  • transformers version: 5.15.0
  • Platform: macOS-26.3.1-arm64-arm-64bit-Mach-O
  • Python version: 3.13.5

(also reproduced on transformers 5.14.1; the parsing logic is platform-independent)

Who can help?

No response

Information

  • My own modified scripts

Tasks

  • My own task or dataset (give details below)

Reproduction

from dataclasses import dataclass, field
from transformers import HfArgumentParser

@dataclass
class Cfg:
    kw: dict | None = field(default=None)

parser = HfArgumentParser((Cfg,))
parser.parse_args_into_dataclasses(["--kw", '{"a": false}'])
# argparse error: argument --kw: invalid dict value: '{"a": false}'

Tried JSON (with and without spaces), k=v and k: v formats: all fail. The cause is in _parse_dataclass_field: list fields have a special case (type=<element>, nargs='+'), but dict fields fall through to the generic branch, which registers the argument with type=dict, and dict("<string>") raises.

The same happens for bare dict and dict[str, Any] annotations.

Expected behavior

dict fields parse from the CLI (e.g. as JSON via type=json.loads), mirroring the existing special case for list fields. There is no backward-compatibility concern: passing a dict-typed argument from the CLI always errors today, so the change is purely additive.

Real-world impact: downstream configs expose dict fields that currently cannot be set from example-script flags at all. For example TRL's GRPOConfig.chat_template_kwargs (needed to disable Qwen3's thinking mode) fails with --chat_template_kwargs '{"enable_thinking": false}' because TrlParser inherits from HfArgumentParser.

I am happy to send a PR (small special case in _parse_dataclass_field + test) if this is welcome.

Source: huggingface/transformers