-
Notifications
You must be signed in to change notification settings - Fork 82
Closed
Description
I need to enforce output to be a valid json object, so I used the json schema {"type": "object"}
, but this fails in xgrammar.
To reproduce:
import xgrammar as xgr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
device = "cuda" # Or "cpu", etc.
model_name = "meta-llama/Llama-3.2-1B-Instruct"
model = AutoModelForCausalLM.from_pretrained(
model_name, torch_dtype=torch.float32, device_map=device
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
config = AutoConfig.from_pretrained(model_name)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Introduce yourself in JSON briefly."},
]
texts = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
model_inputs = tokenizer(texts, return_tensors="pt").to(model.device)
tokenizer_info = xgr.TokenizerInfo.from_huggingface(tokenizer, vocab_size=config.vocab_size)
grammar_compiler = xgr.GrammarCompiler(tokenizer_info)
compiled_grammar = grammar_compiler.compile_json_schema('{"type": "object"}')
Results:
File "/home/rbryant/vllm/xgr_test.py", line 23, in <module>
compiled_grammar = grammar_compiler.compile_json_schema('{"type": "object"}')
File "/home/rbryant/vllm/venv/lib/python3.10/site-packages/xgrammar/compiler.py", line 101, in compile_json_schema
self._handle.compile_json_schema(
RuntimeError: [13:54:19] /project/cpp/grammar_parser.cc:80: EBNF parse error at line 11, column 1: The root rule with name "root" is not found.
Metadata
Metadata
Assignees
Labels
No labels