Belgian Companies under the hood: DCB event sourcing and reading the Staatsblad with AI

At the end of October we launched Belgian Companies at Tandem Studio: a free tool to look up any Belgian company by name, enterprise number or person. The launch post (in Dutch) covers what it does for you. This one is about how it works under the hood.

There are two things in it I think are worth writing up. Everything lives in an event store built on Dynamic Consistency Boundaries (DCB), running on my own Go implementation. And a large part of the data, who manages a company, isn’t behind any API, so we read it out of the Belgisch Staatsblad with AI models.

If you read my earlier posts on DCB for event sourcing and the Postgres benchmark, this is that store in production. I won’t explain DCB from scratch again, so read those first if the terms are new, or the Dutch overview if you’d rather skip the code.

Three sources, three gaps

We only use sources that allow commercial reuse. There are three of them, and by luck each one covers what the others miss.

SourceWhat it givesWhat it doesn’tHow we get it
KBO open data (FPS Economy)All ~1.96M enterprises: names, addresses, legal form, NACE activities, contacts, establishmentsDirectors, and any history: it is a snapshotA monthly full extract, then daily update zips
NBB Central Balance Sheet OfficeAnnual accounts with every heading, plus directors and shareholders as listed in the filingCompanies that don’t file (sole traders, most VOF/CommV); anything between two filings; structured JSON only since April 2022The NBB web services, per day and per company
Belgian Official Gazette (Staatsblad)Every appointment and resignation of every legal person, VOF and CommV includedStructure: each publication is a PDF in free prose, often a scanPublic PDFs, read with OCR and a language model

The gap that mattered to us is directors. The KBO does sell a reuse file with functions, but commercial use runs to €30,000 a year. The NBB tells you who sat on the board as of the accounts being filed, which is once a year and only for companies that file at all.

The Gazette has all of it, free to reuse, on the day it happens. The catch is that it’s written for humans: “de algemene vergadering aanvaardt het ontslag van … en benoemt tot bestuurder …”, in Dutch, French or German, and for most appointments a scanned form. There’s no API. We loaded the archive once and collect new publications day by day, around 1,000 to 1,700 per working day, of which a few hundred change who runs a company.

Every source records its own facts

One rule holds the data model together: events from different sources are never merged. A KBO address change is a KboAddressesRecorded. An NBB filing is NbbFilingPublished, NbbAccountsFiled, NbbDirectorsReported and so on. A Gazette publication is StaatsbladPublished, and what a model read in it is a separate StaatsbladDirectorsExtracted. Nothing overwrites anything.

That has already paid off with the KBO. Its open data is a snapshot without history, but because every daily update is stored as events, we now have the history the KBO itself doesn’t publish: when an address or a name changed, and what it was before.

Tags are what tie the sources together. Every event carries company:<enterprise number>, plus whatever else it is about: filing, publication, director, person. In my DCB library the tags are declared on the payload struct, the way json tags name fields, so an event can’t be built without its boundaries:

// NbbDirectorsReported records the directors a filing lists. Directors that
// are companies are tagged with their enterprise number, so a company's
// mandates elsewhere are one query away.
type NbbDirectorsReported struct {
	FilingRef // Company and Filing, tagged company:… and filing:…
	DirectorCompanies []string          `json:"directorCompanies,omitempty" dcb:"tag:director,omitempty"`
	People            []PersonDirector  `json:"people"`
	Companies         []CompanyDirector `json:"companies"`
}

func NewNbbDirectorsReported(data NbbDirectorsReported) (eventstore.Event, error) {
	return eventstore.NewEventFrom(TypeNbbDirectorsReported, data)
}

The director tag immediately shows why the tags are handy. A management company sitting on fifty boards gets tagged on every one of those events, so “where is this company a director?” is a tag query instead of a scan.

A company page is then a single query: every event tagged with that enterprise number, folded into a struct in memory. No company table, no joins across sources:

// CompanyDetails reads everything tagged with one enterprise number.
func CompanyDetails(number string) *eventstore.DefaultProjection[Company] {
	return &eventstore.DefaultProjection[Company]{
		Query: eventstore.QueryItem{Tags: eventstore.NewTags("company", number)},
		State: newCompany(number),
		Handlers: map[string]eventstore.HandleFunc[Company]{
			kbo.TypeKboEnterpriseRecorded: on(func(c Company, d kbo.KboEnterpriseRecorded) Company {
				c.Enterprise = &d
				return c
			}),
			nbb.TypeNbbAccountsFiled: on(func(c Company, d nbb.NbbAccountsFiled) Company {
				c.filing(d.Filing).Accounts = &d
				return c
			}),
			// … one handler per event type, from all four sources
		},
	}
}

The projection is built on demand and thrown away after the request. It’s the same projection type the write side uses for decisions, so there’s one way to fold events rather than two. And because each section of the page knows which events it came from, it can show its source and date, which the site does everywhere.

Append conditions do the boring work

In the DCB posts the example was always a unique username. Here, most append conditions guard something far less glamorous: imports have to be idempotent, and several jobs may run at the same time.

Take an NBB filing. All its events go in with one append, conditioned on “nothing with this filing tag exists yet”. If a parallel run got there first, the condition fails and we count the filing as skipped:

func filingQuery(reference string) eventstore.Query {
	return eventstore.Query{{Tags: eventstore.NewTags("filing", reference)}}
}

func (im *Importer) importFiling(ctx context.Context, ref nbb.Reference, filing *nbb.Filing) (Summary, error) {
	events, err := FilingEvents(ref, filing)
	if err != nil {
		return Summary{}, err
	}
	cond := &eventstore.AppendCondition{Query: filingQuery(ref.ReferenceNumber)}
	if err := im.Store.Append(ctx, events, cond); err != nil {
		if errors.Is(err, eventstore.ErrAppendConditionViolated) {
			return Summary{Skipped: 1}, nil // someone else imported it
		}
		return Summary{}, err
	}
	return Summary{Filings: 1, Events: len(events)}, nil
}

The same pattern shows up all over:

  • Gazette publications arrive from two places, the archive and our daily collection. StaatsbladPublished is appended with a condition on its publication tag, so a publication seen from both sources is recorded once.
  • KBO update files end with a KboFileImported marker, conditioned on that file’s tags. A second run on the same file fails loudly instead of applying it twice.
  • Accounts: one account per email address, the classic DCB uniqueness rule. The tag is an HMAC of the address rather than the address itself, which brings us to privacy further down.

None of this needed a stream design up front. Every time a new question came up (“was this publication already read with this model and this prompt?”), it turned out to be a new query over tags that were already there.

Reading the Staatsblad with AI

Getting from a Gazette PDF to “X joined as director on this date” takes three steps: pull out the text, have a model read it, then check the model’s answer against the text with plain code. That last step is where most of the work went.

Getting the text

Notarial deeds and many other publications are digital PDFs, so pdftotext does the job. The hard part is everything else: about 73% of BV/NV appointment and resignation deeds are scanned forms. OCR is where names get mangled, and a wrong name is the worst error this product can make.

I benchmarked a handful of engines on 200 deliberately hard scans and then 520 ordinary recent ones: Mistral OCR, tesseract, several vision models and a few local OCR models. Mistral OCR came out most accurate. Tesseract, properly tuned, got close enough to be the first pass:

  • render at 300 dpi and crop to the form’s frame;
  • clean the page (Sauvola threshold, remove the dotted frame, long lines and specks);
  • read with the tessdata_best models;
  • undo its l/I/1/t confusions in ordinary words (“besiuit” → “besluit”) using a vocabulary built from the Gazette’s own texts, never touching names.

A scan only goes to Mistral’s batch OCR when tesseract isn’t sure: a mean confidence under 85, or a person’s name that nobody in Belgium has while a name at least 20 people have is one OCR confusion away. That second check leans on Statbel’s first name and surname lists. On the 520 scans it sends 17% of the pages to Mistral, which works out to about €4.50 a month instead of €26. Deeds with a wrong name went from 0.2% (Mistral only) to 0.6%, and every misread person’s name was caught. The three that slipped through were company names.

The prompt

The model gets the text and a strict JSON schema. The system prompt is long, currently at version 23, and most of it is Belgian company law in disguise: a “stille vennoot” isn’t a manager, “ontslaan van de voorlezing” isn’t a resignation, a substitute director who only takes over when the current one dies isn’t an appointment. It opens with the rule that matters most:

Your answer feeds a public register, so a wrong entry is worse than a missing one: only report what the text states.

The order of the schema fields is deliberate, because the model writes them in that order. First a people list: everyone the text names and what it says about them. Then the changes, each with a verbatim evidence quote, the actionWords that state the change, and a confidence level for the person and for the change. Then managersAfter: who manages the company after this act, and whether the text says that list is complete. The idea is to make the model commit to who is in the text before it gets to decide who changed.

Very long deeds keep their first 15,000 characters (the parties) and their end (the decisions), and drop the middle of the statutes.

Checking the answer

A language model will happily return a name that isn’t in the text. So every entry goes through checks in plain Go before it is stored:

// The checks a small model needs: the name must really be in the text,
// and the publishing company cannot be its own director.
if c.PersonType == "legal" {
	if !companyInText(haystack, c.CompanyName) || sameCompany(c.CompanyName, in.CompanyName) {
		out.Dropped++
		continue
	}
} else if !strings.Contains(haystack, normalize(c.LastName)) {
	// A letter or two off ("Dhosche" for DHOSSCHE): take the text's spelling,
	// when it is the only such name and the first name is next to it.
	last, ok := nameInText(text, c.LastName, c.FirstName)
	if !ok {
		out.Dropped++
		continue
	}
	c.LastName, respelled = last, true
}

A silent partner gets dropped. A quote that isn’t in the text lowers the change’s confidence, and so does a first name the model spelled differently. The same person written twice (“Hai Pham”, “Hai Dang Pham”) becomes one entry with the fuller name. An answer cut off at the token limit gets one retry with 32,000 tokens instead of 8,000; cut off again, it’s recorded as given up and left alone.

The daily reading runs on DeepSeek V4.1 Flash, which matched Mistral Large 4 on our BV/NV test set at a fraction of the price. Mistral Large 4 handles the re-reads, founding deeds where the first reading found nobody, for instance.

Models and prompts were picked on hand-checked test sets. There’s also a chain where two models read the same publication and a third judges every entry they produce. Too expensive to run on everything, but it’s how a new model or prompt version gets scored.

A model’s reading is an interpretation, so it is its own event

The Gazette publication is a fact: StaatsbladPublished. What a model made of it isn’t, it’s an interpretation. So it gets an event of its own, and that event records how it was made:

// StaatsbladDirectorsExtracted records the director changes one language
// model read from a publication's text. It is an interpretation, not the act
// itself: the model, prompt version and a quote from the text are kept with
// every change, so results can be checked and a better model can be re-run later.
type StaatsbladDirectorsExtracted struct {
	PublicationRef                     // tagged company:… and publication:…
	DirectorCompanies []string         `json:"directorCompanies,omitempty" dcb:"tag:director,omitempty"`
	Persons           []string         `json:"persons,omitempty" dcb:"tag:person,omitempty"`
	Changes           []DirectorChange `json:"changes"`
	ManagersAfter     []Manager        `json:"managersAfter,omitempty"`
	Model             string           `json:"model"`
	PromptVersion     string           `json:"promptVersion"`
	TextSource        string           `json:"textSource"` // huggingface-text, pdf-text, ocr-tesseract, ocr-mistral
	Dropped           int              `json:"dropped,omitempty"` // answers rejected by the checks
	// … tokens, decision date, company status, home addresses
}

A few things follow from that.

Re-reading never destroys anything. A new prompt version or a better model appends a new reading next to the old one. Which readings the site uses is one function: the models we trust, at the current prompt version. Older readings stay in the store and count for nothing. When DeepSeek returned empty readings for a batch of founding deeds, we re-read them with Mistral Large 4; 149 of 265 then named their directors, and the pages updated without a migration.

Each entry points back to its source. Every change carries the quote it was based on, and the event records whether the text came from a digital PDF, tesseract or Mistral. On the site, AI-read information is labelled as such and links to the original publication.

The bills are events as well. Every reading appends a StaatsbladReadingBilled with what it cost, either the price the provider reported or one computed from the tokens, and every Mistral OCR call gets a StaatsbladOCRBilled. The admin page with costs per day is just another projection.

Giving up is recorded too. A publication with no usable text, or one cut off twice, gets a StaatsbladReadingFailed, so the next nightly run skips it instead of paying for it again.

The last step is working out who runs a company now. A single publication only tells you what changed in that one act. ComposeBoard walks a company’s readings from newest to oldest: everyone in office after the newest act, plus those appointed earlier and never seen leaving. It stops at the first reading whose text says its list of managers is complete (“de enige zaakvoerder is …”), or at the founding deed. Someone who left in a newer publication is never brought back by an older one, and once a company is dissolved only liquidators remain.

GDPR in an append-only store

Director data is personal data, and Gazette deeds regularly contain a director’s home address. “We never delete events” doesn’t hold up against a request to be forgotten.

Crypto-shredding is the way out, and my DCB library supports it per event. Natural persons get a person tag (a hash of the name). The home addresses sit in their own top-level field, and that field is encrypted with a key belonging to those person tags:

func NewStaatsbladDirectorsExtracted(data StaatsbladDirectorsExtracted) (eventstore.Event, error) {
	var opts []eventstore.EventOption
	if len(data.HomeAddresses) > 0 && len(data.Persons) > 0 {
		opts = append(opts,
			encryption.WithEncryptionKeys(TagPerson),
			encryption.WithEncryptedFields("homeAddresses"),
		)
	} else {
		data.HomeAddresses = nil // never store an address without a subject to forget it by
	}
	return eventstore.NewEventFrom(TypeStaatsbladDirectorsExtracted, data, opts...)
}

Forgetting a person deletes their key. The events stay, the sequence stays intact, projections still replay, but the addresses can no longer be decrypted. Names and municipalities remain, since those are what the Gazette publishes and what the product is about.

The same idea protects our own users. The email address in AccountRegistered is encrypted per user, and the uniqueness check from earlier runs on an HMAC of the address, so it works without ever querying the plain one.

The read side

Folding events per request is fine for one company page. It doesn’t work for “find every BV in Limburg with more than 10 FTE”. That needs persistent projections, which my library calls CheckpointProjection: they remember their position in the event store and catch up from there.

  • company_search holds one row per enterprise with its names, seat, legal form and status. A pg_trgm trigram index on all names folded together lets typos and partial names match. It processes 5,000 events per transaction and catches up every 30 seconds. Bumping its version name rebuilds it from scratch; the first full build took 22 minutes.
  • company_facts holds what the advanced search filters on: location, NACE codes, legal form, founding year and the figures from the latest annual account.

Both tables are disposable. If one is wrong, I fix the projection and rebuild it from the events, which is the main reason to do event sourcing in the first place.

There’s an MCP server as well, so Claude, ChatGPT or Le Chat can look up companies and people for you.

Looking back

DCB paid off, though mostly in different places than I expected. The cross-entity invariants I wrote about before barely come up. What I use every day is that tags make every source queryable from every angle (company, filing, publication, director, person), that append conditions make imports safe to rerun, and that a model’s reading is just another event I can replace without losing the old one.

The AI part turned out to be less about the model than about everything around it: getting clean text out of scans, a prompt that encodes the law, checks that don’t trust the answer, and recording enough to know later what was read with what.

Try it at yourself belgiancompanies.be. Always interested to hear your feedback!

Leave a comment