Databricks Tutorials

What Is a Charity? One Ontology for Five National Registers, and a Genie Agent for Donors

From Genie Code Prototype to Production Agent on Databricks — Episode 5 of 10 ·

In Episode 4 we loaded five national charity registers into one table: 1,981,654 organisations from England and Wales, Canada, Australia, the US and Poland. We also saw the catch. Those rows don't mean the same thing. A registered charity in England is not a US 501(c)(3) exemption, and in Poland there is no legal "charity" at all.

If we hand that table to an AI assistant as it is, it will happily add pounds to dollars, compare Polish "zero income" with British income, and call all of it "charities". So in this episode we give the data meaning before we let anyone ask questions:

Everything runs on Databricks Free Edition, and every table comes from the E4 pipeline. All code is in the repo folder e05-charity-ontology/.

Clip 1: five registers, five meanings of "charity", written down as a table.

Why an ontology, and why now

Databricks made Genie Ontology generally available on 11 November 2026. The idea is simple: Genie combines learned knowledge (snippets it picks up from your queries, dashboards and Genie Agents) with modeled knowledge that you define and govern in Unity Catalog semantics: metric views, domains, Pages and certification. When the two disagree, the modeled knowledge wins.

For Charity Lens that matters a lot. The learned version of "charity income" would be whatever query someone ran last. The modeled version says: local currency, income above zero only, and here is what "charity" means in each country. We want Genie to use the second one.

Our ontology is deliberately small:

Entity Table One row per
Jurisdiction ont_jurisdiction register (GB-EAW, CA, AU, US, PL), with what "charity" means there
Charity ont_charity organisation, with a normalised charity_kind
Finances ont_charity_finances organisation, latest period, local currency
Cause ont_cause + ont_cause_crosswalk Charity Lens cause, and each register's code mapped to it
Programme country ont_country + ont_charity_programme_area ISO country, and where each organisation says it works

Step 1: Write down what "charity" means

The most important table has five rows. jurisdictions.csv lives in the repo, and we upload it to the volume next to the E4 files, together with causes.csv and country_map.csv. For each register it records the regulator, the legal basis, what one row means, the currency, and which data simply don't exist (Poland has no open financial data, the US and Poland have no programme countries in our files).

We give Genie Code a new skill, charity-lens-ontology, with the entity list, the rules and the table names, and start in the SQL editor:

@charity-lens-ontology Read /Volumes/workspace/charity_lens/raw/ref/jurisdictions.csv and causes.csv and create ont_jurisdiction, ont_cause and ont_cause_crosswalk as the skill says. Show me the SQL first. After the run, show jurisdiction, charity_concept, currency, has_income and charity_definition from ont_jurisdiction.

Genie Code loaded the skill, read the two files and showed the SQL first, as asked: three tables, their keys and the result query. We replied "run all steps as shown, but stop if the checks find unmapped causes or orphans". It asked before each step (twice with a safety prompt, because CREATE OR REPLACE would overwrite a table), checked that every key was unique (5/5, 22/22, 95/95) with zero orphans and zero unmapped causes, and only then added the keys. It took about 12 minutes, with no fixes and no fallback.

jurisdiction charity_concept currency has_income
AU registered_charity AUD true
CA registered_charity CAD true
GB-EAW registered_charity GBP true
PL public_benefit_status PLN false
US tax_exempt_organisation USD true

(The live result from ont_jurisdiction, which matches the reference file. The charity_definition column is in the repo CSV.)

The cause vocabulary gets the same treatment: the E4 v0 mapping becomes ont_cause (22 causes with a display name, a description, a coarser cause group and synonyms like "food banks" or "veterans") and a keyed crosswalk from each register's own codes.

Step 2: Charity is an attribute, not an assumption

Next, the entities that hold organisations. Instead of one more wide table, we split E4's gold_charities into a few narrow ones and connect them with keys:

@charity-lens-ontology Create ont_charity, ont_charity_finances and ont_charity_cause from gold_charities with the rules from the skill. Then add the primary keys and foreign keys in two phases: primary keys first, then foreign keys. Before adding keys, prove each primary key is unique and that no foreign key has orphans. Show the SQL before each step.

Three design choices do most of the work:

Primary keys get RELY, which tells the optimizer it can trust them. That's a promise, so we check it first. In Unity Catalog, primary and foreign keys are informational and not enforced, so the uniqueness and orphan checks are ours to run:

check result
duplicate primary keys 0 (every key checked before it was added)
orphan foreign keys 0 on all four foreign keys
ont_charity rows vs gold_charities 1,981,654 = 1,981,654

Genie Code kept to the two phases. First it proved the keys: 1,981,654 unique charity_ids in ont_charity and in ont_charity_finances, 1,881,506 distinct rows in ont_charity_cause, and no orphans. Then it added the three primary keys (pk_ont_charity, pk_ont_charity_finances, pk_ont_charity_cause), and after them the four foreign keys (fk_charity_jurisdiction, fk_finances_charity, fk_charity_cause_charity, fk_charity_cause_cause). Parent tables came first, so the E3 ordering problem never came up. It took about ten minutes, again without the fallback. Catalog Explorer then shows charity_id as PK and jurisdiction as FK, and View relationships draws the link to ont_jurisdiction.

Step 3: Three ways to name a country

Donors often ask "who works in Kenya?". Three of our registers can answer, each in its own format:

country_map.csv maps every value we found in the E4 files to ISO 3166-1 alpha-2: 707 rows, built with a country library plus a short manual list (the emirates map to AE, Scotland and Northern Ireland to GB, Kosovo to the widely used XK). What can't be a country stays visible as region or unmapped. The UK Sovereign Base Areas on Cyprus have no ISO code, the Netherlands Antilles no longer exist, and two Canadian codes aren't in the CRA guide.

@charity-lens-ontology Load raw/ref/country_map.csv as ont_country_map, build ont_country, then build ont_charity_programme_area as the skill says, with its keys. Show me, per jurisdiction, how many area codes are countries, regions and unmapped, and list every unmapped or region value with its organisation count. Don't guess any mapping.
jurisdiction country codes region codes unmapped codes organisations working in a mapped country*
AU 236 0 0 44,644
CA 188 6 2 3,356
GB-EAW 249 0 3 24,702

* The code counts come from the live run. The organisation counts come from our rehearsal run of the same mapping on the same data, where the code counts were identical.

Genie Code didn't guess a single mapping. The live run built 707 map rows, 250 countries and 183,567 programme-area rows, and listed everything that isn't a country. In Canada those are the CRA regions QM (101 organisations), QN (21), QO (31), QP (165), QR (163) and QS (319), plus the two unknown codes QZ (1) and XZ (3). In England and Wales they are Akrotiri (126), Dhekelia (110) and the Netherlands Antilles (19). Scotland and Northern Ireland became GB, which covers 4,248 charities. Like Prompt 2, this one went through first time and needed no fallback.

Step 4: Metric views that refuse to add pounds to złoty

Now the definitions that must be exact. As in Episode 3, metric views hold them: one YAML definition per subject, queried with MEASURE(), and the same numbers in SQL, dashboards and Genie. We create three: mv_charity_finance, mv_charity_causes and mv_programme_reach (full YAML in fallback/e05_4_metric_views.sql).

The interesting part is a three-line guard. Every money measure only returns a value when the result covers exactly one currency:

  - name: median_income_local
    expr: CASE WHEN COUNT(DISTINCT source.currency) = 1 THEN percentile_approx(CASE WHEN source.income > 0 THEN source.income END, 0.5) END
    display_name: Median income (local currency)
    synonyms: ['typical income', 'median revenue']

Grouped by jurisdiction, you get five medians in five currencies. Asked for "the median across all five countries", the metric view returns NULL instead of a confident wrong number. Genie inherits that behaviour because it uses the same definition.

@charity-lens-ontology Create mv_charity_finance, mv_charity_causes and mv_programme_reach exactly as the skill describes, with display names, synonyms and tags. Then run two queries: median_income_local and organisations per jurisdiction, and the same measures with no GROUP BY. Explain in one sentence why the second query returns NULL for money.
jurisdiction currency organisations median_income_local
AU AUD 65,907 152,435
CA CAD 83,761 168,939
GB-EAW GBP 171,914 26,116
PL PLN 9,604 NULL (no income data)
US USD 1,650,468 207,173.5
(all five, no GROUP BY) – 1,981,654 NULL

The counts match E4 exactly. The medians are close but not identical:

jurisdiction metric view (Genie Code) E4 gold_jurisdiction_summary difference
AU 152,435 152,396 +0.03%
CA 168,939 169,028 −0.05%
GB-EAW 26,116 26,108 +0.03%
US 207,173.5 207,104 +0.03%

Both use the same rule (income above zero only) and the same organisations: the "with income" counts are identical (41,779, 78,905, 150,422 and 444,574). The difference is the median function. E4's gold table, and our fallback YAML above, use percentile_approx, which is an approximate median. In rehearsal the fallback metric view gave exactly the E4 numbers. The skill only says "median over income > 0" and doesn't name a function, so Genie Code picked its own. We didn't capture its YAML on screen, but the results give it away: they come back as decimals (152,435.0), and the US median ends in .5. US incomes are whole dollars and 444,574 is an even count, so that .5 is the average of the two middle values. An exact median does that; percentile_approx always returns a value that is actually in the data. So the metric view almost certainly uses an exact median, and E4 the approximate one. To confirm, run SHOW CREATE TABLE workspace.charity_lens.mv_charity_finance. To avoid it, name the function in the skill. We'll do that in v1, and we'll use the exact median in both places.

The cause shares agree with E4 too. In Step 7 the agent counted 83,795 education charities in England and Wales out of 171,914. That's 48.7%, the same share as in E4.

Genie Code got the YAML through without the fallback, but not first time, and this is the clip with the most retries (sped up in the video). mv_charity_finance took three attempts. The first had an unresolved jurisdiction reference. The second hit an alias collision: a join named jurisdiction clashed with the column of the same name, so it renamed the join to register. mv_charity_causes also took three attempts. The SQL editor's statement splitter cut the YAML at a ; inside a comment, and then the same alias collision came back, fixed with cause_lookup. mv_programme_reach went through first time. The tags needed about five attempts before quoted tag keys worked. Halfway through, the chat stalled and we had to queue a follow-up prompt. In total it took about 16 minutes. Genie Code saved the lessons (quoted tag keys, no semicolons in YAML, metric view DDL on the warehouse only) to its memory for next time.

Step 5: Teach Genie the definitions

Metric views are already modeled knowledge. We add three more signals:

The Page text is in prompts/pages.md if you want to reuse it.

Step 6: A Genie Agent for donors

Genie spaces are now called Genie Agents. An agent needs a SQL warehouse and can hold up to 50 tables, views or metric views. Databricks recommends starting with five or fewer, and that's what we do: the three metric views, ont_charity (to list organisations) and ont_jurisdiction (for the definitions).

When you create an agent, Genie Code opens and suggests context. We asked it to build the whole thing:

@charity-lens-ontology Create a Genie agent "Charity Lens: donor questions" for people who want to compare charities across the UK (England and Wales), Canada, Australia, the US and Poland. Use only mv_charity_finance, mv_charity_causes, mv_programme_reach, ont_charity and ont_jurisdiction. Add the instructions from the skill, and the example questions from genie_agent_setup.md. Show me the instructions before you save.

The instructions are short on purpose. Databricks' own guidance is to prefer metric views and example SQL over long text. The rules that matter: name the currency and never convert it, say what "charity" means when comparing countries, and say "no data" instead of showing zero. The full setup, six example queries and the benchmark questions are in prompts/genie_agent_setup.md.

Genie Code created the agent and attached the five data objects. Then it tried to add the rest through the Genie spaces API, and that PATCH call failed twice. So we finished the agent by hand from genie_agent_setup.md: the description, the instructions (the editor was empty), five example SQL queries and four common questions. We left out the Ukraine example because it lists organisation names, and on camera we show aggregates only. Genie added a fifth common question tile on its own: "What tables are there and how are they connected?". Lesson: when an assistant writes configuration through an API, check what actually arrived.

Step 7: Donor questions

Three questions in chat mode, chosen to test the guard rails:

  1. "How big is a typical charity in each country?" Five medians, each labelled with its own currency: AUD 152,435, CAD 168,939, GBP 26,116 and USD 207,173.5. For each register it also showed how many organisations there are and how many report income. Poland: no reported income, so no number. These are the metric view's figures, so the agent and the SQL agree.
  2. "What is the total income of all charities in all five countries?" It gave no grand total. Instead there is one total per currency, over organisations with positive income: AUD 239.9 billion, CAD 412.5 billion, GBP 109.2 billion and USD 3,655.9 billion. It noted that Poland has no data. This is the answer we wanted: the currency guard did its job.
  3. "I care about animals. Where are animal charities most common, and what does 'charity' mean in each of those countries?" England and Wales 2.7% of the register (4,623 charities), the US 2.5% (40,888), Australia 1.9% (1,238), Canada 1.2% (1,037) and Poland 0.06%, which the table rounds to 0.1%. It then gave a definition for all five, straight from ont_jurisdiction, including "Poland: no legal category called charity".

Then one question for Agent mode, which plans, runs several SQL queries and writes a report with citations:

"I want to support education projects in Kenya through a charity registered in the UK, Canada or Australia. Which registers have the most organisations working in Kenya, how many of them list education as a cause, and what should I keep in mind when comparing their income?"

Agent mode works on Free Edition. It planned four steps (organisations working in Kenya by register, organisations with an education cause, definitions and comparability notes, financial overview), ran them and wrote a report with charts and four cited sources. In the recorded run that took about a minute; our first run took about 1 minute 40 seconds. The Kenya numbers are right: England and Wales 2,824 organisations, Canada 347, Australia 242. The comparison notes are useful too. They point out the different definitions, the different income bases, and how many organisations report income at all (87.5% in England and Wales, 94.2% in Canada, 63.4% in Australia).

The education numbers are where it falls short. The report gives 83,795 education charities for England and Wales, 13,633 for Australia and 10,605 for Canada. Those are counts for the whole register, not for organisations working in Kenya. In an earlier run Genie said so plainly: it can't combine cause with programme country. The reason is in our model. Cause lives in mv_charity_causes (one row per organisation and cause) and programme country in mv_programme_reach (one row per organisation and country). The two metric views have different grains, neither has the other's dimension, and the bridge tables behind them (ont_charity_cause, ont_charity_programme_area) aren't agent sources. So "education and Kenya" can't be expressed. The fix is to model that question. For example, add a view with one row per organisation, programme country and cause, put a metric view on it (COUNT(DISTINCT charity_id) by cause and country), add it as the agent's sixth source and give it an example SQL. That's on the list for E6.

Finally we turned six questions into benchmarks, each with the expected behaviour (one total per currency, "no data" for Polish income) rather than a number. One run in Agent mode took about four and a half minutes and scored 83% (5 of 6). The one question flagged for manual review was "What is the median income in Canada?", with two score reasons: Empty Ground Truth SQL and Filter Issue. Both are on us. We wrote behaviour notes instead of ground-truth SQL, so the judge had nothing to compare with. And because the answer's SQL had no explicit income > 0 filter, the judge flagged it. The answer itself is correct: CAD 168,939 over the 78,905 organisations with positive income. The income > 0 rule lives inside the median_income_local measure, where the judge can't see it. Next time we'll add ground-truth SQL to the numeric benchmarks.

What we learned

Licences and attribution

Same sources as Episode 4, so the same attributions apply:

CRA region codes come from the CRA guide T4033 (Completing Form T3010). Country codes follow ISO 3166-1.

Charity Lens is an independent project, not endorsed by any of these publishers. Information, not financial or legal advice.

Next

Episode 6: annual reports as data. We turn charity report PDFs into searchable text with document processing and AI Search, and build a code-first agent that uses the same ontology through the Genie One MCP server.

All code, prompts and the skill are in the repo folder e05-charity-ontology/.