Skip to content

Validate US household state_code_str instead of silently defaulting to CA#50

Merged
vahid-ahmadi merged 1 commit into
mainfrom
fix/us-state-code-validation
Jul 19, 2026
Merged

Validate US household state_code_str instead of silently defaulting to CA#50
vahid-ahmadi merged 1 commit into
mainfrom
fix/us-state-code-validation

Conversation

@vahid-ahmadi

Copy link
Copy Markdown
Contributor

Investigation

Reported symptom: calculate_household returned identical US results with state_code, state_code_str, or neither — including a non-zero state income tax.

Reproduced against the hosted server (single filer, $200k employment income, 2026):

household arg state_income_tax
{"state_code_str": "CA"} 14,270.80
{"state_code_str": "TX"} 0.00
omitted 14,270.80
{"state_code": "CA"} (wrong key) 14,270.80

The state field is NOT ignored — CA and TX differ correctly (net income 132,036 vs 148,927). Our layer forwards household to pe.us.calculate_household at the correct entity level, and there is no caching.

Root cause

PolicyEngine US defaults to CA when no state is supplied. The original test passed the state under the wrong key / at the top level, where it was silently dropped — so all three calls fell back to the CA default and looked identical, with a plausible non-zero state tax masking the problem.

Not a wiring bug, but a real silent-failure bug: a caller who mistypes the key gets a confidently wrong answer for 49 states rather than an error.

Fix

integration/src/policyengine_macro/core.py — new _validate_us_household, called from _pe_run before the PolicyEngine import:

  • state_code instead of state_code_strValueError naming the correct key
  • invalid / non-two-letter code → ValueError
  • codes normalised to upper case
  • mcp_server.py docstring now states the CA default explicitly

Tests

integration/tests/test_core.py:

  • test_pe_household_us_state_code_changes_state_income_tax (slow) — CA vs TX must differ, TX must be exactly 0
  • wrong-key and invalid-code rejection, case normalisation (fast, no PolicyEngine needed)

Fast suite: 104 passed. test_summary_parses fails on this branch and on main alike — pre-existing and unrelated.

🤖 Generated with Claude Code

…o CA

PolicyEngine US takes the state as a household-level `state_code_str` and
silently defaults to California when it is absent or given under a wrong
key. A caller passing `state_code` (the person-level-looking name) got a
plausible but wrong CA answer -- $14,270 of state income tax on a $200k
single filer that should be $0 in TX.

The state itself was always wired through correctly; the failure mode was
the silent fallback. Now reject a wrong key or an invalid code with an
actionable message, normalise case, and document the CA default in the
tool docstring.

Tests: CA-vs-TX regression (TX must be 0, CA must differ), wrong-key and
invalid-code rejection, case normalisation.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 19, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
macromod Ready Ready Preview, Comment Jul 19, 2026 11:10am

Request Review

@vahid-ahmadi
vahid-ahmadi merged commit 5657a63 into main Jul 19, 2026
3 checks passed
@vahid-ahmadi
vahid-ahmadi deleted the fix/us-state-code-validation branch July 19, 2026 11:12
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.

1 participant