vllm_mlx.constrained.json_schema_processor¶
JSONSchemaLogitsProcessor — a mlx_lm-compatible logits processor that masks the vocabulary so the model can only emit tokens forming a valid JSON value (optionally matching a JSON schema).
View the complete module source at #L1-L924.
API details¶
Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.
vllm_mlx.constrained.json_schema_processor
¶
JSONSchemaLogitsProcessor — a mlx_lm-compatible logits processor that
masks the vocabulary so the model can only emit tokens forming a valid JSON
value (optionally matching a JSON schema).
The processor implements the signature expected by mlx_lm.generate.generate_step
and vllm_mlx's batched engine alike:
processor(tokens: mx.array, logits: mx.array) -> mx.array
tokens contains the full sequence generated for this request so far
(prompt + previously emitted tokens), and logits is the last-step logits
row.
vllm_mlx.constrained.json_schema_processor.logger
module-attribute
¶
vllm_mlx.constrained.json_schema_processor._parser_cache
module-attribute
¶
vllm_mlx.constrained.json_schema_processor._MAX_NONPROGRESS_WHITESPACE_CHARS
module-attribute
¶
vllm_mlx.constrained.json_schema_processor._JSON_WHITESPACE
module-attribute
¶
vllm_mlx.constrained.json_schema_processor._GENERIC_JSON_SCHEMA
module-attribute
¶
vllm_mlx.constrained.json_schema_processor.LMFormatEnforcerNotAvailableError
¶
Bases: RuntimeError
Raised when lm-format-enforcer is required but not installed.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor
¶
Logits processor that constrains generation to valid JSON.
Parameters¶
schema:
The JSON Schema the output must match. When None, any valid JSON
object/array is accepted (json_object mode).
tokenizer:
The tokenizer used for generation. Its vocabulary is iterated once
(via :mod:vllm_mlx.constrained.cache) and cached for subsequent
requests.
Source code in vllm_mlx/constrained/json_schema_processor.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._tokenizer
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._schema
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._tok_data
instance-attribute
¶
_tok_data = get_tokenizer_data(tokenizer)
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._disabled
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._enforcer
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._parser
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._prompt_len
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._vocab_size
instance-attribute
¶
_vocab_size: int = _get_vocab_size(tokenizer)
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._eos_set
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._valid_key_first_chars
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._valid_key_names
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._token_decode_cache
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._cached_suffix_text
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._cached_suffix_len
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._json_ctx_in_string
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._json_ctx_last_quote_pos
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._json_ctx_scanned_len
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._brace_depth
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._bracket_depth
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._container_stack
instance-attribute
¶
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.schema
property
¶
Return the normalized JSON Schema enforced for this request.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.vocab_size
property
¶
Return the tokenizer vocabulary size used to construct masks.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._suffix
¶
Return the slice of tokens that corresponds to generated output.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._decode_token_cached
¶
Return the decoded text for a single token (cached).
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._decode_suffix
¶
Decode suffix tokens to text.
Always uses full tokenizer.decode(suffix) which is correct for
all tokenizer families (BPE, SentencePiece, etc.). Per-token
concatenation is NOT safe because whitespace may be encoded as a
token prefix (e.g. decode([1526]) = "world" but in context
decode([22557, 1526]) = "Hello world").
Results are cached by suffix length to avoid redundant decodes
within the same generation step (_get_json_context and
_suffix_is_complete_json both call this method).
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._suffix_is_complete_json
¶
Return True if the decoded suffix parses as a complete JSON value.
Uses cached bracket/brace depth from _get_json_context as a
fast pre-check: JSON cannot be complete when brackets are
unbalanced or we are inside a string. This avoids the expensive
json.loads call on ~99% of steps.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._get_json_context
¶
Determine the JSON structural context of the current suffix.
Processes only newly appended characters instead of re-scanning the full decoded text on every call (O(1) amortised per step instead of O(n)).
Returns one of:
- "key_start": expecting a new key (after { or ,)
- "in_key": inside an open key string
- "other": any other position
Source code in vllm_mlx/constrained/json_schema_processor.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 | |
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_at_key_context
¶
Apply schema-aware filtering when in key-related context.
At key_start: only allow tokens that begin a valid key, whitespace,
}, or just ".
At in_key: only allow tokens compatible with continuing a valid
property name (no leading whitespace; content must be a valid prefix).
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_key_start_tokens
¶
Filter tokens at key-start position.
Only permit tokens that:
- Are whitespace-only (before the key ")
- Decode to } (close object)
- Start a valid key: " followed by a valid first char
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_in_key_tokens
¶
Filter tokens when we're inside an open key string.
Only allow tokens whose content continues a valid property name. Reject whitespace-only/leading-whitespace tokens.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._is_valid_key_prefix
¶
Return True if prefix is a prefix of at least one valid key name.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_nonprogress_whitespace_tokens
¶
Stop constrained JSON from spending a long run on pure whitespace.
JSON permits arbitrary whitespace around structural tokens. That is valid, but with non-streaming requests a model can keep selecting whitespace-only tokens for minutes without producing useful JSON content. Once the decoded suffix has a long trailing whitespace run outside a string, remove pure-whitespace tokens from the next-step allowed set so generation must make structural/content progress.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._build_allow_mask
¶
Build a 1-D mask of length vocab_size where allowed positions are
0 and disallowed positions are -inf.
Uses numpy for mask construction (C-level speed) instead of a
Python loop over vocab_size elements.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.__call__
¶
Apply the allowed-tokens mask to logits.
Source code in vllm_mlx/constrained/json_schema_processor.py
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 | |
vllm_mlx.constrained.json_schema_processor._canonical_schema_key
¶
vllm_mlx.constrained.json_schema_processor._get_or_build_parser
¶
Return (parser_schema, JsonSchemaParser) for schema, memoised.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor.is_available
¶
Return True iff lm-format-enforcer is importable.
vllm_mlx.constrained.json_schema_processor._simplify_schema
¶
Pre-process a JSON Schema for lm-format-enforcer compatibility.
lm-format-enforcer does not support $ref, not, type as an
array, or recursive definitions. This function:
- Resolves
$refby inlining referenced definitions (with cycle detection so recursive definitions are truncated to{}). - Removes
notsub-schemas (makes the schema more permissive). - Strips metadata / serialisation-hint keywords that the enforcer does
not understand:
default,examples,title,description,$schema,$id. - Converts
type: [t1, t2, ...]toanyOf: [{type: t1}, ...]. - Cleans up empty
anyOf/oneOfbranches. - Flattens nested
anyOf/oneOf(e.g.anyOf: [{anyOf: [A, B]}, C]→anyOf: [A, B, C]).
Source code in vllm_mlx/constrained/json_schema_processor.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
vllm_mlx.constrained.json_schema_processor._force_no_additional_properties
¶
Return a deep copy of schema with additionalProperties: false
injected into every object-type sub-schema that declares properties.
lm-format-enforcer has a bug where multi-character tokens spanning
JSON structural boundaries (e.g., a single token that decodes to "")
can produce empty or whitespace-only keys, causing KeyError crashes in
jsonschemaparser.py. Setting additionalProperties: false tells the
enforcer's trie traversal that only the declared property names are valid
keys, which significantly narrows the allowed tokens and prevents most of
these boundary-spanning issues.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor._inject_no_additional_props
¶
Recursively inject additionalProperties: false into node.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor._collect_property_names
¶
Collect all property names declared anywhere in schema.
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor._walk_properties
¶
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor._complete_json_eos_logits
¶
_complete_json_eos_logits(eos_set: set[int], suffix: list[int], logits: array, is_complete_json, build_allow_mask) -> array | None
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor._eos_logits
¶
Source code in vllm_mlx/constrained/json_schema_processor.py
vllm_mlx.constrained.json_schema_processor._eos_logits_or_original
¶
Complete contract reference¶
Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.
vllm_mlx.constrained.json_schema_processor.LMFormatEnforcerNotAvailableError · class
Raised when lm-format-enforcer is required but not installed.
Parameters
This callable has no explicit inputs.
Returns
- Constructs:
vllm_mlx.constrained.json_schema_processor.LMFormatEnforcerNotAvailableError
Exceptions and behavior
Class LMFormatEnforcerNotAvailableError derives from RuntimeError and declares 0 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._canonical_schema_key · function
Function _canonical_schema_key calls json.dumps(schema, sort_keys=True, separators=(',', ':')).encode, json.dumps, hashlib.sha256(blob).hexdigest, hashlib.sha256; has 2 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
schema |
dict \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
'__none__';hashlib.sha256(blob).hexdigest()
Exceptions and behavior
Function _canonical_schema_key calls json.dumps(schema, sort_keys=True, separators=(',', ':')).encode, json.dumps, hashlib.sha256(blob).hexdigest, hashlib.sha256; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._get_or_build_parser · function
vllm_mlx.constrained.json_schema_processor._get_or_build_parser(schema: dict | None) -> tuple[dict, Any]
Return (parser_schema, JsonSchemaParser) for schema, memoised.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
schema |
dict \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[dict, Any] - Direct return expressions:
cached;(parser_schema, parser)
Exceptions and behavior
Function _get_or_build_parser calls _canonical_schema_key, _parser_cache.get, _simplify_schema, _force_no_additional_properties; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.is_available · function
Return True iff lm-format-enforcer is importable.
Parameters
This callable has no explicit inputs.
Returns
- Type:
bool - Direct return expressions:
False;True
Exceptions and behavior
Function is_available has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._simplify_schema · function
Pre-process a JSON Schema for lm-format-enforcer compatibility.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
schema |
dict |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
dict - Direct return expressions:
_resolve(schema)
Exceptions and behavior
Function _simplify_schema calls copy.deepcopy, definitions.update, schema.pop, set; returns _resolve(schema).
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._simplify_schema._resolve · nested function
vllm_mlx.constrained.json_schema_processor._simplify_schema._resolve(node: Any, depth: int = 0) -> Any
Nested Function _simplify_schema._resolve calls isinstance, ref.split, len, resolving.add; has 3 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
node |
Any |
yes |
none |
Required positional or keyword input. |
depth |
int |
no |
0 |
Optional positional or keyword input; defaults to 0. |
Returns
- Type:
Any - Direct return expressions:
node;result;{}
Exceptions and behavior
Nested Function _simplify_schema._resolve calls isinstance, ref.split, len, resolving.add; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._force_no_additional_properties · function
Return a deep copy of schema with additionalProperties: false injected into every object-type sub-schema that declares properties.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
schema |
dict |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
dict - Direct return expressions:
schema
Exceptions and behavior
Function _force_no_additional_properties calls copy.deepcopy, _inject_no_additional_props; returns schema.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._inject_no_additional_props · function
Recursively inject additionalProperties: false into node.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
node |
Any |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Function _inject_no_additional_props calls isinstance, node.values, _inject_no_additional_props; returns None.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._collect_property_names · function
Collect all property names declared anywhere in schema.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
schema |
dict \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
set[str] - Direct return expressions:
names
Exceptions and behavior
Function _collect_property_names calls set, _walk_properties; returns names.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._walk_properties · function
Function _walk_properties calls isinstance, node.get, names.update, props.keys; returns None.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
node |
Any |
yes |
none |
Required positional or keyword input. |
names |
set[str] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Function _walk_properties calls isinstance, node.get, names.update, props.keys; returns None.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._complete_json_eos_logits · function
vllm_mlx.constrained.json_schema_processor._complete_json_eos_logits(eos_set: set[int], suffix: list[int], logits: mx.array, is_complete_json, build_allow_mask) -> mx.array | None
Function _complete_json_eos_logits calls is_complete_json, _eos_logits; has 2 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
eos_set |
set[int] |
yes |
none |
Required positional or keyword input. |
suffix |
list[int] |
yes |
none |
Required positional or keyword input. |
logits |
mx.array |
yes |
none |
Required positional or keyword input. |
is_complete_json |
not annotated |
yes |
none |
Required positional or keyword input. |
build_allow_mask |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array | None - Direct return expressions:
None;_eos_logits(eos_set, logits, build_allow_mask)
Exceptions and behavior
Function _complete_json_eos_logits calls is_complete_json, _eos_logits; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._eos_logits · function
vllm_mlx.constrained.json_schema_processor._eos_logits(eos_set: set[int], logits: mx.array, build_allow_mask) -> mx.array | None
Function _eos_logits calls build_allow_mask, sorted; has 2 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
eos_set |
set[int] |
yes |
none |
Required positional or keyword input. |
logits |
mx.array |
yes |
none |
Required positional or keyword input. |
build_allow_mask |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array | None - Direct return expressions:
None;logits + mask
Exceptions and behavior
Function _eos_logits calls build_allow_mask, sorted; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor._eos_logits_or_original · function
vllm_mlx.constrained.json_schema_processor._eos_logits_or_original(eos_set: set[int], logits: mx.array, build_allow_mask) -> mx.array
Function _eos_logits_or_original calls _eos_logits; returns logits if masked is None else masked.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
eos_set |
set[int] |
yes |
none |
Required positional or keyword input. |
logits |
mx.array |
yes |
none |
Required positional or keyword input. |
build_allow_mask |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
logits if masked is None else masked
Exceptions and behavior
Function _eos_logits_or_original calls _eos_logits; returns logits if masked is None else masked.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor · class
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor(schema: dict | None, tokenizer: Any)
Logits processor that constrains generation to valid JSON.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
schema |
dict \| None |
yes |
none |
Required positional or keyword input. |
tokenizer |
Any |
yes |
none |
Required positional or keyword input. |
Returns
- Constructs:
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor
Exceptions and behavior
Class JSONSchemaLogitsProcessor declares 15 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.__init__ · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.__init__(schema: dict | None, tokenizer: Any) -> None
Method JSONSchemaLogitsProcessor.__init__ updates self._tokenizer, self._schema, self._tok_data, self._disabled; calls is_available, LMFormatEnforcerNotAvailableError, get_tokenizer_data, _get_or_build_parser; can raise LMFormatEnforcerNotAvailableError.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
schema |
dict \| None |
yes |
none |
Required positional or keyword input. |
tokenizer |
Any |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
None
Exceptions and behavior
Method JSONSchemaLogitsProcessor.__init__ updates self._tokenizer, self._schema, self._tok_data, self._disabled; calls is_available, LMFormatEnforcerNotAvailableError, get_tokenizer_data, _get_or_build_parser; can raise LMFormatEnforcerNotAvailableError.
Directly raised exceptions: LMFormatEnforcerNotAvailableError.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._suffix · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._suffix(tokens_list: list[int]) -> list[int]
Return the slice of tokens that corresponds to generated output.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tokens_list |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[int] - Direct return expressions:
tokens_list[self._prompt_len:]
Exceptions and behavior
Method JSONSchemaLogitsProcessor._suffix updates self._prompt_len; calls len; returns tokens_list[self._prompt_len:].
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._decode_token_cached · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._decode_token_cached(tok_id: int) -> str | None
Return the decoded text for a single token (cached).
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tok_id |
int |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str | None - Direct return expressions:
cached;None;result
Exceptions and behavior
Method JSONSchemaLogitsProcessor._decode_token_cached calls self._token_decode_cache.get, self._tokenizer.decode, isinstance; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._decode_suffix · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._decode_suffix(suffix: list[int]) -> str | None
Decode suffix tokens to text.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
suffix |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str | None - Direct return expressions:
'';self._cached_suffix_text;None;result
Exceptions and behavior
Method JSONSchemaLogitsProcessor._decode_suffix updates self._cached_suffix_text, self._cached_suffix_len, self._json_ctx_scanned_len, self._json_ctx_in_string; calls len, self._tokenizer.decode, list, isinstance; has 4 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._suffix_is_complete_json · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._suffix_is_complete_json(suffix: list[int]) -> bool
Return True if the decoded suffix parses as a complete JSON value.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
suffix |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
bool - Direct return expressions:
False;True
Exceptions and behavior
Method JSONSchemaLogitsProcessor._suffix_is_complete_json calls self._decode_suffix, text.strip, json.loads; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._get_json_context · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._get_json_context(suffix: list[int]) -> str
Determine the JSON structural context of the current suffix.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
suffix |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
'other';'in_key';'key_start'
Exceptions and behavior
Method JSONSchemaLogitsProcessor._get_json_context updates self._json_ctx_in_string, self._json_ctx_last_quote_pos, self._json_ctx_scanned_len, self._brace_depth; calls self._decode_suffix, len, container_stack.append, container_stack.pop; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_at_key_context · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_at_key_context(context: str, suffix: list[int], allowed: list[int]) -> list[int]
Apply schema-aware filtering when in key-related context.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
context |
str |
yes |
none |
Required positional or keyword input. |
suffix |
list[int] |
yes |
none |
Required positional or keyword input. |
allowed |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[int] - Direct return expressions:
allowed;self._filter_key_start_tokens(suffix, allowed);self._filter_in_key_tokens(suffix, allowed)
Exceptions and behavior
Method JSONSchemaLogitsProcessor._filter_at_key_context calls self._filter_key_start_tokens, self._filter_in_key_tokens; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_key_start_tokens · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_key_start_tokens(suffix: list[int], allowed: list[int]) -> list[int]
Filter tokens at key-start position.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
suffix |
list[int] |
yes |
none |
Required positional or keyword input. |
allowed |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[int] - Direct return expressions:
result if result else allowed
Exceptions and behavior
Method JSONSchemaLogitsProcessor._filter_key_start_tokens calls self._decode_token_cached, result.append, tok_text.lstrip, rest.find; returns result if result else allowed.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_in_key_tokens · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_in_key_tokens(suffix: list[int], allowed: list[int]) -> list[int]
Filter tokens when we're inside an open key string.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
suffix |
list[int] |
yes |
none |
Required positional or keyword input. |
allowed |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[int] - Direct return expressions:
allowed;result if result else allowed
Exceptions and behavior
Method JSONSchemaLogitsProcessor._filter_in_key_tokens calls self._decode_suffix, text.rfind, self._decode_token_cached, result.append; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._is_valid_key_prefix · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._is_valid_key_prefix(prefix: str) -> bool
Return True if prefix is a prefix of at least one valid key name.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
prefix |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
bool - Direct return expressions:
any((name.startswith(prefix) for name in self._valid_key_names))
Exceptions and behavior
Method JSONSchemaLogitsProcessor._is_valid_key_prefix calls any, name.startswith; returns any((name.startswith(prefix) for name in self._valid_key_names)).
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_nonprogress_whitespace_tokens · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._filter_nonprogress_whitespace_tokens(suffix: list[int], allowed: list[int]) -> list[int]
Stop constrained JSON from spending a long run on pure whitespace.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
suffix |
list[int] |
yes |
none |
Required positional or keyword input. |
allowed |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[int] - Direct return expressions:
allowed;filtered if filtered else allowed
Exceptions and behavior
Method JSONSchemaLogitsProcessor._filter_nonprogress_whitespace_tokens calls self._decode_suffix, len, text.rstrip, self._decode_token_cached; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._build_allow_mask · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor._build_allow_mask(allowed: list[int], vocab_size: int) -> mx.array
Build a 1-D mask of length vocab_size where allowed positions are 0 and disallowed positions are -inf.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
allowed |
list[int] |
yes |
none |
Required positional or keyword input. |
vocab_size |
int |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
mx.full((vocab_size,), -float('inf'));mx.array(buf)
Exceptions and behavior
Method JSONSchemaLogitsProcessor._build_allow_mask calls mx.full, float, np.full, mx.array; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.__call__ · method
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.__call__(tokens: mx.array, logits: mx.array) -> mx.array
Apply the allowed-tokens mask to logits.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tokens |
mx.array |
yes |
none |
Required positional or keyword input. |
logits |
mx.array |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
_eos_logits_or_original(self._eos_set, logits, self._build_allow_mask);eos_logits;logits;logits + mask
Exceptions and behavior
Method JSONSchemaLogitsProcessor.__call__ updates self._disabled; calls _eos_logits_or_original, hasattr, tokens.tolist, list; has 4 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.schema · method
Return the normalized JSON Schema enforced for this request.
Parameters
This callable has no explicit inputs.
Returns
- Type:
dict | None - Direct return expressions:
self._schema
Exceptions and behavior
Method JSONSchemaLogitsProcessor.schema returns self._schema.
No direct raise statement appears in this definition.
vllm_mlx.constrained.json_schema_processor.JSONSchemaLogitsProcessor.vocab_size · method
Return the tokenizer vocabulary size used to construct masks.
Parameters
This callable has no explicit inputs.
Returns
- Type:
int - Direct return expressions:
self._vocab_size
Exceptions and behavior
Method JSONSchemaLogitsProcessor.vocab_size returns self._vocab_size.
No direct raise statement appears in this definition.
Complete symbol map¶
This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.
| Symbol | Kind | Signature and inputs | What it does | Source |
|---|---|---|---|---|
LMFormatEnforcerNotAvailableError |
class | LMFormatEnforcerNotAvailableError() |
Raised when lm-format-enforcer is required but not installed. |
#L33-L34 |
_canonical_schema_key |
function | _canonical_schema_key(schema: dict \| None) -> str |
Function _canonical_schema_key calls json.dumps(schema, sort_keys=True, separators=(',', ':')).encode, json.dumps, hashlib.sha256(blob).hexdigest, hashlib.sha256; has 2 explicit return paths. |
#L50-L54 |
_get_or_build_parser |
function | _get_or_build_parser(schema: dict \| None) -> tuple[dict, Any] |
Return (parser_schema, JsonSchemaParser) for schema, memoised. |
#L57-L73 |
is_available |
function | is_available() -> bool |
Return True iff lm-format-enforcer is importable. |
#L76-L82 |
_simplify_schema |
function | _simplify_schema(schema: dict) -> dict |
Pre-process a JSON Schema for lm-format-enforcer compatibility. |
#L97-L207 |
_simplify_schema._resolve |
nested function | _simplify_schema._resolve(node: Any, depth: int = 0) -> Any |
Nested Function _simplify_schema._resolve calls isinstance, ref.split, len, resolving.add; has 3 explicit return paths. |
#L121-L205 |
_force_no_additional_properties |
function | _force_no_additional_properties(schema: dict) -> dict |
Return a deep copy of schema with additionalProperties: false injected into every object-type sub-schema that declares properties. |
#L210-L224 |
_inject_no_additional_props |
function | _inject_no_additional_props(node: Any) -> None |
Recursively inject additionalProperties: false into node. |
#L227-L238 |
_collect_property_names |
function | _collect_property_names(schema: dict \| None) -> set[str] |
Collect all property names declared anywhere in schema. | #L241-L247 |
_walk_properties |
function | _walk_properties(node: Any, names: set[str]) -> None |
Function _walk_properties calls isinstance, node.get, names.update, props.keys; returns None. |
#L250-L264 |
_complete_json_eos_logits |
function | _complete_json_eos_logits(eos_set: set[int], suffix: list[int], logits: mx.array, is_complete_json, build_allow_mask) -> mx.array \| None |
Function _complete_json_eos_logits calls is_complete_json, _eos_logits; has 2 explicit return paths. |
#L267-L276 |
_eos_logits |
function | _eos_logits(eos_set: set[int], logits: mx.array, build_allow_mask) -> mx.array \| None |
Function _eos_logits calls build_allow_mask, sorted; has 2 explicit return paths. |
#L279-L290 |
_eos_logits_or_original |
function | _eos_logits_or_original(eos_set: set[int], logits: mx.array, build_allow_mask) -> mx.array |
Function _eos_logits_or_original calls _eos_logits; returns logits if masked is None else masked. |
#L293-L299 |
JSONSchemaLogitsProcessor |
class | JSONSchemaLogitsProcessor(schema: dict \| None, tokenizer: Any) |
Logits processor that constrains generation to valid JSON. | #L302-L924 |
JSONSchemaLogitsProcessor.__init__ |
method | JSONSchemaLogitsProcessor.__init__(schema: dict \| None, tokenizer: Any) -> None |
Method JSONSchemaLogitsProcessor.__init__ updates self._tokenizer, self._schema, self._tok_data, self._disabled; calls is_available, LMFormatEnforcerNotAvailableError, get_tokenizer_data, _get_or_build_parser; can raise LMFormatEnforcerNotAvailableError. |
#L317-L414 |
JSONSchemaLogitsProcessor._suffix |
method | JSONSchemaLogitsProcessor._suffix(tokens_list: list[int]) -> list[int] |
Return the slice of tokens that corresponds to generated output. |
#L418-L426 |
JSONSchemaLogitsProcessor._decode_token_cached |
method | JSONSchemaLogitsProcessor._decode_token_cached(tok_id: int) -> str \| None |
Return the decoded text for a single token (cached). | #L428-L442 |
JSONSchemaLogitsProcessor._decode_suffix |
method | JSONSchemaLogitsProcessor._decode_suffix(suffix: list[int]) -> str \| None |
Decode suffix tokens to text. | #L444-L493 |
JSONSchemaLogitsProcessor._suffix_is_complete_json |
method | JSONSchemaLogitsProcessor._suffix_is_complete_json(suffix: list[int]) -> bool |
Return True if the decoded suffix parses as a complete JSON value. |
#L495-L520 |
JSONSchemaLogitsProcessor._get_json_context |
method | JSONSchemaLogitsProcessor._get_json_context(suffix: list[int]) -> str |
Determine the JSON structural context of the current suffix. | #L522-L643 |
JSONSchemaLogitsProcessor._filter_at_key_context |
method | JSONSchemaLogitsProcessor._filter_at_key_context(context: str, suffix: list[int], allowed: list[int]) -> list[int] |
Apply schema-aware filtering when in key-related context. | #L645-L662 |
JSONSchemaLogitsProcessor._filter_key_start_tokens |
method | JSONSchemaLogitsProcessor._filter_key_start_tokens(suffix: list[int], allowed: list[int]) -> list[int] |
Filter tokens at key-start position. | #L664-L717 |
JSONSchemaLogitsProcessor._filter_in_key_tokens |
method | JSONSchemaLogitsProcessor._filter_in_key_tokens(suffix: list[int], allowed: list[int]) -> list[int] |
Filter tokens when we're inside an open key string. | #L719-L758 |
JSONSchemaLogitsProcessor._is_valid_key_prefix |
method | JSONSchemaLogitsProcessor._is_valid_key_prefix(prefix: str) -> bool |
Return True if prefix is a prefix of at least one valid key name. | #L760-L762 |
JSONSchemaLogitsProcessor._filter_nonprogress_whitespace_tokens |
method | JSONSchemaLogitsProcessor._filter_nonprogress_whitespace_tokens(suffix: list[int], allowed: list[int]) -> list[int] |
Stop constrained JSON from spending a long run on pure whitespace. | #L764-L793 |
JSONSchemaLogitsProcessor._build_allow_mask |
method | JSONSchemaLogitsProcessor._build_allow_mask(allowed: list[int], vocab_size: int) -> mx.array |
Build a 1-D mask of length vocab_size where allowed positions are 0 and disallowed positions are -inf. |
#L795-L810 |
JSONSchemaLogitsProcessor.__call__ |
method | JSONSchemaLogitsProcessor.__call__(tokens: mx.array, logits: mx.array) -> mx.array |
Apply the allowed-tokens mask to logits. |
#L814-L910 |
JSONSchemaLogitsProcessor.schema |
method | JSONSchemaLogitsProcessor.schema() -> dict \| None |
Return the normalized JSON Schema enforced for this request. | #L915-L918 |
JSONSchemaLogitsProcessor.vocab_size |
method | JSONSchemaLogitsProcessor.vocab_size() -> int |
Return the tokenizer vocabulary size used to construct masks. | #L921-L924 |