Skip to content

SQLite

ThinkThen is a classifier you call like a function. It reads text, answers one bounded question, and hands back a result you can branch on.

.load ./thinkthen_sqlite sqlite3_thinkthensqlite_init
.mode json
CREATE TABLE messages(msg_id INTEGER PRIMARY KEY, body TEXT NOT NULL);
INSERT INTO messages VALUES (1, 'Please refund my order.');
SELECT id AS msg_id, answer AS is_refund, status
FROM thinkthen_decide('SELECT msg_id AS id, body AS record FROM messages',
'Does the customer request a refund?');

Output

[{"msg_id":1,"is_refund":1,"status":"answered"}]

In SQLite, each ThinkThen function is a table function. You give it a query of messages and one question, and it gives back one result per message, with the answer in a column beside your message.

Use SQLite 3.41.0 or later with extension loading on, which the sqlite3 shell has. Download the SQLite archive for your platform from the ThinkThen release page and extract it. Set THINKTHEN_API_KEY in the environment of the SQLite process, start sqlite3 in the extracted folder, and run:

.load ./thinkthen_sqlite sqlite3_thinkthensqlite_init
CREATE TABLE messages (msg_id INTEGER PRIMARY KEY, body TEXT);
INSERT INTO messages VALUES
(1, 'Please refund my order.'),
(2, 'Where is my parcel?'),
(3, 'I was charged twice for one invoice.'),
(4, 'I was charged twice and my parcel is late again.'),
(5, 'Morgan works for Example Group.
Please refund my order.');

Every example on this page uses that messages table. SELECT thinkthen_version(); gives the version you loaded. Loading into an older SQLite fails with a message that names the version. From a program, load the extension once per connection with your library's load_extension call.

Every result is in exactly one of three states, named by its status column:

  • answered: there is an answer, and the answer column holds it.
  • unsure: the model's number fell between the two bars you set, or no choice was likely enough.
  • failed: the message could not be judged, or the service did not respond.

Three rules hold for every function on this page.

  1. Where there is no answer, answer is NULL, and NULL is never a yes or a no. WHERE answer keeps only the yeses. WHERE NOT answer keeps only the noes. A missing answer passes neither test.
  2. Checking the state never fails. status, probability, error_code and error_message are on every result, beside the answer, in every state.
  3. A failed message is a result, not a failed statement. One bad message never costs you the others. A statement fails for a message only where you ask it to: in thinkthen_filter, and in any call with raise_if_failed.

Rule 1 has one trap, and it is SQL's own: ELSE, coalesce and NOT IN all treat NULL as "everything else". CASE WHEN answer THEN 'refund queue' ELSE 'inbox' END sends a not-sure message and a failed message to the inbox. Test status first, or name the no with WHEN NOT answer.

Here is one call, with everything a call can hold:

SELECT id AS msg_id, record AS body, answer AS is_refund, status
FROM thinkthen_decide('SELECT msg_id AS id, body AS record FROM messages',
'Does the customer request a refund?')
WHERE low = 0.2 AND high = 0.8;

The messages:

  • The messages are a query, given as text in the first argument. It must be one read-only statement.
  • The column to judge is named record, and your key is a column named id. record_column and id_column name other columns.
  • Your other columns do not come back. Each result carries id and record. To get other columns, join on id.
  • Bind values into the query with params, a JSON array, and never paste them into its text.
  • Add ORDER BY inside the query so position follows your order.

The options:

  • An option goes in WHERE after the call, as WHERE low = 0.2 AND high = 0.8. Write one = per option, and join options with AND. Your own tests on the result go in the same WHERE, as in WHERE low = 0.2 AND high = 0.8 AND answer. With OR, SQLite may silently ignore an option and use its default; use AND.
  • A list is JSON text: '["billing", "shipping", "other"]'. Rules, descriptions and annotate's questions are JSON text too.

The results:

  • A yes is 1 and a no is 0, which is how SQLite writes true and false. WHERE answer and WHERE NOT answer work as the rules above say.
  • An answer with parts, and every list, is JSON text. Read a part with answer ->> '$.text' and a list with json_each(answer).
  • status and error_code are text.

SQL's if is WHERE. A decide answer goes straight into it.

SELECT 'refund queue' AS where_it_goes
FROM thinkthen_decide('SELECT body AS record FROM messages WHERE msg_id = 1',
'Does the customer request a refund?')
WHERE answer;

The result comes back only on a yes. If the message could not be judged, answer is NULL, NULL is never true, and no result comes back. It never reads as a yes.

Only a decide answer belongs in WHERE. SQLite takes any value there and refuses nothing: a score of 0.3 counts as true, and a choice such as billing counts as false.

Don't turn this into a CASE with a plain ELSE. ELSE runs for every result that is not a yes: no, not sure and failed alike. To act on a no, name it:

SELECT CASE
WHEN answer THEN 'refund queue'
WHEN NOT answer THEN 'inbox'
END AS where_it_goes
FROM thinkthen_decide('SELECT body AS record FROM messages WHERE msg_id = 1',
'Does the customer request a refund?');

Where there is no answer, this gives NULL. It never gives inbox.

Set two bars and decide can also be not sure. status tells you whether there is a yes or no to read. This call saves its one result in a small table named is_refund, so the next few examples can read it without asking again.

CREATE TEMP TABLE is_refund AS
SELECT answer, probability, status, error_code, error_message
FROM thinkthen_decide('SELECT body AS record FROM messages WHERE msg_id = 2',
'Does the customer request a refund?')
WHERE low = 0.2 AND high = 0.8;
SELECT CASE
WHEN status = 'answered'
THEN CASE WHEN answer THEN 'refund queue' ELSE 'inbox' END
ELSE 'a person reads it'
END AS where_it_goes
FROM is_refund;

The inner ELSE is safe, because the result is already known to be answered. status <> 'answered' is the one test that covers both not sure and failed.

SELECT CASE
WHEN status = 'unsure' THEN 'a person reads it'
WHEN status = 'failed' THEN 'try again later'
WHEN answer THEN 'refund queue'
ELSE 'inbox'
END AS where_it_goes,
probability,
error_code
FROM is_refund;

Yes and no are the two answers. Not sure and failed are the two ways of having none. To name every state, switch on status:

SELECT CASE status
WHEN 'answered' THEN CASE WHEN answer THEN 'refund queue' ELSE 'inbox' END
WHEN 'unsure' THEN 'a person reads it'
WHEN 'failed' THEN 'try again later'
END AS where_it_goes
FROM is_refund;

SQL can't check that a CASE names every state. Leave the outer ELSE off, as here, so a state you forgot shows up as NULL and not as the wrong queue.

SELECT 'could not judge it: failed: ' || error_code || ': ' || error_message AS note
FROM is_refund
WHERE status = 'failed';

For a failed result, this query combines could not judge it: failed: with its actual code and reason, using the same failure format as the thinkthen command. Not sure counts as working: ThinkThen did its job, and the answer was "not sure".

error_code and error_message are NULL on every result that did not fail. error_http_status holds the service's HTTP status when it gave one.

SELECT answer AS is_refund
FROM thinkthen_decide('SELECT body AS record FROM messages WHERE msg_id = 1',
'Does the customer request a refund?')
WHERE raise_if_failed = 1;

With raise_if_failed, the statement fails with one error when any message failed. For annotate, a failed form or any failed entry counts as a failed message; a not-sure entry does not. Count each affected message once, even when several entries failed. For a failed outer form, report its error; otherwise report the first failed entry in question-name order. If no message failed, the results come back as usual, so the option fits on any call. Every function but filter takes it. filter always raises, as its section says.

For a fully consumed call, the error comes after every message is judged. It says how many messages failed, the position of the first, and its error code. The statement fails as a whole, so a CREATE TABLE ... AS or an INSERT ... SELECT around it saves nothing. An error can't carry results, so the answers that did come are gone with it. Leave the option off whenever you save answers: the failed results then sit in your table with the reason beside them.

SELECT probability FROM is_refund;
  • No bar option: one bar at 0.5. The answer is always yes or no.
  • threshold = 0.7: one bar. Yes at 0.7 or above, otherwise no.
  • low = 0.2 AND high = 0.8: two bars. No below 0.2, not sure from 0.2 up to 0.8, yes at 0.8 or above.

probability is the number ThinkThen compared with your bars: the model's probability of yes. A confident no has a low probability, such as 0.05. It is NULL only when the result failed.

The number is the model's own. To choose bars for your data, run examples you have already labelled through thinkthen_decide, save each probability in a table, and compare with your labels.

Give your choices as a list. The answer comes back as one of them, as text, spelled exactly as you wrote it.

CREATE TEMP TABLE topic AS
SELECT answer, probability, status
FROM thinkthen_choose('SELECT body AS record FROM messages WHERE msg_id = 3',
'What is the message about?')
WHERE choices = '["billing", "shipping", "other"]';
SELECT CASE
WHEN status = 'unsure' THEN 'a person reads it'
WHEN answer = 'billing' THEN 'billing team'
WHEN answer = 'shipping' THEN 'warehouse'
WHEN answer = 'other' THEN 'front desk'
END AS team_for
FROM topic;

choose can be not sure even when you set no bar. Its bar is 0.5 on the best choice, and with three choices all three can fall below it. So test status before the answer. threshold = 0.3 moves the bar. choose takes one bar only.

The choices (billing, shipping, other) are the words the model reads, so make them plain words or short phrases, not codes.

SQL has no list of choices it can check a CASE against. Add a choice and nothing points at this CASE. Name every choice with its own WHEN and leave ELSE off, so a new choice shows up as NULL and a not-sure result is never sent to the front desk.

probability is the best choice's. detail holds every choice with its probability.

Every function takes many messages the same way: the query you give it can return any number of results. You get one result back per message, in the order of your query, and each result carries your id and record beside the answer.

CREATE TABLE refund_checks AS
SELECT id AS msg_id, record AS body, answer AS is_refund, probability, status, error_code, error_message
FROM thinkthen_decide('SELECT msg_id AS id, body AS record FROM messages WHERE msg_id <= 3 ORDER BY msg_id',
'Does the customer request a refund?')
WHERE low = 0.2 AND high = 0.8;
SELECT msg_id, body,
CASE
WHEN status = 'unsure' THEN 'a person reads it'
WHEN status = 'failed' THEN 'try again later'
WHEN is_refund THEN 'refund queue'
ELSE 'inbox'
END AS where_it_goes
FROM refund_checks
ORDER BY msg_id;

One failed message does not stop the statement. Each result keeps its own state, with position counted from 1 and the message's id when it has one.

Give the whole set of messages to one call, so ThinkThen can send them in batches. Don't call a function once per result of another table: each call is a separate piece of work.

To save every result, put the call in CREATE TABLE ... AS, as above. The table keeps the state of every result, so nothing is lost. Then read the table as often as you like. Running the call again asks the service again, and costs again.

There is no separate call for one message. One message is a query that returns one result, as in the decide examples above.

filter means "give me the yeses". It hands back your own messages, in order.

SELECT refund_request.id AS msg_id, refund_request.record AS body
FROM thinkthen_filter('SELECT msg_id AS id, body AS record FROM messages WHERE msg_id <= 3 ORDER BY msg_id',
'Does the customer request a refund?') AS refund_request;

filter takes one bar, so every message is a yes or a no and none is left out for being not sure. threshold = 0.7 moves the bar. low and high are refused with a usage.threshold error that names thinkthen_decide: when you want not sure, use thinkthen_decide, as in the section above.

Your own messages can't carry a failure. So if any message fails, a fully consumed filter call raises one error after every message is judged, and the statement fails as a whole. It works as raise_if_failed does: nothing is saved, and the error says how many messages failed, the position of the first, and its code.

To see each failed message, or to keep the answers you paid for whatever happens, ask with thinkthen_decide, save the results, and pick the yeses yourself:

SELECT msg_id, body FROM refund_checks WHERE is_refund;
SELECT msg_id, body, error_code FROM refund_checks WHERE status = 'failed';

The first query is the refund queue. The second lists what to try again later.

Labels work as choices do. The answer is the list of labels that fit, in the order you gave them, and it can be empty.

CREATE TEMP TABLE topics AS
SELECT position, id, answer, status
FROM thinkthen_tag('SELECT body AS record FROM messages WHERE msg_id = 4',
'Which topics appear?')
WHERE labels = '["billing", "shipping"]';
SELECT 'billing team' AS team_for
FROM topics
WHERE EXISTS (SELECT 1 FROM json_each(topics.answer) WHERE value = 'billing');
SELECT topics.position, topics.id, topics.status, label.value AS topic
FROM topics
LEFT JOIN json_each(topics.answer) AS label ON true;

The left join keeps an empty or failed result as one result with a NULL topic. position, id and status still identify it and distinguish an empty answer from a failure.

tag is never not sure. threshold = 0.7 sets how likely a label must be to count. An empty list is an answer: no label fit. A NULL answer means the result failed. probability is always NULL for tag, and each label's own number is in detail.

Levels name a scale, lowest first. The answer is a number from 0 to 1 along it.

CREATE TEMP TABLE urgency AS
SELECT answer, status
FROM thinkthen_score('SELECT body AS record FROM messages WHERE msg_id = 1',
'How urgent is the request?')
WHERE levels = '["routine", "urgent"]';
SELECT answer AS urgency,
CASE WHEN answer > 0.5 THEN 'answer today' END AS note
FROM urgency;

The score is a position between your levels, not one of them, so it is a number and there is nothing to put in a CASE by name. To get exactly one level back, ask with thinkthen_choose. score is never not sure, and its probability is always NULL.

rank puts many messages in order along your levels, highest first.

SELECT ranked.answer, ranked.record AS body
FROM thinkthen_rank('SELECT msg_id AS id, body AS record FROM messages WHERE msg_id <= 3 ORDER BY msg_id',
'How urgent is the request?') AS ranked
WHERE levels = '["routine", "urgent"]'
ORDER BY ranked.answer NULLS LAST;

answer is the rank, starting at 1, and id and record are your message. position still says where the message was in your query. rank reads every message before it gives anything back. top = 5 selects the five highest-ranked messages after judging the collection. A failed message comes back with a NULL rank and status set to failed. rank is never not sure.

find points to the one line that answers.

CREATE TEMP TABLE action AS
SELECT record AS body, answer, status
FROM thinkthen_find('SELECT body AS record FROM messages WHERE msg_id = 5',
'Which line states the requested action?');
SELECT CASE
WHEN status = 'unsure' THEN 'a person reads it'
WHEN status = 'answered' AND answer IS NULL THEN 'no line answers'
WHEN status = 'answered' THEN answer ->> '$.text'
END AS line
FROM action;

The answer has three parts, text, start_offset and end_offset, or it is NULL when no line answers. find is the one function where a NULL answer can be an answer, so status is what tells "no line answers" from not sure and failed.

The offsets count characters from 0, and end_offset is one past the last character. SQL counts from 1, so substr(body, (answer ->> '$.start_offset') + 1, (answer ->> '$.end_offset') - (answer ->> '$.start_offset')) is the line. Like choose, find can be not sure with no bar set.

SELECT names.position, names.id, names.status,
name.value ->> '$.kind' AS kind, name.value ->> '$.text' AS text
FROM thinkthen_recognize('SELECT ? AS record') AS names
LEFT JOIN json_each(names.answer) AS name ON true
WHERE kinds = '["person", "organization"]'
AND params = '["Morgan works for Example Group."]';

answer is a list of names. Each name has kind, text, start_offset, end_offset and probability. The list can be empty. recognize is never not sure.

LEFT JOIN json_each(...) gives one result per name. A message with no names, or one that failed, stays as one result whose name columns are NULL, so it is never dropped without a word. A plain join with a comma would drop that message.

SELECT relations.position, relations.id, relations.status,
relation.value ->> '$.source.text' AS source,
relation.value ->> '$.relation' AS relation,
relation.value ->> '$.target.text' AS target
FROM thinkthen_relate('SELECT body AS record FROM messages WHERE msg_id = 5') AS relations
LEFT JOIN json_each(relations.answer) AS relation ON true
WHERE rules = '[{"source": "person", "target": "organization", "relation": "works_for"}]';

A rule names the kind it starts from, the kind it points to, and the relation, in that order. It may also give reads, the wording the model sees, and either, for a relation with no direction. Each relation in answer has source, target, relation, probability and either. detail holds every name found, including names in no relation.

annotate asks several questions of each message and gives back one form per message. The questions are written as JSON, each under a name of your choosing.

CREATE TABLE forms AS
SELECT id AS msg_id, record AS body, status, error_code, error_message, detail,
answer ->> '$.refund' AS is_refund,
answer ->> '$.topic' AS topic,
answer ->> '$.urgency' AS urgency,
detail ->> '$.refund.status' AS refund_status,
detail ->> '$.topic.status' AS topic_status,
detail ->> '$.urgency.status' AS urgency_status
FROM thinkthen_annotate('SELECT msg_id AS id, body AS record FROM messages WHERE msg_id <= 3 ORDER BY msg_id')
WHERE questions = '{
"refund": {"function": "decide", "question": "Does the customer request a refund?",
"threshold": {"low": 0.2, "high": 0.8}},
"topic": {"function": "choose", "question": "What is the message about?",
"choices": ["billing", "shipping", "other"]},
"urgency": {"function": "score", "question": "How urgent is the request?",
"levels": ["routine", "urgent"]}
}';
SELECT msg_id, body, status, refund_status, topic_status, urgency_status,
CASE WHEN topic_status = 'answered' THEN topic END AS topic,
CASE WHEN urgency_status = 'answered' THEN urgency END AS urgency,
CASE
WHEN status = 'failed' OR refund_status = 'failed'
OR topic_status = 'failed' OR urgency_status = 'failed'
THEN 'try again later'
WHEN refund_status = 'unsure' OR topic_status = 'unsure'
OR urgency_status = 'unsure'
THEN 'a person reads it'
WHEN refund_status = 'answered' AND topic_status = 'answered'
AND urgency_status = 'answered'
THEN CASE WHEN is_refund THEN 'refund queue' ELSE 'inbox' END
END AS where_it_goes
FROM forms
ORDER BY msg_id;

answer holds one entry per question, under the name you gave it. Each entry is the answer of its own function: answer ->> '$.refund' is a yes or no, so it works in WHERE. An entry is NULL when its question was not sure or failed, and detail holds that entry's own status, probability and error under the same name. One entry can be not sure while the others answer. The result's own status is failed only when the message could not be judged at all.

The saved detail keeps every entry's state and error. The routing query checks the form and each entry it uses: a failed entry goes to retry, a not-sure entry goes to a person, and an answer is used only after its entry is answered. With raise_if_failed, a failed form or any failed entry raises; a not-sure entry does not. A message with several failed entries counts once in the failure total.

Question names must not collide with generated result columns such as status, probability, position, id, answer, error_code and detail. The call reports a usage error naming the clash before sending any paid request. SQL keeps each question's answer and details nested under answer and detail, so refund beside refund_status is allowed: neither generates a column named after the other.

Any function except filter, rank and annotate can be an entry. filter and rank are about many messages, and a form is about one.

When a short question is not enough, say what the answers mean.

SELECT answer AS is_refund, status
FROM thinkthen_decide('SELECT body AS record FROM messages WHERE msg_id = 3',
'Does the customer request a refund?')
WHERE yes_means = 'the customer wants money back, including for a duplicate charge'
AND no_means = 'the customer wants anything else';
SELECT answer AS topic, status
FROM thinkthen_choose('SELECT body AS record FROM messages WHERE msg_id = 3',
'What is the message about?')
WHERE choices = '["billing", "shipping", "other"]'
AND descriptions = '{"billing": "charges, invoices and refunds",
"shipping": "parcels and delivery"}';

decide and filter take yes_means and no_means. choose, tag, score and rank take descriptions, keyed by your choices, labels or levels. A key that is not one of them is refused.

A message is one value of the record column: text, or JSON. Text is judged as it stands, even when it looks like JSON. field = '/text' picks one field of a JSON message, and tells ThinkThen the messages are JSON. format = 'json' says the same without picking a field, and ThinkThen then judges the whole message.

CREATE TABLE inbox (line TEXT);
INSERT INTO inbox VALUES ('{"id": "a1", "text": "Please refund my order."}');
SELECT id, answer AS is_refund, status
FROM thinkthen_decide('SELECT line AS record FROM inbox',
'Does the customer request a refund?')
WHERE field = '/text';

Give JSON messages an id. Each result carries it back in the id column. id is NULL when the message has none.

id holds your query's id column when it has one, and otherwise the JSON message's own id. Either way it keeps its own type: a number stays an INTEGER and text stays TEXT, so it joins to your key as it is.

A message that can't be judged always comes back as a failed result, and nothing about it is sent to the service:

The message The failed result's error_code
Blank: an empty string, only spaces, or {} record.empty
NULL, or a field that points at nothing record.missing
Not text, such as a number or a blob record.invalid

A query that returns no results gives no results back and sends nothing.

In SQL every result is already a table, so there is nothing to switch on: the answer column and the columns that explain it always come together.

Column Holds
position The message's place in your query, from 1
id Your id column, or a JSON message's own id, or NULL
record The message, as your query gave it
answer The answer, in the type of the function. NULL when not sure or failed
probability The number compared with your bars. NULL when failed, and for tag, score, rank, recognize, relate and annotate
status answered, unsure or failed
error_code, error_message, error_http_status Why a failed result failed. NULL on every other result
source Which question, service and model produced the result
detail The numbers behind the answer, by function

answer is 1 or 0 for decide and filter, text for choose, a REAL for score and an INTEGER for rank. For tag, find, recognize, relate and annotate it is JSON text, and so are detail and source. Saved in a table they stay JSON text, and ->> and json_each read them the same way.

Tables treat missing values their own way. WHERE is_refund drops the NULL results, for example, and so does a join on the answer. count(*) counts every result and count(is_refund) counts only the answered ones, so the difference is how many had no answer. To see why an answer is missing, read status:

SELECT status, count(*) AS records FROM refund_checks GROUP BY status;
SELECT msg_id, body FROM refund_checks WHERE status = 'unsure';

Save answers in a table before you use them, as refund_checks is saved above. A saved table can be read, joined and filtered for free. A call can't: every run asks the service again. Within one statement, SQLite may run the same call more than once, in a join for example, and an unchanged call then replays its results without asking again. Nothing is kept from one statement to the next.

The functions can't be used inside a view or a trigger. Save the answers in a table and build the view on that.

Choose the messages before the call, not after. Put WHERE and LIMIT inside the query you give the function. An outer LIMIT limits the results you see; it is not a reliable limit on work or cost because the database may read ahead. It can also hide an already-read, already-judged failure: SQLite can end the statement before filter or raise_if_failed reports its final error. Put LIMIT inside the input query instead. Do not treat an early-stopped statement as proof that every input message succeeded. Save the complete results before inspecting a subset when failure reporting matters.

One statement is all or nothing. If it is cancelled or fails, nothing it would have saved is kept, even when the answers were already paid for. For a large job, save a few thousand messages per statement and commit each one before starting the next.

The extension reads THINKTHEN_API_KEY and THINKTHEN_BACKEND from the environment of the SQLite process. To choose settings yourself, set them for the connection.

SELECT thinkthen_set('model', '...');
SELECT thinkthen_set('batch', 16);

The settings are backend, model, batch, max_retries, initial_backoff_ms (the first wait before a retry) and attempt_timeout_ms (the time limit for one attempt). Both times are in milliseconds. thinkthen_set takes one setting at a time. A setting lasts for the connection and never changes a statement that is already running. max_retries goes from 0 to 3.

SQL can't set a key or a service address. Both come only from the environment of the SQLite process, so no query can read a key or send your messages somewhere else.

Stop a statement the way SQLite stops any statement: Ctrl-C in the sqlite3 shell, or sqlite3_interrupt from a program. The statement fails with SQLite's own "interrupted" error, and the connection is ready for the next one. Requests already sent are still charged, and a cancelled statement saves nothing.

What happened What you get
A mistake in the call, such as a bad threshold, a missing list, low and high on filter, or a column your query does not have The statement fails before anything is sent. The error carries a code such as usage.threshold
The engine could not start, such as no API key, or the service rejects authentication with HTTP 401 or 403 The statement fails before anything is sent, with a code such as usage.key-missing
The statement was cancelled The statement fails with SQLite's own "interrupted" error
A message could not be judged, or the service failed A result with status of failed, a NULL answer, and the reason in error_code and error_message. The statement goes on
The same, in thinkthen_filter or with raise_if_failed On full consumption, one error after every message is judged. The statement fails and saves nothing; see Tables for early stopping
Not sure A result with status of unsure, a NULL answer and its probability. Never an error
You read status, probability or the error columns Never an error

Two tests let you sort results by meaning:

  • status <> 'answered' finds every result with no answer. That covers not sure and failed.
  • status = 'failed' finds every result where something went wrong. Not sure is a result and not an error.

HTTP 401 or 403 stops the call with backend.status. The call also stops when every request in its first batch fails and at least one failure has an HTTP status from 400 through 499. These are statement errors even without raise_if_failed; other service failures follow the failed-result rule above.

A failed result's error_code is one of eight: record.empty, record.missing, record.invalid, record.json, record.too-large, backend.transport, backend.status and backend.reply. Every other code belongs to a failed statement. Branch on the code, never on the message, which is written for people and can change.

A raised error is an ordinary SQLite error, and its message starts with the code, as in usage.threshold: .... The error for failed messages starts with the first failed message's code, then reports how many failed out of those judged, the first failed position and its actual reason. A mistake in how an option is written, such as text where a number belongs, has the code usage.argument. An option the function does not have is SQLite's own "no such column" error.

When an error is raised for failed messages, treat any results your program had already read from that statement as not returned. SQLite hands results over one step at a time, so a program that steps through the result gets results before the error reaches it. The statement has still failed, and a CREATE TABLE ... AS around it saves nothing.

The baseline is the database implementation in the published 0.1.2 source.

0.1.2 Now
Scalar calls return a bare answer or NULL Table calls return an answer with status, probability and errors
Keyed _many calls take JSON objects and return key/value The query/table input and position/id/result columns shown above
Separate details/probability functions State and detail columns on each result
find selects among supplied units find returns a span within each message

Update stored queries and dependent views using this database’s calling convention. The public SQL driver ticket proves the shared contract; each database ticket proves these statements against its built extension.