Repository navigation
Calculate only the variables a household calculation lists - #1177
Merged
Merged
Conversation
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>
This was referenced Dec 1, 2026
Open
Open
anth-volk
marked this pull request as ready for review
December 2, 2026 13:47
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1176
Same-Repository Draft PR Check
PolicyEngine/policyengine-sim-api, not a personal fork.Changelog
changelog.d/<name>.<type>.md, whose type is breaking, added, changed, fixed or removed:changelog.d/household-calculation-variables.added.md.Summary
POST /household-calculationstakesvariables, 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 throughadd_missing_variables. In local measurements, asking for every variable made one household calculation take 100–154 s.household_api_models.py):HouseholdCalculationRequestandInlineHouseholdCalculationRequestboth takevariables, 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 withadd_missing_variables: trueis refused with a 422 at["body", "variables"]. An empty list adds nothing.libs/policyengine-simulation-contract,HouseholdExecutionRequest): it carriesvariablesand 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 withoutvariableskeep working on them. A request withvariablesthat reaches such a function fails its job; that is accepted.household_job_preparation.py,household_contract_fields.py): both builders pass the list on, and a refusedvariablesfield maps back to the caller'svariables, never to a 500. The job row keeps the list inrequest_data; no column is added. Household jobs are never reused by key, so a job calculated with one list is never returned for another.household_inputs.py,household_calculation.py):add_listed_variablesadds 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_variablesreports a listed name the country model lacks as aninvalid_inputsfailure, ahousehold_variableinput error atvariables[<index>]. These errors come after the household's own unrecognized inputs and before the policy's.add_missing_variablesthemselves and don't sendvariables; they are unchanged.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'sPOST /v2/household-calculationspasses the field on in a follow-up, which pins thepolicyengine-api-simulation-clientrelease 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 in579003e8. Each of the 23 new or changed tests was run on its own by pytest node ID:0c4f76db: all 23 failed. Most failed on assertions: the field mappings returned None, the OpenAPI property set differed, the routes answered 422extra_forbiddeninstead of 202 orvalue_error, and refusal messages didn't match. Six failed because the request models refused the unknown fieldvariables.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:
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.projects/policyengine-simulation-entry/tests):test_household_contract_fields.py: two new cases oftest_each_refused_contract_field_traces_to_the_request_field_that_set_it, andtest_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 oftest_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.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 in898d90ef, which changes docs only:Left, not bugs: some untested cases.
"variables": nullsent to the route.add_missing_variables).variablesout 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