huggingface/transformers · warning · ValueError

Failed to convert `generate_flags` into a valid JSON object.

Error message

Failed to convert `generate_flags` into a valid JSON object.
`generate_flags` = {generate_flags}
Converted JSON string = {generate_flags_string}

What it means

parse_generate_flags converts chat-CLI flags like `/generate temperature=0.5 top_k=20` into a dict by assembling a JSON string and calling json.loads. If the constructed string is not valid JSON (bad quoting, missing '=', unquoted strings with special characters, malformed lists), a JSONDecodeError is caught and re-raised as ValueError. Note: the message text itself is buggy — it lacks the f-prefix, so it prints literal {generate_flags} placeholders instead of values.

Source

Thrown at src/transformers/cli/chat.py:652

    generate_flags_string = ", ".join([f"{k}: {v}" for k, v in generate_flags_as_dict.items()])

    # 4. Add the opening/closing brackets
    generate_flags_string = "{" + generate_flags_string + "}"

    # 5. Remove quotes around boolean/null and around lists
    generate_flags_string = generate_flags_string.replace('"null"', "null")
    generate_flags_string = generate_flags_string.replace('"true"', "true")
    generate_flags_string = generate_flags_string.replace('"false"', "false")
    generate_flags_string = generate_flags_string.replace('"[', "[")
    generate_flags_string = generate_flags_string.replace(']"', "]")

    # 6. Replace the `=` with `:`
    generate_flags_string = generate_flags_string.replace("=", ":")

    try:
        processed_generate_flags = json.loads(generate_flags_string)
    except json.JSONDecodeError:
        raise ValueError(
            "Failed to convert `generate_flags` into a valid JSON object."
            "\n`generate_flags` = {generate_flags}"
            "\nConverted JSON string = {generate_flags_string}"
        )
    return processed_generate_flags


def new_chat_history(system_prompt: str | None = None) -> list[dict]:
    """Returns a new chat conversation."""
    return [{"role": "system", "content": system_prompt}] if system_prompt else []


def save_chat(filename: str, chat: list[dict], settings: dict) -> str:
    """Saves the chat history to a file."""
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    with open(filename, "w") as f:
        json.dump({"settings": settings, "chat_history": chat}, f, indent=4)
    return os.path.abspath(filename)

View on GitHub (pinned to a597f97485)

Solutions

  1. Use simple key=value pairs with numeric, boolean, or null values: /generate temperature=0.7 do_sample=true
  2. Write lists as int lists: /generate suppress_tokens=[1,2,3]
  3. Quote nothing manually — the CLI adds quotes; avoid commas/braces/quotes inside string values
  4. Ensure every flag has the form name=value with no spaces around '='

Example fix

# before
/generate stop="," temperature=0.5   # comma breaks JSON assembly

# after
/generate temperature=0.5 do_sample=true
Defensive patterns

Strategy: validation

Validate before calling

import re

def flags_parseable(flags: list[str]) -> bool:
    for f in flags:
        parts = f.split("=")
        if len(parts) != 2 or not parts[0] or not parts[1]:
            return False
        if any(ch in parts[1] for ch in ",{}\""):
            return False
    return True

Type guard

def is_simple_flag(flag: str) -> bool:
    name, _, value = flag.partition("=")
    ok_value = value.replace(".", "", 1).removeprefix("-").isdigit() or value.lower() in {
        "true", "false", "none"
    } or re.fullmatch(r"\[[0-9,\s]*\]", value)
    return bool(name) and ok_value

Prevention

When it happens

Trigger: Using a flag without '=' (e.g. /generate temperature); passing a value containing a comma or brace that breaks the hand-rolled JSON assembly; a string value with an embedded quote; a list not written as [1,2,3] of ints (lists of strings are explicitly unsupported); flags whose value is empty.

Common situations: Users typing free-form generation kwargs in the chat UI; passing string values like stop=","; lists of strings (unsupported per the help text); flag split on '=' producing more than 2 parts (value containing '=').

Related errors


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