Skip to content

Calculate only the variables a household calculation lists - #1177

Merged
anth-volk merged 4 commits into
mainfrom
v2plan/d2-2a-sim-household-variables
Dec 2, 2026
Merged

anth-volk merged 4 commits into
mainfrom
v2plan/d2-2a-sim-household-variables

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #1176

Same-Repository Draft PR Check

  • This PR is a draft.
  • This PR is from a branch in PolicyEngine/policyengine-sim-api, not a personal fork.

Changelog

  • This PR adds a changelog fragment, changelog.d/<name>.<type>.md, whose type is breaking, added, changed, fixed or removed: changelog.d/household-calculation-variables.added.md.

Summary

POST /household-calculations takes variables, an optional list of the variables to calculate, on both kinds of body. With it, app v3's Household tab can ask API v2 for the roughly 330 variables it reads, instead of every variable through add_missing_variables. In local measurements, asking for every variable made one household calculation take 100–154 s.

  • Request models (household_api_models.py): HouseholdCalculationRequest and InlineHouseholdCalculationRequest both take variables, a list of up to 10,000 distinct names of 1–255 characters, or null. A duplicate name, a longer list, or a list sent together with add_missing_variables: true is refused with a 422 at ["body", "variables"]. An empty list adds nothing.
  • Household function's request (libs/policyengine-simulation-contract, HouseholdExecutionRequest): it carries variables and refuses the same combinations. It leaves the field out of its JSON when the caller sent none. Modal household functions deployed before this change refuse unknown fields, so requests without variables keep working on them. A request with variables that reaches such a function fails its job; that is accepted.
  • Job preparation (household_job_preparation.py, household_contract_fields.py): both builders pass the list on, and a refused variables field maps back to the caller's variables, never to a 500. The job row keeps the list in request_data; no column is added. Household jobs are never reused by key, so a job calculated with one list is never returned for another.
  • Executor (household_inputs.py, household_calculation.py): add_listed_variables adds each listed variable as null for the calculation year to every member of its entity that has no value for it in that year, input variables included, so it is calculated. Every value the household gives, for that year or another period, is kept. With axes, every variable in the household is calculated at every point, as before, so listed variables get one value per point and the axis still sets the input it varies. find_unrecognized_listed_variables reports a listed name the country model lacks as an invalid_inputs failure, a household_variable input error at variables[<index>]. These errors come after the household's own unrecognized inputs and before the policy's.
  • Household reports set add_missing_variables themselves and don't send variables; they are unchanged.
  • Docs: the service README and the docstrings describe the field.

The design was decided by Anthony on 2026-12-01 and 2026-12-02: the behaviour and shape, null for listed inputs, unknown names failing the job, refusing both fields, the list kept only in request_data, and accepting failure on older workers. API v1's POST /v2/household-calculations passes the field on in a follow-up, which pins the policyengine-api-simulation-client release this merge publishes. App v3 (PolicyEngine/policyengine-app-v2#1611) sends the list.

Testing

Tests that fail before the change came first, in 0c4f76db; the change is in 579003e8. Each of the 23 new or changed tests was run on its own by pytest node ID:

  • On 0c4f76db: all 23 failed. Most failed on assertions: the field mappings returned None, the OpenAPI property set differed, the routes answered 422 extra_forbidden instead of 202 or value_error, and refusal messages didn't match. Six failed because the request models refused the unknown field variables.
  • On 579003e8: all 23 passed. That is 4 tests of the household function's request format, 15 of the service, and 4 of the executor, one of which uses the US model.

The tests:

  • Household function's request format (libs/policyengine-simulation-contract/tests/test_household_execution.py): test_a_request_may_list_the_variables_to_calculate, test_a_request_without_listed_variables_leaves_the_field_out, test_listed_variables_are_distinct_and_at_most_ten_thousand, test_listed_variables_are_refused_with_add_missing_variables.
  • Service (projects/policyengine-simulation-entry/tests):
    • test_household_contract_fields.py: two new cases of test_each_refused_contract_field_traces_to_the_request_field_that_set_it, and test_a_refused_variable_list_is_located_at_the_inline_bodys_variables.
    • test_openapi.py::test_a_household_calculation_takes_a_stored_or_an_inline_household.
    • test_household_routes.py: test_either_body_may_list_the_variables_to_calculate, and four new cases of test_a_refused_body_is_located_at_its_own_fields.
    • test_household_calculations.py: test_a_stored_households_listed_variables_reach_its_function_and_job, test_an_inline_households_listed_variables_reach_its_function_and_job, test_listed_variables_with_add_missing_variables_are_refused.
  • Executor (projects/policyengine-simulation-executor/tests/test_household_calculation.py): test_listed_variables_are_added_where_their_entity_lacks_the_years_value, test_with_axes_listed_variables_are_calculated_at_every_point, test_listed_variables_the_model_lacks_fail_with_the_households_inputs, test_us_listed_variables_are_the_only_ones_added_and_calculated.

Ruff format and lint, the rules registry, the quality guards (file length included), the lock checks, and Pyright on the contract, service and executor packages all pass locally.

Review

One round of adversarial review, of 579003e8, found no bugs (no high, medium or low). Following the rule to stop after a round with no bugs, no further rounds ran. Its documentation findings are fixed in 898d90ef, which changes docs only:

  • the field description wrongly said that leaving the field out calculates only the household's null values;
  • the error docstrings didn't say where listed variables' errors come;
  • new docstrings named API v1 differently;
  • the README's paragraph placement and field lists were off;
  • there was no comment on the field order the validators rely on.

Left, not bugs: some untested cases.

  • Listing the variable an axis varies.
  • A listed variable given as one value.
  • An explicit "variables": null sent to the route.
  • A listed variable whose entity collection the household lacks (it gets no value, as with add_missing_variables).
  • The generated client leaving an unset variables out of its request.

Merge order

This PR builds on main, not on another task's branch. Its merge waits only for the earlier tasks of tracker substage D2 to merge, in tracker order:

It needs none of their deployments. No deployment code, workflow or environment variable changes.

🤖 Generated with Claude Code

anth-volk and others added 4 commits December 2, 2026 00:58
Tests that fail before the change: a household calculation request,
stored or inline, may list the variables to calculate; the household
function adds each listed variable as null for the calculation year
wherever its entity has no value for that year, keeps every value the
household gives, calculates listed variables at every axis point, and
reports names the country model lacks as invalid inputs. A list is
refused with duplicate names, beyond 10,000 names, or beside
add_missing_variables, and a request without one leaves the field out
of the household function's request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
POST /household-calculations takes `variables`, a list of distinct
variable names (at most 10,000), on a stored household's body and on an
inline one's. The household function's request carries the list, and
leaves the field out when the caller sent none, so household functions
deployed before it still take requests without it. The household function
adds each listed variable as null for the calculation year to every
member of its entity without a value for that year, input variables
included, keeps every value the household gives, and reports a listed
name the country model lacks as an unrecognized input at its place in the
list. A list beside add_missing_variables is refused at `variables`, in
the route's models and in the household function's request. The job keeps
the list in request_data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The field description no longer says that omitting `variables` calculates
only the household's own null values: add_missing_variables calculates
every variable, and with axes every variable in the household is
calculated at every point. The request format's error docstrings say
where listed variables' input errors come, the executor's docstrings name
API v1, and a comment on each model says `variables` is declared after
add_missing_variables because its validator reads that value. The
service README describes `variables` after both kinds of body and names
it in each body's fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_a_household_calculation_request_names_its_fields_only pins the
stored household body's exact properties, so it now includes
`variables`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review December 2, 2026 13:47
@anth-volk
anth-volk merged commit 29073ed into main Dec 2, 2026
28 checks passed
anth-volk added a commit to PolicyEngine/policyengine-api that referenced this pull request Dec 2, 2026
…ables

policyengine-api-simulation-client 0.20261202.458, published by
PolicyEngine/policyengine-sim-api#1177's release, has `variables` on its
HouseholdCalculationRequest and InlineHouseholdCalculationRequest, so a
household calculation that lists variables is sent through the generated
client instead of being refused before sending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let a household calculation list the variables to calculate

1 participant