usda-nass
by xippios
README.md
# xippios-usda-nass
Typed Python client, MCP server and FastAPI routes for
[USDA NASS Quick Stats](https://quickstats.nass.usda.gov), part of the
[xippios SDK family](https://github.com/xippios). Names and semantics follow the
upstream documentation; every response carries its metadata (source, endpoint,
retrieved_at, request).
## Scope
Of Quick Stats' API the package wraps the three operations that its documentation lists
(`docs/upstream/`): `api_GET`, the published estimates for a set of filters, read as JSON,
`get_counts`, the count of the same filters, and `get_param_values`, the values that a
parameter takes, of which the package ships ten lists and refreshes them before each
release. A filter is any column of the column definitions, with any of the documented
operators. A documented choice that a caller cannot make stands here with its public issue:
- `format` and `callback` of `api_GET`: the documentation offers CSV and XML beside JSON, and
JSONP by a callback, and the package asks for JSON
(https://github.com/xippios/sdk-usda-nass/issues/65).
- The Data Set Download files, which the documentation names as the way past 50,000 records
beside narrower filters: the package reads no file, and refuses a query over the limit
with the filters that narrow it and the split by year that serves a range
(https://github.com/xippios/sdk-usda-nass/issues/66).
- A second filter on one column, whatever its operator, in the filters of `count_data`,
`get_data` and `list_param_values`: the documentation offers each of its operators on any
column and does not limit a column to one, and the package refuses two keys on one column
before a request. The recordings show Quick Stats keeping one of two bounds on `year` and
on `week_ending`, so that it answers for another range than the one asked for
(`docs/upstream/OBSERVED.md`, "One bound on year a query" and "Two bounds on
week_ending"), and `get_data` with `split_by_year=True` takes `year__GE` with `year__LE`
and fetches that range year by year. The other pairs, such as two bounds on `load_time` or
`__LIKE` with `__NOT_LIKE` on `short_desc`, are refused by the package's rule alone, since
no recording shows what Quick Stats does with them
(https://github.com/xippios/sdk-usda-nass/issues/45).
What the package adds is the list of the columns that a filter can name, from the page of
column definitions, the count before a query, the check of each filter against the packaged
column table and lists before a request, and a split by year where one call cannot serve a
range. It fixes no filter, ranks nothing and derives no figure for the caller: the thirteen
tasks of 0.3.0 that fixed filters over `api_GET` left in 0.4.0, and the query of each stays
one `get_data` call with the same filters.
## Install
```bash
uv add xippios-usda-nass # client only
uv add "xippios-usda-nass[mcp]" # + MCP server
uv add "xippios-usda-nass[http]" # + FastAPI router
```
Set `NASS_API_KEY` in the environment. Nothing is read from files.
## 30-second example
```python
from xippios.usda.nass import tasks
filters = {
"commodity_desc": "CORN",
"statisticcat_desc": "AREA PLANTED",
"agg_level_desc": "STATE",
"state_alpha": "VA",
"year": 2012,
}
tasks.count_data(filters).data # 2 records would come back from api_GET
records = tasks.get_data(filters).data
records[0].short_desc # 'CORN - ACRES PLANTED'
records[0].value, records[0].value_number # '510,000', Decimal('510000'); JSON: 510000
tasks.list_param_values("sector_desc").data # ['ANIMALS & PRODUCTS', 'CROPS', ...]
tasks.list_param_values("class_desc", filters={"commodity_desc": "CATTLE"}).data
# ['(EXCL CALVES)', ..., 'INCL CALVES', ...]: the 48 classes of cattle
tasks.list_columns().data[3].column # 'commodity_desc (Commodity)', as upstream's page prints it
tasks.list_columns().data[3].column_label # 'commodity_desc', the name a filter takes; 39 in all
```
`get_data` counts the records first: zero matches return an empty result, more than
50,000 raise `QueryTooLarge` with the count and a hint, narrower filters or a split by
year, before anything is fetched. `list_columns` lists the 39 columns that a filter can
name, each with its dimension, its maximum length and its definition as upstream's page
prints them and with the name that a filter takes beside it, from the package and without a
request. `list_param_values` lists the values a filter can take, ten of the lists
packaged and every other fetched live, and refuses a name that is no column before a
request. With `filters`, which it checks as `count_data` checks them, it lists the values
that Quick Stats gives beside them, fetched live for every column, and the empty list where
no record matches (`docs/upstream/OBSERVED.md`, "Filters narrow a list of values"). The
client keeps a count and the data for 300 seconds and a list fetched live for a
day, so the same call within that time sends nothing, and a year that Quick Stats publishes
within those 300 seconds reads as none until they pass.
`get_data(filters, split_by_year=True)` fetches the years one at a time where one call
cannot serve them, when its filters bound the years as `year`, `year__GE` with
`year__LE`, or `year__GE` alone up to the current UTC year; any other year filter, and a
`year__GE` or `year__LE` past the current UTC year, is refused with
`InvalidRequestError` before a call. Quick Stats keeps one of two bounds on `year`: it
counts `year__GE` with `year__LE` as `year__GE` alone, and drops `year__GE` beside
`year__LT` (`docs/upstream/OBSERVED.md`, "One bound on year a query"), so `count_data` and
`get_data` without the split refuse two filters on `year` before a call, and a two-sided year
range is served only by this split. That form is always split, every year counted before anything is fetched; `year`
is counted and fetched once, and `year__GE` alone is counted first and split only over
the limit. One call splits into at most 20 years in Python and over HTTP, and 11 by
default on the MCP server, whose `--split-years` sets 1 to 20: a longer split raises
`InvalidRequestError` naming the ceiling, for a two-sided range before any call, for
`year__GE` alone after its count, while an open range within the limit is fetched whole
however far back it starts. A year counting 0 is passed over: it has no records, or none
published yet, which Quick Stats does not tell apart, so one note of `meta.notes` names
the years passed over, and `meta.vintage` stays `None`, since Quick Stats names no
release. A year over 50,000 on its own still raises `QueryTooLarge` naming it, and the
records come in year order. A split sums no yearly counts: `meta.total` stays `None` for a
two-sided range, which Quick Stats counts as `year__GE` alone, and keeps upstream's count
of the range for `year__GE` alone. A split costs up to two keyed calls per year. An answer
of one `year` without records says in its note that Quick Stats counted none, the count it
also gives a year not yet published.
Each record carries every column of Quick Stats under its upstream name; `value` stays
the published string and `value_number` its parsed form, and so do `cv_percent` and
`cv_percent_number`. In place of a figure Quick Stats prints a code, such as `(D)` for a
figure withheld to avoid disclosing data for individual operations: an answer of `get_data`
says in `meta.notes` what each code that its records print means, in the words of NASS's
[Quick Stats Glossary](https://quickstats.nass.usda.gov/src/glossary.pdf), with the
records that print it, and names a code that the glossary does not define.
`models.VALUE_CODES` holds the eight codes of the glossary. `meta.published_at` stays
empty: Quick Stats publishes no release time and each row's `load_time` carries no zone,
so nothing is read into it.
Filters are Quick Stats column names, optionally suffixed with an operator (`__GE`,
`__LE`, `__LIKE`, ...), mapped to values; `list_columns` lists the columns, and its
description the seven operators. Quick Stats ignores unknown column names, so the
package checks every key against its column table and raises `InvalidRequestError`,
naming the closest valid names, before anything is sent; it checks the value of a
packaged parameter (`commodity_desc`, `state_alpha`, `unit_desc`, ...) against its packaged
list too and refuses an unknown one with `UnknownCode` before the call, `CORN` named for
`CORNN`, since Quick Stats counts no records for it, and an integer for one with
`InvalidRequestError`, since its list holds text. The value of a packaged parameter given
with an operator, such as `commodity_desc__LIKE`, is not checked against its list, the way to
a value newer than the list. One column takes one filter: two keys on one column, whatever
their operators, are refused with `InvalidRequestError` before a call, those on `year` only
where `get_data` does not split, as `## Scope` says. Quick Stats keeps one of two bounds on
`year` and one of two on `week_ending` (`docs/upstream/OBSERVED.md`, "One bound on year a
query" and "Two bounds on week_ending"), and every other pair is refused by the package's
rule alone. A `year`, bare or with a
comparison, takes a whole year, `2012` or `"2012"`: Quick Stats answers `year=20x2` with
HTTP 500 and the page of an outage (`docs/upstream/OBSERVED.md`, "A malformed year: HTTP
500"), which a caller cannot tell from an outage, so another value is refused with
`InvalidRequestError` before a call. A key with `__` and no operator after it is
refused as an unknown operator is.
## MCP
```json
{
"mcpServers": {
"usda-nass": {
"command": "uvx",
"args": ["--from", "xippios-usda-nass[mcp]", "xippios-usda-nass-mcp"],
"env": { "NASS_API_KEY": "..." }
}
}
}
```
Tools are read-only and named `usda_nass_<verb>_<noun>`. Results are capped, at
200 rows or 64 KB unless a tool has a cap of its own, with a note on how to see the rest:
`usda_nass_list_param_values` returns up to 1,000 values, since its lists run longer than
200 (`docs/upstream/OBSERVED.md`, "The lists' lengths"), takes `filters`, which narrow a
list and which its note names, and takes
`offset`, the values already shown, which continues a cut list from the package or the
client's cache without a request. `usda_nass_get_data` keeps 200 rows or 64 KB, and its
note names the filters that narrow a query; it takes no `offset`, since a continuation
would send its count and its data again once an answer passes the cache's 8 MiB. `--http`
serves streamable HTTP instead of stdio. `--split-years YEARS`, 1 to 20 and 11 by default,
is the most years one `usda_nass_get_data` call with `split_by_year` splits into, at most
23 keyed requests in 92 attempts at 11; the setting holds for the whole process.
## Status
<!-- Core's family check (tests/test_family.py) reads this table: keep the "## Status"
heading and the "Task function" column. Each row's Task function cell starts with the
task's name in backticks, alone or as its call (`get_data(code, year)`); several tasks in
one row are backticked names separated by commas; prose may follow. The tables list
exactly tasks.__all__, and tasks_write.__all__ where the package has write tasks. -->
| Upstream operation | Task function | MCP tool | Status |
| --- | --- | --- | --- |
| `get_counts` | `tasks.count_data(filters)` | `usda_nass_count_data` | alpha |
| `api_GET` | `tasks.get_data(filters, split_by_year=)` | `usda_nass_get_data` | alpha |
| none: the page [`param_define`](https://quickstats.nass.usda.gov/param_define) of the documentation, packaged | `tasks.list_columns()` | `usda_nass_list_columns` | alpha |
| `get_param_values` | `tasks.list_param_values(param, filters=)` | `usda_nass_list_param_values` | alpha |
0.5.0 serves four tasks: the three over the operations that `## Scope` names, and
`list_columns`, which answers from the packaged page `param_define` without a request. 0.5.0
requires xippios-core 0.5.x (`~=0.5.0`), as 0.4.0 did, and 0.3.0 does not work with core
0.5.0. Alpha: the public surface may change in a minor version before 1.0 (conventions §12);
`CHANGELOG.md` lists every change.
## Upstream terms and attribution
Data comes from USDA NASS Quick Stats (https://quickstats.nass.usda.gov) under its own terms of use;
this package adds no rights to it. Documentation snapshot: `docs/upstream/`.
The NASS API Terms of Service ask every service that uses the API to display this notice:
> This product uses the NASS API but is not endorsed or certified by NASS.
## Workflows
`ci.yml` runs ruff, its format check, mypy, pytest and pre-commit on Python 3.12, 3.13 and
3.14, requires a changelog entry of every pull request but Dependabot's and refuses a
fixture file over 256 KiB that a pull request adds or changes. It runs on every pull request and,
called by `publish.yml`, once for a release, and its job `ci` is the check the
organization's ruleset requires. It is handed no secret, and no variable switches it off.
`publish.yml` is the release train (conventions §12): on a push to `main` whose
`pyproject.toml` names a version that has no tag yet and has its section in `CHANGELOG.md`,
it runs `ci.yml`, refuses a core pinned to git, tags the merge commit, builds the package
and, once a reviewer of the environment `pypi` approves the run, publishes it to PyPI by
trusted publishing. Every push to `main` and a dispatch start it, and a push that brings no
release ends quietly. It is handed no secret: the step that tells a release is handed the
job's token as `GH_TOKEN`, the tag goes with the job's token and PyPI trusts the workflow
itself. Its job `tag` holds the one checkout of the repository that keeps its credentials,
since it pushes the tag with the job's token; every other checkout sets
`persist-credentials: false` (conventions §13). No variable switches it off.
`live.yml` runs every `live` test against the upstream with `--tb=short --assert=plain`, so
its public log holds neither a failing test's arguments nor the values an assertion compared.
It runs every night at 01:51 UTC and on demand, never on a pull request.
Its pytest step alone is handed `XIPPIOS_LIVE_TESTS` and, each from the secret of the same
name, `NASS_API_KEY`, so the checkout, setup-uv and `uv sync` see none.
A variable `WORKFLOW_LIVE` of `off`, the repository's over the organization's, switches it off.
`conventions.yml` calls the organization's review of a pull request against the documents
listed under Documents below, the check `conventions / conventions`, when a pull request
opens, is pushed to, reopens or is edited, and on a push to `main` or a dispatch the review
of the range of commits, `range / range`. It hands the review the organization's
secret of the model's key, by name. A variable `WORKFLOW_CONVENTIONS` of `off`, the
repository's over the organization's, switches both off.
`adr-index.yml` calls the organization's check of the ADR index, `adr-index / adr-index`,
which runs `scripts/adr_index.py --check`, when a pull request opens, is pushed to or
reopens. It hands over nothing, and the called workflow reads its switch itself: a variable
`WORKFLOW_ADR_INDEX` of `off`, the repository's over the organization's, skips the check.
`siblings.yml` runs `.github/scripts/check-siblings.sh`, which installs the wheel beside
every sibling on PyPI under the same provider namespace that shares its core minor and
beside the source packages it depends on, and proves that the namespace levels stay portions
(sdk-core ADR-0006, sdk-core ADR-0021). It runs on every pull request and push to `main`, a
check the ruleset does not require, in a workflow of its own, since `ci.yml` is core's file,
which `publish.yml` calls for a release. It hands the script the job's read-only token as
`GITHUB_TOKEN`, for the listing of the organization's repositories, since GitHub limits the
calls of a runner without one. A variable `WORKFLOW_SIBLINGS` of `off`, the repository's
over the organization's, switches it off.
## Documents
- sdk-core: [docs/conventions.md](https://github.com/xippios/sdk-core/blob/main/docs/conventions.md) — the conventions every xippios source package follows: its layout, names, results, tests and releases.
- sdk-core: [docs/adr.md](https://github.com/xippios/sdk-core/blob/main/docs/adr.md) — the index of the family's architecture decisions, which the conventions cite.
- sdk-core: [docs/definition-of-done.md](https://github.com/xippios/sdk-core/blob/main/docs/definition-of-done.md) — what a change satisfies before its pull request is opened.
- sdk-core: [docs/review-checklist.md](https://github.com/xippios/sdk-core/blob/main/docs/review-checklist.md) — what the review of a pull request checks, in order.
- sdk-usda-nass: [docs/adr.md](https://github.com/xippios/sdk-usda-nass/blob/main/docs/adr.md) — the index of this package's own decisions, generated from the records in `docs/adr/`.
- sdk-usda-nass: [docs/upstream/FETCHED.md](https://github.com/xippios/sdk-usda-nass/blob/main/docs/upstream/FETCHED.md) — the snapshot of the upstream documentation, the reference for names, parameters and semantics.
- sdk-usda-nass: [docs/upstream/OBSERVED.md](https://github.com/xippios/sdk-usda-nass/blob/main/docs/upstream/OBSERVED.md) — what Quick Stats does that its documentation does not state, each note with the probe that showed it.
## Licence
Apache-2.0. See [LICENSE](https://github.com/xippios/sdk-usda-nass/blob/main/LICENSE).
This server cannot be deployed
Maintenance
ActivityActive
ResponsivenessLimited data