Expand description
JSON mode logits processor.
Constrains generation to produce valid JSON by tracking a state machine and masking tokens that would produce invalid syntax at each step.
§Approach
Rather than full grammar-guided generation (which requires tokenizer-level mapping), this processor uses a lightweight state machine that tracks whether we’re inside a string, after a key, expecting a value, etc. It biases logits to favor JSON-structural tokens without fully preventing all invalid outputs.
For a production-quality implementation, this would need:
- Tokenizer integration to map token IDs to byte sequences
- Full JSON grammar with recursive descent validation
- Efficient bitset masking over the vocabulary
This MVP provides the infrastructure and demonstrates the pattern.
Structs§
- Json
Mode Processor - JSON mode logits processor.
Enums§
- Json
State - Tracks the current position in JSON structure.