huggingface/transformers · error · TypeError

Expected config to be an OnnxConfig or dict, got {type(confi

Error message

Expected config to be an OnnxConfig or dict, got {type(config)}

What it means

OnnxExporter.export accepts config only as an OnnxConfig instance or a plain dict of its fields (converted via OnnxConfig(**config)); the check is an exact-type check, so subclasses of OnnxConfig are rejected too. Anything else raises this TypeError before the ONNX translation pipeline starts.

Source

Thrown at src/transformers/exporters/exporter_onnx.py:114

    >>> onnx_program = exporter.export(model, inputs, config=OnnxConfig(dynamic=True))
    >>> outputs = onnx_program(**inputs)  # run in-memory
    >>> exporter.export(model, inputs, config=OnnxConfig(output_path="model.onnx"))  # save to disk
    ```
    """

    required_packages = ["torch", "onnx", "onnxscript"]
    tested_versions = {"torch": "2.12.0", "onnx": "1.21.0", "onnxscript": "0.7.0"}

    def export(
        self,
        model: PreTrainedModel,
        sample_inputs: MutableMapping[str, Any],
        config: OnnxConfig | dict[str, Any],
    ) -> ONNXProgram:
        if isinstance(config, dict):
            config = OnnxConfig(**config)
        elif type(config) is not OnnxConfig:
            raise TypeError(f"Expected config to be an OnnxConfig or dict, got {type(config)}")

        with patch_model_outputs(model) as (inputs_names, outputs_names), apply_patches("onnx"):
            exported_program: ExportedProgram = super().export(model, sample_inputs, config=config)
            inputs_names, outputs_names = disambiguate_io_names(inputs_names, outputs_names)
            apply_fx_node_fixes("onnx", exported_program.graph_module)
            onnx_program: ONNXProgram = torch.onnx.export(
                exported_program,
                args=(),
                f=config.output_path,
                input_names=inputs_names,
                output_names=outputs_names,
                kwargs=copy.deepcopy(dict(sample_inputs)),
                custom_translation_table=_ONNX_TRANSLATION_TABLE,
                opset_version=config.opset_version,
                external_data=config.external_data,
                export_params=config.export_params,
                optimize=config.optimize,
            )

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass an OnnxConfig: OnnxExporter().export(model, inputs, config=OnnxConfig(output_path="model.onnx")).
  2. For custom/extra fields pass a dict: config={"output_path": "model.onnx", ...extras}.
  3. If dispatching by format, build the per-format config class (or use AutoHfExporter.from_config).

Example fix

# before
OnnxExporter().export(model, inputs, config=DynamoConfig())  # TypeError

# after
from transformers.exporters.exporter_onnx import OnnxConfig
OnnxExporter().export(model, inputs, config=OnnxConfig(output_path="model.onnx"))
Defensive patterns

Strategy: type-guard

Validate before calling

from transformers.exporters.exporter_onnx import OnnxConfig

if type(config) is not OnnxConfig:
    config = OnnxConfig(**config)
OnnxExporter().export(model, inputs, config)

Type guard

def is_onnx_config_like(cfg) -> bool:
    from transformers.exporters.exporter_onnx import OnnxConfig
    return type(cfg) is OnnxConfig or isinstance(cfg, dict)

Prevention

When it happens

Trigger: Passing DynamoConfig or ExecutorchConfig to OnnxExporter.export; passing a subclass of OnnxConfig with extra fields; passing config=None or a file path string.

Common situations: A multi-backend export loop forwarding one config to all exporters; subclassing OnnxConfig to add fields (must use a dict instead); swapping exporters in existing code without swapping the config.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/804c2bb4ada74850. Report an issue: GitHub.