Practical technical guides / Recent development: promptfoo 0.126.0, released 18 November 2026

promptfoo 0.126.0 Rejects Unsupported Derived Metric Formulas Before Any Test Runs

Derived metric formulas are now compiled once over a small grammar instead of evaluated per row. What fails at start-up, what still fails silently, and upgrades.

By Sandeep Belgavi · Published 2026-11-22

Derived metric formulas are now compiled once over a small grammar instead of evaluated per row. What fails at start-up, what still fails silently, and upgrades.

The change in 0.126.0

promptfoo 0.126.0 was published on 18 November 2026. The first item under "Before upgrading" in its release notes concerns derived metrics, the named scores you compute from other scores, such as an F1 from true and false positives. String formulas now accept arithmetic, comparisons, conditional expressions and score names, including the special __count. Function calls, property access, assignments and formulas longer than 1,024 characters now stop the evaluation before any test cases run. The release notes give the migration in one line: replace max(a, b) with a > b ? a : b, and use a function in a JavaScript configuration for anything more complex.

The change comes from pull request 12125, "restrict derived metric formulas", merged on 4 November. This guide reads that change in the source, explains what moved from run time to start-up and what did not, and draws the general lesson for anyone who lets configuration strings become executable expressions in an evaluation pipeline.

Before: an expression engine per row

In 0.125.1, string formulas were evaluated with the mathjs library. After each scored row, the evaluator rebuilt a context from the running named scores plus __count, and for every derived metric called math.evaluate(formula, context). Two details of that loop matter. The metric was initialized with namedScores[name] ??= 0, and any exception was caught and logged at debug level. A formula that could not be evaluated, for any reason, therefore left the metric at zero or at its last good value, row after row, and the evaluation finished normally. Unless you ran with debug logging, a broken formula looked like a metric that happened to be zero.

mathjs is also a full expression language, with functions, units, matrices and assignment. Accepting it meant that a configuration string could do considerably more than combine scores.

After: compile once, with a small grammar

In 0.126.0 the evaluator calls a new prepareDerivedMetrics step once, before any test case runs. Each string formula is passed to compileDerivedMetricFormula, which parses it with the acorn JavaScript parser as a single expression and walks the syntax tree, accepting only these node types:

  • Numeric literals that are finite.
  • Identifiers, which are looked up as own properties of the score map at evaluation time and must be numbers.
  • Parenthesized expressions.
  • Unary plus and minus.
  • Binary +, -, *, / and the comparisons >, >=, <, <=, ==, !=, where comparisons return 1 or 0 and == compares strictly.
  • The conditional condition ? a : b.

Anything else raises an error naming the metric: Invalid formula for derived metric "f1": .... Comparison chains such as 0 < score < 1 are rejected with a message suggesting separate comparisons in a conditional, because in JavaScript that expression compares the boolean result of the first comparison with 1, which is almost never what the author meant. The source must be at most 1,024 characters and must be one expression with nothing after it.

The result is a plain function over the score map. JavaScript functions supplied in a JavaScript configuration bypass the compiler, as before; the documentation now describes that as the option for a trusted configuration.

What did not move to start-up

The compiler checks syntax. It does not know which score names will exist, because scores are produced by assertions as the evaluation runs. In 0.126.0 the per-row loop is otherwise unchanged: a missing score raises "Missing numeric score: name" inside the same try block, the error is logged at debug level, and the metric keeps its value. The updated documentation says this directly: unsupported formula syntax stops the evaluation before any test cases run, while scoring errors such as a missing input score are logged at debug level.

So a misspelled score name is still silent. That is the most likely mistake in a formula, and it is worth closing yourself.

A model you can run

The companion example is an original Python model of the documented grammar, not promptfoo code. It tokenizes, parses with the same precedence as JavaScript, marks parenthesized sub-expressions so comparison chains can be detected, and compiles each formula before scoring any row. Over four synthetic rows of true positives, false positives and false negatives:

  • The F1 formula tp + fp == 0 ? 0 : (tp + fn == 0 ? 0 : 2 * tp / (2 * tp + fp + fn)) over running totals of 3, 1 and 1 gives 0.75, and 0 over a row of zeros.
  • An unguarded tp / (tp + fp) over zeros gives NaN, as JavaScript division does. The conditional is how you avoid it.
  • Seven formulas are rejected at compile time: max(tp, fp), tp.constructor, tp = 1, 0 < tp < 1, a 1,025-character formula, tp ** 2 and tp; fp.
  • tp / (tp + fnn), with a misspelled score, compiles, stays at 0.0 and produces four debug lines, one per row. With a known-names check before scoring, it is rejected.

The known-names check is a few lines: collect the identifiers from the compiled tree and compare them with the score names your assertions declare.

fn, names = compile_formula(name, formula)
unknown = names - known_scores - set(derived_metric_names) - {'__count'}
if unknown:
    raise FormulaError(f'derived metric "{name}" uses unknown scores: {sorted(unknown)}')

The model differs from promptfoo in one documented way: it accepts decimal and exponent number forms only, while acorn accepts every JavaScript numeric literal.

Why an allowlist rather than a sandbox

There are two ways to make a configuration expression safe. One is to keep a general expression language and try to fence it in, by removing dangerous functions, freezing objects or running it in an isolated context. The other is to accept only a grammar small enough to read in one sitting and compile it to a closed set of operations. The first has to anticipate every way the language can reach something it should not; the second only has to be right about a dozen node types. promptfoo's new compiler is the second kind: after parsing, the only code that runs for a row is the handful of arithmetic and comparison functions in a fixed table.

The cost is expressiveness, and the release notes pay it openly: formulas that used mathjs functions must be rewritten, and anything genuinely complex moves to a JavaScript function, where the author is visibly writing code in a trusted file. That is a good trade for metric formulas, which in practice are weighted sums, ratios and guards against zero.

Upgrading

  1. Search configurations for derivedMetrics and list every string formula.
  2. Replace mathjs functions: max(a, b) becomes a > b ? a : b, min the reverse, and an absolute value becomes a < 0 ? -a : a. Anything iterative or with many branches belongs in a JavaScript function.
  3. Replace range checks written as chains with nested conditionals: score > 0 ? (score < 1 ? 1 : 0) : 0, the form the documentation gives.
  4. Guard every division whose denominator can be zero.
  5. Run the evaluation once with debug logging and search for "Could not evaluate derived metric". Any line there is a name or data problem the compiler cannot see.
  6. Pin the version in CI so the upgrade happens in one reviewed change.

If you do not use derivedMetrics, the change does not affect you. The release notes' other "Before upgrading" item, Node.js 24 for full-repository code scans, is unrelated.

The general lesson

Evaluation pipelines accumulate small languages: formulas for metrics, conditions for routing, templates for prompts. Each one is configuration that becomes code. The pattern in this change is the right default for all of them: parse once, before any work starts; accept the smallest grammar that covers real use; reject everything else with an error that names the offending entry; and keep an escape hatch, here a JavaScript function, for trusted authors who need more. The remaining gap, names that do not exist, is a reminder that syntax checks are only half of validation.

Verification

The companion example's seven tests check the F1 values, the NaN from an unguarded division, all seven compile-time rejections and their messages, the silent misspelling with and without the name check, precedence and parentheses, the 1,024-character boundary, and JavaScript division signs. The interactive lab linked below lets you edit a formula, see whether it compiles, and step through its evaluation row by row.

Run the example yourself

The formula tokenizer, parser and compiler, the row scorer, the name check and seven tests. It is not promptfoo code. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.

Interactive experiment

Derived metric formulas: compile once, reject early, evaluate per row. Synthetic inputs computed in your browser; read the lab notes on assumptions and limits before drawing conclusions.

Primary references and authorship

Sources read 22 November 2026. Original explanation, measurements and synthetic examples by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.

Related on this site: