
Outlines Has a Free API: Guarantee Valid JSON Output From Any LLM
LLMs sometimes return invalid JSON. Outlines makes it physically impossible — by constraining token generation to match your schema. What Is Outlines? Outlines is a Python library that constrains LLM text generation using formal grammars. Unlike Instructor (which validates after generation), Outlines constrains DURING generation — the LLM can only produce valid tokens. import outlines model = outlines . models . transformers ( " mistralai/Mistral-7B-v0.3 " ) # JSON schema constraint schema = ''' { " type " : " object " , " properties " : { " name " : { " type " : " string " }, " age " : { " type " : " integer " }, " city " : { " type " : " string " } }, " required " : [ " name " , " age " , " city " ] } ''' generator = outlines . generate . json ( model , schema ) result = generator ( " Extract info: John is 30 and lives in NYC " ) # GUARANTEED valid JSON matching schema # {"name": "John", "age": 30, "city": "NYC"} Regex Constraints # Phone number — only valid phone patterns phone_gen
Continue reading on Dev.to Python
Opens in a new tab


