Skip to content

Bug: Unable to load grammar from json.gbnf example #7991

Closed
@vecorro

Description

@vecorro

What happened?

I have tried to load the json.gbnf grammar example but haven't been able to do so. The following code is not working.

from llama_cpp.llama import Llama, LlamaGrammar
import httpx
grammar_text = httpx.get("https://raw.githubusercontent.com/ggerganov/llama.cpp/master/grammars/json.gbnf").text
grammar = LlamaGrammar.from_string(grammar_text)

This throws the following error:

ValueError: from_string: error parsing grammar file: parsed_grammar.rules is empty

I'm not sure if the problem resides in the grammar definition file or in the LlamaGrammar class. The problem shows up when I use the .from_file method as well.

Name and Version

Ubuntu 22.04
Python 3.11 (Anaconda)
llama_cpp_python 0.2.78

What operating system are you seeing the problem on?

Linux

Relevant log output

parse: error parsing grammar: expecting ')' at {4}) # escapes
  )* "\"" ws

number ::= ("-"? ([0-9] | [1-9] [0-9]{0,15})) ("." [0-9]+)? ([eE] [-+]? [0-9] [1-9]{0,15})? ws

# Optional space: by convention, applied in this grammar after literal chars when allowed
ws ::= | " " | "\n" [ \t]{0,20}


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[14], line 4
      2 import httpx
      3 grammar_text = httpx.get("https://raw.githubusercontent.com/ggerganov/llama.cpp/master/grammars/json.gbnf").text
----> 4 grammar = LlamaGrammar.from_string(grammar_text)

File ~/miniconda3/envs/llama-cpp/lib/python3.11/site-packages/llama_cpp/llama_grammar.py:71, in LlamaGrammar.from_string(cls, grammar, verbose)
     69 parsed_grammar = parse(const_char_p(grammar))  # type: parse_state
     70 if parsed_grammar.rules.empty():
---> 71     raise ValueError(
     72         f"{cls.from_string.__name__}: error parsing grammar file: parsed_grammar.rules is empty"
     73     )
     74 if verbose:
     75     print(f"{cls.from_string.__name__} grammar:", file=sys.stderr)

ValueError: from_string: error parsing grammar file: parsed_grammar.rules is empty

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug-unconfirmedmedium severityUsed to report medium severity bugs in llama.cpp (e.g. Malfunctioning Features but still useable)stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions