Inspiration
The documents people most need help with are the ones they should least paste into a chatbot. A hospital discharge note for an aging parent, a three-day "pay rent or quit" notice, an insurance denial for an MRI: all are written in jargon, all come with deadlines, and all are full of names, dates of birth, medical record numbers, addresses and policy numbers.
Today people choose between understanding the document (and handing their identity to a cloud model) and staying private (and staying confused). Caregivers, tenants and patients are the people least able to afford either mistake. Hybrid inference let us drop that trade-off: the device does the private part, the cloud does the hard part, and the user can see the boundary between them.
What it does
Veil is a web app that explains official documents without your identity leaving your device.
- Shield (on device). Paste a document or scan a paper letter. Deterministic detectors (labelled IDs, Luhn-checked card numbers, phones, emails, addresses, honorific names) and Gemini Nano with a JSON schema (
ONLY_ON_DEVICE) find every identifier. Each unique value becomes a stable placeholder such as[PERSON_1]or[ID_1], and surnames are linked to full names. - Consent view. You see your text with every detection highlighted, next to exactly what the cloud would see. One click masks or releases any value.
- Ask. Summarize, "What do I need to do?" (a deadline checklist), "Explain the jargon", "Draft a reply", or a free question, answered in English, Spanish or Japanese. Before you run anything, Veil shows the route it will take and why.
- Restore (on device). Placeholders in the answer are swapped back for the real values on your screen. The draft reply addresses the real property manager by name, and the cloud never learned who that was.
- Privacy receipt. Every inference in the session is logged with its mode, where it actually ran (
inferenceSource), latency, bytes sent, and identifiers withheld. The headline number, "identifiers sent", stays at 0 unless you release one yourself.
Real problems rarely come in one letter, so a case folder keeps the discharge note, the hospital bill and the insurer's explanation of benefits together on the device. Opening the whole case shields all of them at once, so "Maria E. Alvarez" on the insurer's letter and "Maria Elena Alvarez" on the bill become the same [PERSON_1]. Connect the case then builds one timeline with citations and points out what does not add up: in the sample, the hospital is billing the ambulance charge the insurer denied for missing paperwork.
On-device triage (category, urgency, deadline, one-line headline) runs automatically with structured output when Gemini Nano is available. On other devices it waits for a click, because nothing goes to the cloud without consent.
How we built it
- Firebase AI Logic Web SDK (
firebase/ai, Gemini Developer API backend) with all four inference modes, each chosen for a reason:ONLY_ON_DEVICE: identifier detection on raw text (onDeviceParams.promptOptions.responseConstraintwith aSchema.object), reading scanned letters with Nano's image input, and Private mode.PREFER_ON_DEVICE: light tasks and triage. They are fast, free and work offline.PREFER_IN_CLOUD: jargon glossary, reply drafting, connecting a case, and documents too long for Nano's context window. When the network fails, these fall back to Gemini Nano.ONLY_IN_CLOUD: Cloud mode, for best quality on demand.
- One invariant makes the hybrid fallback safe: raw text and images are only ever given to a model instance created with
ONLY_ON_DEVICE. EveryPREFER_*request carries masked text, so wherever the SDK routes it, no identifier leaves the device. - Matching
inCloudParams(responseMimeType: application/json+responseSchema) andonDeviceParams(responseConstraint) give the same structured output on either side. createOptionsdeclares exactly the inputs and outputs each model needs, includingexpectedInputs/expectedOutputslanguages for Spanish and Japanese, soLanguageModel.availability()answers the real question. If a device's Nano cannot handle a language pair, the device-first task takes the masked cloud path and the UI explains why. Gemini Nano is downloaded withinitializeDeviceModel()from a user gesture, with a progress bar.- Firebase Remote Config holds the routing policy (
cloud_preferred_tasks,cloud_model,cloud_temperature,device_char_budget,cloud_enabled,routing_notice) with the shipped policy as in-app defaults. Turningcloud_enabledoff makes Auto runONLY_ON_DEVICEon masked text: an operator's emergency brake if the free cloud quota runs out. - App Check with reCAPTCHA Enterprise protects the Firebase AI Logic endpoint.
- Streaming with
generateContentStream. The badge on each answer comes fromresponse.inferenceSource. - Nano's detections are accepted only if they literally appear in the text, so a hallucinated "identifier" can never alter the document.
- Case folders live in
localStorageand are never uploaded. - Vite + TypeScript with no UI framework, a service worker for an offline app shell, deployed to GitHub Pages by GitHub Actions.
- A Playwright suite of 33 checks verifies that on the cloud path no raw identifier appears in any request body sent to Google; that with Chrome's Prompt API present,
PREFER_ON_DEVICEreportson_deviceand zero requests reach Google; that scanning sends nothing; that the Spanish and Japanese samples leave no raw identifier in the outbound text; that a Remote Config change applies without a rebuild; and offline behaviour with and without Nano.
Challenges we ran into
- Making fallback safe instead of just convenient.
PREFER_ON_DEVICEcan silently go to the cloud. We restructured the app so that whether text is masked depends on the mode, not on device availability, and the UI predicts the route before every run. - Availability vs. capability. The SDK's default on-device options include image input, which makes
availability()reportunavailableon many Chrome installs. Declaring exactly what each model uses fixed it, and the same idea later handled languages. - Hallucinated and over-eager PII. Small models occasionally "find" values that are not in the text, or hide deadlines and dosages that the answer needs. Literal-match validation, dropping dates and bare quantities, and merging with deterministic rules made detection reliable without making it brittle.
- One person, many spellings. Across a case, the same person appears as a full name, initials or a bare surname ("高橋様" in Japanese). Linking them to one placeholder took name normalization that runs before masking.
- App Check vs. automated tests. reCAPTCHA Enterprise treats headless browsers as bots, so the end-to-end suite moved to real Chrome with an optional debug token.
Accomplishments that we're proud of
- A privacy guarantee that is tested, not just claimed: the suite fails if a single raw identifier shows up in network traffic.
- A shield that is measured, not just demoed: on 16 held-out letters the rules alone mask 85.5% of 76 identifiers (a generic email-and-phone filter: 19.7%) while keeping all 47 deadlines, amounts and doses. What they miss, mostly first names with no label ("leave your key with Rita"), is exactly what Gemini Nano is there to catch, and the consent view shows every detection before anything is sent.
- All four inference modes, each used for a different reason, visible in the UI and in the receipt.
- A finished product rather than a demo: realistic sample documents in three languages, scanning, case folders, consent controls, streaming answers, a live routing policy, offline behaviour, accessible markup and a responsive layout.
What we learned
- Measure before claiming. Writing letters the detectors had never seen dropped our rules from "works on the samples" to 74% on the first set, and the fixes are now checked against a held-out set that was never used to tune them.
- The most useful thing to run on device isn't always the whole task. Sometimes it is the part that decides what the cloud may see.
inferenceSourcematters as much as the answer. Showing users where their data went builds more trust than any privacy policy.- Structured output on both sides of the hybrid boundary makes on-device models dependable building blocks.
- Putting the routing policy in Remote Config turns "device or cloud" from a code decision into an operational one, without touching the privacy guarantee.
What's next
- Encrypt case folders at rest and let a user move a case between their own devices without a server ever seeing it.
- A Chrome extension that shields text before it is pasted into any web form.
- Android with Gemini Nano, reusing the same shield and routing policy.
- Working with patient advocates and tenant unions to tune the detectors and task prompts for their documents.
Built With
- chrome-prompt-api
- firebase-ai-logic
- firebase-app-check
- firebase-remote-config
- gemini
- gemini-nano
- github
- github-actions
- playwright
- recaptcha-enterprise
- typescript
- vite