oobabooga/textgen · warning · RuntimeError

expecting name at {src}

Error message

expecting name at {src}

What it means

Raised by parse_name (modules/grammar/grammar_utils.py:99) when the GBNF parser expects a rule name (at the start of a rule, or after '(' grouping / '*' repetition context) but the next characters contain no word characters ([alnum-_]). This is a grammar syntax error, not a generation-time error.

Source

Thrown at modules/grammar/grammar_utils.py:99

    """
    pos = 0
    while pos < len(src) and (src[pos].isspace() or src[pos] == "#"):
        if src[pos] == "#":
            while pos < len(src) and src[pos] not in ("\r", "\n"):
                pos += 1
        else:
            if not newline_ok and src[pos] in ("\r", "\n"):
                break
            pos += 1
    return src[pos:]


def parse_name(src):
    pos = 0
    while pos < len(src) and is_word_char(src[pos]):
        pos += 1
    if pos == 0:
        raise RuntimeError("expecting name at " + src)
    return src[:pos], src[pos:]


def read_hex(s):
    val = 0
    for c in s:
        val = (val << 4) + hex_to_int(c)
    return chr(val)


def parse_char(src):
    """
    parse the leading char from the input string
    :param src:
    :return: char, remaining_src
    """

    # if we have a backslash, it's maybe an escape

View on GitHub (pinned to ed888c71f2)

Solutions

  1. Look at the src snippet echoed in the message — it shows the exact text where a name was expected; fix or delete it.
  2. Ensure every rule has the form 'name ::= alternates' and rules are separated by newlines.
  3. Validate the grammar in isolation (e.g. via llama.cpp's grammar compiler or a small python driver around modules/grammar) before passing it to generation.

Example fix

// before
root ::= "a"
::= "b"

// after
root ::= "a"
alt ::= "b"
Defensive patterns

Strategy: validation

Validate before calling

import re

RULE_LINE = re.compile(r'^\s*[A-Za-z0-9_-]+\s*::=')

def rule_lines_have_names(grammar: str) -> list[int]:
    return [i for i, line in enumerate(grammar.splitlines(), 1)
            if line.strip() and not line.lstrip().startswith('#')
            and not RULE_LINE.match(line)]

Type guard

def looks_like_gbnf(grammar: str) -> bool:
    lines = [l for l in grammar.splitlines() if l.strip() and not l.lstrip().startswith('#')]
    return bool(lines) and all(RULE_LINE.match(l) for l in lines)

Try / catch

try:
    parse_grammar(grammar_text)
except RuntimeError as e:
    # message echoes the offending src; surface it to the grammar author
    raise ValueError(f'GBNF syntax error: {e}') from None

Prevention

When it happens

Trigger: A grammar rule line starting with '::=' (name missing), e.g. '::= "a"'; two consecutive operators like 'a* *'; an empty rule ' ::= ...'; leftover junk where the parser expects the next rule name after a newline.

Common situations: Hand-edited grammars where a rule name was deleted; missing newline separating rules so one rule's tail is parsed as the next rule's name; comment syntax misuse (comments need '#', not '//'); CRLF/stray characters corrupting rule starts.

Related errors


AI-assisted analysis of oobabooga/textgen@ed888c71f2 (2026-08-15). Data as JSON: /api/errors/9ce2a44810381ddb. Report an issue: GitHub.