ConstrainCall
Deterministic Procedural JSON Generation via a 0.6B Parameter Language Model
Table of Contents
-
Description
-
What Problem Does ConstrainCall Solve?
-
Instructions
-
System Architecture
-
LLM Generation: The Complete Picture
-
Why Normal LLM Generation Is Not Enough
-
Constrained Decoding
-
ConstrainCall's Decoding Strategy
-
The Function-Calling Problem
-
The Procedural JSON Strategy
-
Detailed Generation Pipeline
-
Token Vocabulary and id_to_token
-
Why Token-to-String Mapping Is Necessary
-
Prefix-Constrained Function Selection
-
Trie-Based Interpretation
-
Number-Constrained Generation
-
String-Constrained Generation
-
Structural Tokens vs Generated Tokens
-
Complete Example: 2 + 3
-
State Evolution During Decoding
-
Formal View of the Decoder
-
Finite-State Machines
-
Regular Languages and Regex Constraints
-
Context-Free Grammars
-
Pushdown Automata
-
Grammar-Constrained Decoding
-
Tokenizer/Grammar Alignment
-
Why Token-Level Constraints Are Difficult
-
Constraint Types
-
ConstrainCall vs General Grammar Engines
-
Complexity and Performance
-
KV Cache vs Token History
-
Determinism
-
Correctness Guarantees
-
Current Limitations
-
Future Architecture
-
Research Directions
-
Resources and References
Description
ConstrainCall is an experimental structured-generation engine that converts natural-language requests into typed function calls using a small language model and token-level constrained decoding.
The project explores an important question:
Can a relatively small language model be transformed from a free-form text generator into a deterministic structured-output engine by controlling the decoding process?
Instead of allowing the language model to generate arbitrary text and attempting to repair the result afterward, ConstrainCall moves part of the responsibility from the model into the decoder.
The basic architecture is:
Natural Language
│
▼
┌────────────┐
│ Tokenizer │
└─────┬──────┘
│
▼
Token IDs
│
▼
┌──────────────────┐
│ Transformer LLM │
└────────┬─────────┘
│
▼
Logits
│
▼
┌──────────────────┐
│ Constraint Layer │
└────────┬─────────┘
│
Valid token set
│
▼
Masked logits
│
▼
Token selection
│
▼
Next Token ID
│
└──────────────┐
│
▼
Repeat decoding
The central observation is simple:
The model does not need to be allowed to choose every possible token.
If the decoder knows that the next token must be part of a function name, it can eliminate every token that cannot produce a valid function name.
If the decoder knows that the next value must be numeric, it can eliminate tokens that cannot continue a valid numeric representation.
This is the fundamental mechanism behind constrained decoding.
What Problem Does ConstrainCall Solve?
A standard causal language model estimates:
$$
P(x_t \mid x_1, x_2, ..., x_{t-1})
$$
At every generation step, it produces a probability distribution over the vocabulary.
For a vocabulary (V):
$$
|V| = 151936
$$
for Qwen3-0.6B.
Therefore, at every step, the model conceptually says:
token A -> probability 0.00001
token B -> probability 0.42
token C -> probability 0.003
...
token N -> probability 0.0002
The model has no inherent obligation to produce valid JSON.
For example, given:
What is the sum of 2 and 3?
an unconstrained model could produce:
or:
{"name":"fn_add_numbers","parameters":{"a":2,"b":3}}
or:
Sure! Here is the function call:
{"name": ...}
For a human, all three might be understandable.
For an automated function-calling interface, only one may be acceptable.
ConstrainCall therefore separates:
from:
The model decides what should be generated.
The constraint engine decides what is legally allowed to be generated.
Instructions
The project uses uv for dependency management.
Installation
Running
uv run python -m constraincall \
--functions_definition data/input/functions_definition.json \
--input data/input/function_calling_tests.json \
--output data/output/functions_result.json
Linting
Strict type checking
System Architecture
At a high level:
┌───────────────────────────────────────────────┐
│ Input Files │
│ │
│ functions_definition.json │
│ function_calling_tests.json │
└───────────────────────┬───────────────────────┘
│
▼
┌──────────────────┐
│ Input Validation │
└────────┬─────────┘
│
┌─────────┴──────────┐
│ │
▼ ▼
Functions Prompts
│ │
└─────────┬──────────┘
│
▼
Model Instance
│
▼
Tokenizer / Vocabulary
│
▼
ConstrainedDecoder
│
▼
Structured Function Call
│
▼
Type Validation
│
▼
JSON Output
The important design principle is that the decoder does not simply ask:
"What text should the model generate?"
Instead, it asks:
"Given the model's preferences, which of the currently legal tokens should be selected?"
LLM Generation: The Complete Picture
A useful mental model is:
Text
│
▼
Tokenizer
│
▼
Token IDs
│
▼
Embedding Lookup
│
▼
Positional Information
│
▼
Transformer Layers
│
├── Self Attention
│
├── MLP
│
├── Normalization
│
└── Residual Connections
│
▼
Hidden State
│
▼
Language Model Head
│
▼
Logits
│
▼
Constraint / Sampling
│
▼
Next Token ID
│
▼
Decode
│
▼
Text
Generation repeats this process token by token.
Text
Consider:
What is the sum of 2 and 3?
The neural network does not directly operate on this string.
The tokenizer converts it into integer IDs.
Conceptually:
"What" -> 120
"is" -> 54
"the" -> 88
"sum" -> 230
...
The exact tokenization depends on the tokenizer.
Tokenization
Modern LLM tokenizers usually operate on subwords or byte-level representations rather than simply splitting on spaces.
For example:
might become:
while another string might remain one token.
The tokenizer therefore implements a mapping:
$$
T : \Sigma^* \rightarrow V^*
$$
where:
-
$\Sigma^*$ is the set of possible input strings.
-
$V$ is the model vocabulary.
-
$V^*$ is a sequence of vocabulary tokens.
The result is a sequence of integers:
std::vector<int> input_ids;
For Qwen3-0.6B, the configured vocabulary size is 151,936.
The important point is:
A token ID is not a word ID.
A token can represent:
or:
or:
or:
or:
or:
This is extremely important for constrained decoding.
Token IDs
Suppose:
Then:
does not mean that 120 mathematically represents "hello".
It is simply an index into the vocabulary and model embedding table.
Conceptually:
Token ID
│
▼
120
│
▼
Embedding Matrix Row 120
Embedding Lookup
The model contains an embedding matrix:
$$
E \in \mathbb{R}^{|V| \times d}
$$
where:
-
$|V|$ = vocabulary size
-
$d$ = hidden/embedding dimension
For Qwen3-0.6B, the hidden size is 1024 according to the model configuration.
Conceptually:
$$
E \in \mathbb{R}^{151936 \times 1024}
$$
If:
then:
$$
E_{120}
$$
is selected.
In C++-like pseudocode:
std::vector<float> embedding =
embedding_matrix[120];
This is an embedding lookup, not a matrix multiplication over the entire vocabulary.
The token ID is simply used as an index.
Positional Information
Transformers must know that:
is different from:
Therefore, the representation contains information about token position.
Modern transformer models commonly use positional mechanisms such as rotary positional embeddings (RoPE).
Conceptually: