0125GitHub
pg_typesafe
A PostgreSQL extension that calls Jev from SQL.
giuliosmall/pg_typesafeREADME ↗
# pg_typesafe
[](https://github.com/giuliosmall/pg_typesafe/actions/workflows/ci.yml)
**Pre-alpha.** A PostgreSQL extension that calls [TypeSafe AI](https://console.typesafe.ai/home) (System One / Jev) from SQL for categorical work: Choice, Noul, and Score.
Not affiliated with TypeSafe AI or the PostgreSQL Global Development Group.
Tested on **PostgreSQL 16 and 17**, libcurl 7.61+.
## Install
```bash
git clone https://github.com/giuliosmall/pg_typesafe.git
cd pg_typesafe
make
make install # needs write access to pkglibdir (often sudo)
```
```sql
CREATE EXTENSION typesafe;
```
`EXECUTE` is revoked from `PUBLIC`. The owner (usually a superuser) can run the functions. To let an app role call them:
```sql
GRANT EXECUTE ON FUNCTION typesafe_noul(text, text, text, text, text) TO app;
GRANT EXECUTE ON FUNCTION typesafe_detect_many(text[], text, text, text, text) TO app;
```
## API key
Set `TYPESAFE_API_KEY` on the **Postgres server** process. Do not put it in SQL.
```bash
export TYPESAFE_API_KEY=tsk_...
pg_ctl restart
```
Alternatively point `typesafe.api_key_file` (superuser-only GUC) at a file whose
first line is the key — this keeps the key out of SQL, logs, and
`postgresql.auto.conf`:
```sql
ALTER SYSTEM SET typesafe.api_key_file = '/etc/postgresql/typesafe.key';
SELECT pg_reload_conf();
```
```sql
SELECT typesafe_noul(
'Help! My payouts have been failing for 3 days.',
'Does this convey urgency?'
);
```
`SET typesafe.api_key` works for a session (superuser only) but appears in query logs.
`typesafe.endpoint` must be `https://`; plain `http://` is allowed only toward
localhost (used by the test suite). Responses are capped at 8 MB. HTTP 429/529
are retried up to 3 times per request with exponential backoff, honoring
`Retry-After`. Requests remain