Skip to content
System One

giuliosmall/pg_typesafe

Pre-alpha PostgreSQL extension that calls TypeSafe Jev from SQL for Choice, Noul and Score, including a batched function that classifies many distinct rows in one request.

giuliosmall/pg_typesafe on GitHub

Stars
82
Forks
1
Language
C
License
MIT
Last push
24 Sep 2026

Read from GitHub on . Counts change daily.

Reported by the author

One-by-one time
23s23 sgithub.com
Batched time
0.86s0.86 sgithub.com

pg_typesafe is a pre-alpha PostgreSQL extension that calls Jev directly from SQL, exposing all three question types as functions: typesafe_noul for a yes/no probability, typesafe_classify or typesafe_label for a choice among categories, typesafe_score for a rating against an ordered rubric, and typesafe_ask for mixing several questions in one request. Scalars issue one HTTP call per row, but for a table of many rows, typesafe_detect_many and typesafe_classify_many batch distinct values, sharing one request across up to typesafe.batch_size (32 by default) texts at a time.

The README demonstrates this on 1,000 closed NYC 311 complaints that reduce to 38 unique resolution strings: calling typesafe_detect once per distinct text took 23 seconds, while typesafe_detect_many, batching those same 38 strings into two TypeSafe requests instead of 38 separate HTTP calls, took 0.86 seconds, measured on a laptop against the live Jev API.

The extension keeps API keys out of SQL: the key lives in a Postgres server environment variable or a superuser-only file setting rather than in a session command that would show up in query logs. It’s tested on PostgreSQL 16 and 17.

How it uses Jev

SET typesafe.mock_response = $${
  "model": "jev-latest",
  "answers": {
    "flag": {"type": "noul", "noul": 0.92}
  },
  "usage": {"input_tokens": 1, "output_tokens": 1}
}$$;

SELECT typesafe_noul('anything', 'Is this urgent?');

View in README.md

Open on GitHub

PrimitivesChoiceScoreNoul

More like this

Keep browsing