Reference
build_regex
Convert a Pydantic model or JSON schema to a regex.
Examples:
>>> from typing import Literal
>>> from pydantic import BaseModel, Field
>>> from litelines import build_regex
>>>
>>> class Sentiment(BaseModel):
... "Correctly inferred `Sentiment` with all the required parameters with correct types."
...
... label: Literal["positive", "negative"] = Field(
... ..., description="Sentiment of the text"
... )
>>> build_regex(Sentiment, whitespace_pattern="")
'\\{"label":("positive"|"negative")\\}'
>>> build_regex(Sentiment, whitespace_pattern="[ ]?")
'[ ]?\\{[ ]?"label"[ ]?:[ ]?("positive"|"negative")[ ]?\\}'
>>> build_regex(Sentiment)
'[\\n\\t ]*\\{[\\n\\t ]*"label"[\\n\\t ]*:[\\n\\t ]*("positive"|"negative")[\\n\\t ]*\\}'
>>> build_regex(Sentiment, include_tool_call=True, whitespace_pattern="")
'<tool_call>\\{"name":"Sentiment","arguments":\\{"label":("positive"|"negative")\\}\\}</tool_call>'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Union[dict, str, Type[Any]]
|
The Pydantic model or JSON schema. |
required |
include_tool_call
|
optional
|
Is the Pydantic model expecting a tool call or not. |
False
|
tool_call_start
|
optional
|
The expected tool call start. |
'<tool_call>'
|
tool_call_end
|
optional
|
The expected tool call end. |
'</tool_call>'
|
whitespace_pattern
|
optional
|
Pattern to use for JSON syntactic whitespace. |
'[\\n\\t ]*'
|
Returns:
Type | Description |
---|---|
str
|
The JSON schema converted to a regex. |
Raises:
Type | Description |
---|---|
ValueError
|
An error occurs if the schema is not a Pydantic model, a dictionary, or a string. |
Source code in src/litelines/build_regex.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
build_dfa
Build a deterministic finite automaton that fullfils the response format requirement
Examples:
>>> from typing import Literal
>>> from pydantic import BaseModel, Field
>>> from transformers import AutoTokenizer
>>> from litelines import build_dfa
>>>
>>> model_id = "Qwen/Qwen3-0.6B"
>>> tokenizer = AutoTokenizer.from_pretrained(model_id)
>>> build_dfa("A|B", tokenizer)
{0: {33: 1, 32: 1}}
>>> build_dfa("A0|B0", tokenizer)
{1: {15: 3}, 2: {15: 3}, 0: {33: 1, 32: 2}}
>>>
>>> class Sentiment(BaseModel):
... "Correctly inferred `Sentiment` with all the required parameters with correct types."
...
... label: Literal["positive", "negative"] = Field(
... ..., description="Sentiment of the text"
... )
>>> build_dfa(Sentiment, tokenizer, whitespace_pattern="")
{18: {72: 15, 344: 17, 533: 16}, 9: {92: 28}, 20: {72: 21, 12303: 7, 275: 6, 3404: 8}, 23: {2974: 5, 25: 24}, 1: {14380: 2, 75: 25, 4260: 26, 1502: 4}, 14: {10251: 15, 83: 18}, 8: {9207: 28, 1: 9}, 22: {82: 20, 6321: 21, 46865: 6}, 4: {3252: 5, 1: 23, 788: 24}, 0: {4913: 1, 90: 27}, 13: {64: 14, 19488: 17, 266: 18, 1388: 16, 9307: 15}, 10: {68: 8}, 19: {436: 20, 78: 22, 34054: 6, 30724: 21}, 3: {75: 4}, 16: {9207: 28, 1: 9}, 12: {70: 13, 6743: 14}, 7: {586: 8, 85: 10}, 11: {68: 12, 15060: 16, 11188: 14, 791: 13}, 2: {68: 3, 301: 4}, 17: {68: 16}, 27: {92667: 4, 1: 1}, 6: {72: 7, 344: 10, 533: 8}, 5: {2724: 6, 77: 11, 28775: 13, 42224: 16, 79: 19, 5368: 22, 30487: 8, 966: 20, 811: 12}, 26: {1371: 3, 65: 2, 9779: 4}, 15: {586: 16, 85: 17}, 21: {10251: 7, 83: 6}, 24: {1: 5}, 25: {370: 2, 64: 26, 780: 4, 8229: 3}}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response_format
|
Union[dict, str, Type[Any]]
|
A Pydantic model, a dictionary, or a regular expression (as a string) that defines the expected response format |
required |
tokenizer
|
Union[str, PreTrainedTokenizer, PreTrainedTokenizerFast]
|
The model's tokenizer or the model name (as a string) |
required |
include_tool_call
|
optional
|
Is the Pydantic model expecting a tool call or not. |
False
|
tool_call_start
|
optional
|
The expected tool call start. |
'<tool_call>'
|
tool_call_end
|
optional
|
The expected tool call end. |
'</tool_call>'
|
whitespace_pattern
|
optional
|
Pattern to use for JSON syntactic whitespace. |
'[\\n\\t\\r ]*'
|
Returns:
Type | Description |
---|---|
dict[int, dict[int, int]]
|
The deterministic finite automaton as a dictionary. |
Raises:
Type | Description |
---|---|
ValueError
|
An error occurs if the response format is not a Pydantic model, a dictionary, or a string that corresponds to the regular expression. |
Source code in src/litelines/build_dfa.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 |
|
draw_dfa
Create a graphical representation of a Deterministic Finite Automaton (DFA) using Graphviz DOT language.
The function visualizes the DFA with:
- states as circles (double circles for final states)
- directed edges showing transitions between states
- edge labels containing tables of token IDs and their corresponding text
- optional red highlighting for edges in the provided trajectory
Examples:
>>> from typing import Literal
>>> from pydantic import BaseModel, Field
>>> from transformers import AutoTokenizer
>>> from litelines import build_dfa
>>>
>>> model_id = "Qwen/Qwen3-0.6B"
>>> tokenizer = AutoTokenizer.from_pretrained(model_id)
>>> draw_dfa("A|B", tokenizer, render=False)
#
>>> draw_dfa("A0|B0", tokenizer, render=False)
#
>>>
>>> class Sentiment(BaseModel):
... "Correctly inferred `Sentiment` with all the required parameters with correct types."
...
... label: Literal["positive", "negative"] = Field(
... ..., description="Sentiment of the text"
... )
>>> draw_dfa(Sentiment, tokenizer, whitespace_pattern="")
#
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dfa
|
Union[dict[int, dict[int, int]], str, Type[Any]]
|
The DFA representation, which can be either: A dictionary mapping states to their transitions A JSON schema string A Pydantic schema |
required |
tokenizer
|
Union[PreTrainedTokenizer, PreTrainedTokenizerFast]
|
The tokenizer used to decode token IDs into readable text |
required |
trajectory
|
list
|
Optional list of tokens representing a path through the DFA |
[]
|
include_tool_call
|
optional
|
Is the Pydantic model expecting a tool call or not. |
False
|
tool_call_start
|
optional
|
The expected tool call start. |
'<tool_call>'
|
tool_call_end
|
optional
|
The expected tool call end. |
'</tool_call>'
|
whitespace_pattern
|
optional
|
Pattern to use for JSON syntactic whitespace. |
'[\\n\\t ]*'
|
max_labels_per_edge
|
optional
|
Maximum number of labels to show per edge |
3
|
remove_outer_whitespace
|
optional
|
Whether to strip whitespace from token labels in the table. |
True
|
render
|
optional
|
Whether to return a rendered Graphviz Source object or raw DOT string |
True
|
Returns:
Type | Description |
---|---|
str | None
|
A Graphviz Source object if render=True, otherwise the DOT language string |
Source code in src/litelines/draw_dfa.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 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 |
|
Schema_Processor
Bases: LogitsProcessor
Build the Logits Processor that enforces the response format
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
Token IDs of shape (batch_size, sequence_length) |
required | |
scores
|
Logits of shape (batch_size, vocab_size) |
required |
Returns:
Type | Description |
---|---|
The logits processor that enforces the response format |
Source code in src/litelines/transformers/schemaprocessor.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
reset_state()
Reset the processor to its initial state
Source code in src/litelines/transformers/schemaprocessor.py
105 106 107 108 109 110 |
|