Recursion Depth Management

xgrammar.get_max_recursion_depth() int[source]

Get the maximum allowed recursion depth. The depth is shared per process.

The maximum recursion depth is determined in the following order: 1. Manually set via set_max_recursion_depth() 2. XGRAMMAR_MAX_RECURSION_DEPTH environment variable (if set and is a valid integer <= 1000000) 3. Default value of 10000

Returns:

max_recursion_depth – The maximum allowed recursion depth.

Return type:

int

xgrammar.set_max_recursion_depth(max_recursion_depth: int) None[source]

Set the maximum allowed recursion depth. The depth is shared per process. This method is thread-safe.

Parameters:

max_recursion_depth (int) – The maximum allowed recursion depth.

xgrammar.max_recursion_depth(temp_depth: int)[source]

A context manager for temporarily setting recursion depth.

Example

>>> with recursion_depth(1000):
...     # recursion depth is 1000 here
...     pass
... # recursion depth is restored to original value