Computed fields¶
Language models read numbers well and calculate them badly. A compute
field is calculated by code from other fields, so totals, ratios and
installments are exact:
"net_salary": {"type": "number", "integer": true, "max_digits": 10},
"installment": {"type": "number", "integer": true, "max_digits": 10},
"dsr_percent": {"type": "compute", "expr": "installment / net_salary * 100", "round": 2}
The pattern: the model extracts, code computes, the model explains.
Options¶
| Option | Meaning |
|---|---|
expr |
The formula: field names, numbers, + - * /, parentheses, abs, min, max, round. |
round |
0 gives an integer, 1–10 rounds to that many decimals, omitted keeps full precision. |
on, on_reject |
Flow rules, as on any field. |
- Fields named in
exprbecome dependencies automatically. - A computed field can use other computed fields
(
new_installment→total_installment→dsr_percent). - Later fields see the computed value like any other value.
- A name that is not a field of the schema is rejected with HTTP 400.
Missing inputs¶
If an input is null or not a number, or the formula divides by zero, the
result is null. The request does not stop by itself, because a missing
value can be legitimate. Decide with a rule:
"dsr_percent": {"type": "compute", "expr": "installment / net_salary * 100", "round": 2,
"on": [{"when": {"eq": null}, "do": "reject"}]}
Use review instead of reject when the value matters but should not stop
the request.
Policy gates¶
A computed field in a later stage is a good place for checks that stop the
request, because a reject only cancels fields of its own stage: the values
extracted earlier stay in result for your records.
"age_gate": {"type": "compute", "expr": "age", "round": 0,
"on": [{"when": {"lt": 21}, "do": "reject"},
{"when": {"gt": 55}, "do": "reject"}]}
In the response¶
- The value is in
result. - Each calculation is in
trace:{"do": "compute", "field": "dsr_percent", "expr": "installment / net_salary * 100", "value": 40.29}. - Computed fields have no
confidence: they are not model output. Protect their inputs instead (extract,votes, confidence rules).
See the credit assessment example for seven computed fields in one schema.