█

AI/TLDR

मुझे पिलाएं कॉफ़ी
मुझे एक कॉफ़ी पिलाएँ

XGrammar

Grammar-constrained decoding, जो LLM के आउटपुट की structure को हमेशा सही रखने की गारंटी देती है

Structured Outputओपन सोर्स
भाषा
Python
$pip install xgrammar
संक्षेप में

XGrammar क्या है?

XGrammar Grammar-constrained decoding, जो LLM के आउटपुट की structure को हमेशा सही रखने की गारंटी देती है. यह App Orchestration टूल्स की Structured Output श्रेणी में आता है; ओपन सोर्स है और Python में लिखा गया है। शुरू करने के लिए: xGrammar इंस्टॉल करें.

सारांश

XGrammar, large language models से structured generation के लिए एक ओपन सोर्स library है। यह constrained decoding का इस्तेमाल करती है, ताकि मॉडल का आउटपुट दिए गए structure का पालन करे और परिणाम हमेशा उस format के हिसाब से valid रहे जो आप माँगते हैं। यह JSON, regex और general context-free grammars को सपोर्ट करती है।

यह उन developers और teams के लिए बनाई गई है जो LLM inference चलाते हैं और ऐसे आउटपुट चाहते हैं जिन्हें वे parse कर सकें और भरोसा कर सकें, जैसे tool calls के लिए JSON या fixed schemas। यह मॉडल जो tokens emit कर सकता है उन्हें सीमित करती है, इसलिए structure बाद में ठीक करने के बजाय शुरू से ही सही बनता है। साथ ही, इसे JSON generation में बहुत कम overhead जोड़ने के लिए डिज़ाइन किया गया है।

LLM orchestration की दुनिया में XGrammar structured-output layer पर काम करती है। यह vLLM, SGLang, TensorRT-LLM और MLC-LLM समेत कई inference engines का डिफ़ॉल्ट structured generation backend है, और Linux, macOS व Windows पर CPU और GPU hardware दोनों पर चलती है।

यह क्या करता है

  • Constrained decoding, जो 100% structurally correct आउटपुट सुनिश्चित करता है
  • custom structures के लिए JSON, regex और general context-free grammars को सपोर्ट करता है
  • कम overhead, JSON generation के लिए लगभग शून्य overhead की रिपोर्ट के साथ
  • Linux, macOS और Windows पर CPU, NVIDIA/AMD GPU, Apple Silicon और TPU पर universal deployment
  • Python, C++, JavaScript और Swift APIs
  • vLLM, SGLang, TensorRT-LLM और MLC-LLM में डिफ़ॉल्ट structured generation backend

शुरू करें

pip से XGrammar इंस्टॉल करें और इसे import करें, फिर पूरे quickstart उपयोग के लिए आधिकारिक docs देखें।

XGrammar इंस्टॉल करें

PyPI से package इंस्टॉल करें।

bashbash
pip install xgrammar

Apple Silicon के लिए इंस्टॉल करें (वैकल्पिक)

Apple Silicon पर MPS के साथ उपयोग के लिए metal extra इंस्टॉल करें।

bashbash
pip install "xgrammar[metal]"

XGrammar import करें

Python में library import करें; पूरे quick start के लिए आधिकारिक documentation देखें।

pythonpython
import xgrammar as xgr

कमांड और कोड प्रोजेक्ट के अपने दस्तावेज़ों से लिए गए हैं — हमेशा जांचें आधिकारिक repo ताज़ा जानकारी के लिए।

कब इस्तेमाल करें

  • Tool calling या API responses के लिए LLM को ऐसा valid JSON लौटाने पर मजबूर करना जो schema से मेल खाए
  • किसी domain-specific format के लिए आउटपुट को custom context-free grammar या regex तक सीमित करना
  • vLLM या SGLang जैसे inference server में structured generation जोड़ना
  • post-hoc validation और retries के बिना भरोसेमंद तरीके से parse होने लायक आउटपुट बनाना

How XGrammar compares

XGrammar alongside other open-source structured output tools AI/TLDR tracks, ranked by GitHub stars.

टूलस्टारयह क्या करता है
Laya★ 36.1kटेक्स्ट के बारे में टाइप किए गए सवाल पूछें और कैलिब्रेटेड संभावनाएँ पाएँ, जनरेट किया हुआ JSON नहीं
Guidance★ 21.8kConstrained, structured generation के साथ LLM को नियंत्रित करने वाला एक programming model
Outlines★ 16kकिसी भी LLM को हमेशा valid structured output देने के लिए बाध्य करें
Instructor★ 14.2kकिसी भी LLM से भरोसेमंद structured आउटपुट, Pydantic से validated
BAML★ 9.6kएक prompting language जो LLM prompts को typed functions में बदलती है और भरोसेमंद structured output देती है
Marvin★ 6.2kLLM calls को typed Python functions और agentic workflows में बदलें
SemIf★ 5kकिसी ओपन मॉडल से JSON जनरेट कराने के बजाय, टाइप किए गए विकल्पों की संभावनाएँ सीधे पढ़ें
XGrammar★ 2kGrammar-constrained decoding, जो LLM के आउटपुट की structure को हमेशा सही रखने की गारंटी देती है