typesafe-client 0.1.0

Unofficial typed async Rust client for the TypeSafe System One API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# typesafe-client

[![CI](https://github.com/JedimEmO/typesafe-client/actions/workflows/ci.yml/badge.svg)](https://github.com/JedimEmO/typesafe-client/actions/workflows/ci.yml)
[![crates.io](https://img.shields.io/crates/v/typesafe-client.svg)](https://crates.io/crates/typesafe-client)
[![docs.rs](https://img.shields.io/docsrs/typesafe-client)](https://docs.rs/typesafe-client)
[![MSRV 1.87](https://img.shields.io/badge/MSRV-1.87-blue)](#minimum-supported-rust-version)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue)](#license)

A typed, async Rust client for the [TypeSafe](https://typesafe.ai) System One API.

> **Unofficial.** This is an independent community project. It is not affiliated with,
> endorsed by, or supported by TypeSafe AI. For the API itself, see the
> [official TypeSafe documentation]https://docs.typesafe.ai.

TypeSafe's System One models, such as Jev, don't generate text. They answer narrow questions
about content you give them, and return typed answers with calibrated probabilities: the
probability that something is true, the most likely option out of a set, or a position on a
scale. This crate lets you ask those questions from Rust and read each answer back as the type
its question promises.

- **Typed end to end.** Adding a question returns a key typed by its answer, so reading a
  Choice answer as a yes/no probability doesn't compile. Enums and runtime values map straight
  to Choice options.
- **Checked both ways.** Requests are checked against the documented limits before they are
  sent. Responses are verified against the questions before you see them.
- **Production defaults.** Retries 408, 429 and 5xx responses, connection errors and timeouts
  with exponential backoff. Honors `Retry-After`, and puts a hard deadline on every call.
- **Testable.** Application code depends on the `SystemOne` trait. The `fake` feature provides
  an in-memory implementation that answers every question and records requests.
- **Light when you want it.** Without default features the crate is just the types, builders
  and validation, with no HTTP stack.

## Contents

- [Installation]#installation
- [Quick start]#quick-start
- [Core concepts]#core-concepts
- [Guide]#guide: [yes/no questions]#yesno-questions,
  [choices from an enum]#choices-from-an-enum, [choices from runtime values]#choices-from-runtime-values,
  [scores]#scores, [structured state]#structured-state,
  [many questions at once]#many-questions-in-one-request, [configuration]#configuring-the-client,
  [retries and timeouts]#retries-and-timeouts, [errors]#handling-errors,
  [testing]#testing-code-that-uses-the-client, [without HTTP]#using-only-the-types
- [Examples]#examples
- [Minimum supported Rust version]#minimum-supported-rust-version
- [License]#license

## Installation

```toml
[dependencies]
typesafe-client = "0.1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
```

| Feature | Default | What it adds |
|---|---|---|
| `http` | yes | `Client`, the async HTTP client (reqwest with rustls) |
| `fake` | no | `fake::FakeSystemOne`, an in-memory implementation for tests |

Create an API key in the [TypeSafe console](https://console.typesafe.ai/keys) and make it
available to your program:

```sh
export TYPESAFE_API_KEY="your-key"
```

## Quick start

```rust,no_run
use typesafe_client::{Client, NoulQuestion, Questions};

#[tokio::main]
async fn main() -> Result<(), typesafe_client::Error> {
    // Reads TYPESAFE_API_KEY, and optionally TYPESAFE_BASE_URL and TYPESAFE_DEFAULT_MODEL.
    let client = Client::from_env()?;

    let mut questions = Questions::new();
    let urgent = questions.add(
        "is_urgent",
        NoulQuestion::new("Does this message convey urgency?"),
    );

    let response = client
        .system_one("Help! My payouts have been failing for 3 days.", questions)
        .send()
        .await?;

    let urgent = response.answer(&urgent)?;
    println!("probability of urgency: {:.2}", urgent.noul);
    Ok(())
}
```

## Core concepts

| Concept | In this crate |
|---|---|
| **State**: the content every question is about | `Content`: a string, a JSON object or a JSON array. Strings and `serde_json` values convert directly, and any `Serialize` type goes through `Content::json`. |
| **Question**: one narrow judgment | `NoulQuestion` (yes/no), `ChoiceQuestion`, `EnumChoice` and `ValueChoice` (one of up to 255 options), `ScoreQuestion` (2 to 10 ordered levels) |
| **Key**: how you read an answer | `Questions::add(id, question)` returns a `QuestionKey`, typed by how its answer is read |
| **Answer**: probabilities, not prose | `NoulAnswer` (`noul`), `ChoiceAnswer` and `TypedChoice<T>` (`choice`, `probabilities`, `confidence`), `ScoreAnswer` (`score`, level probabilities, `confidence`) |

Every question in a request sees the same state and is answered independently and in parallel,
so extra questions in one request cost little. The question id only matches answers to
questions. It is never sent to the model, so each question's instructions must make sense on
their own.

TypeSafe's guides explain how to phrase questions and choose thresholds. Start with
[primitives](https://docs.typesafe.ai/primitives), [state](https://docs.typesafe.ai/concepts/state)
and [confidence](https://docs.typesafe.ai/confidence).

## Guide

### Yes/no questions

A Noul answer is the probability that the answer is yes. Near 1 means yes and near 0 means no.
A value near 0.5 means the model is unsure, not that something is "somewhat" true. Criteria
describe what counts as each answer.

```rust
use typesafe_client::{NoulQuestion, Questions};

let mut questions = Questions::new();
let refund = questions.add(
    "refund_requested",
    NoulQuestion::new("Is the customer explicitly asking for a refund?")
        .with_criteria(
            "Asks for money back or a credit",
            "Mentions a charge without asking for money back",
        ),
);
```

### Choices from an enum

`choice_options!` declares an enum whose variants are the options, each with an optional
description. The answer comes back as that enum, with a probability for every variant and a
confidence for the distribution as a whole.

```rust
use typesafe_client::{
    ChoiceAnswer, EnumChoice, Questions, SystemOneResponse, Usage, choice_options,
};

choice_options! {
    /// Which team handles a support ticket.
    pub enum Department {
        Billing = "billing" => "Payments, invoices, refunds",
        Technical = "technical" => "Bugs, outages, integrations",
        Sales = "sales" => "Pricing, upgrades, new accounts",
    }
}

fn main() -> Result<(), typesafe_client::Error> {
    let mut questions = Questions::new();
    let department = questions.add(
        "department",
        EnumChoice::<Department>::new("Which team should handle this ticket?"),
    );

    // A response like the one the API returns (sending is shown in the quick start).
    let response = SystemOneResponse::new("jev-latest", Usage::default()).with_answer(
        &department,
        ChoiceAnswer::new(
            "technical",
            0.82,
            [("billing", 0.08), ("technical", 0.85), ("sales", 0.07)],
        ),
    );

    let department = response.answer(&department)?;
    let queue = match department.choice {
        _ if department.confidence < 0.5 => "human review",
        Department::Billing => "billing",
        Department::Technical => "engineering",
        Department::Sales => "sales",
    };
    assert_eq!(queue, "engineering");
    Ok(())
}
```

Descriptions can also be structured JSON, which helps separate similar options:
`Billing = "billing" => serde_json::json!({ "what": "...", "not_for": "..." })`.

### Choices from runtime values

`ValueChoice` offers values that are only known at runtime, such as line numbers or record ids.
Each value is sent as its `Display` text and parsed back with `FromStr`. Put what each value
refers to in the state.

```rust
use typesafe_client::{NoulQuestion, Questions, ValueChoice};

let lines = [
    "Refunds are issued within 5 business days.",
    "Contact support by email.",
    "You can cancel your plan at any time.",
];
let document: String = lines
    .iter()
    .enumerate()
    .map(|(number, line)| format!("{number}| {line}\n"))
    .collect();

let mut questions = Questions::new();
let line = questions.add(
    "line",
    ValueChoice::new("Which line says how long refunds take?", 0..lines.len()),
);
// Choice probabilities always sum to 1, so ask separately whether any line fits.
let answered = questions.add(
    "answered",
    NoulQuestion::new("Does any line say how long refunds take?"),
);
// Send `document` as the state. `response.answer(&line)?` is a `TypedChoice<usize>`,
// and `.top(3)` returns the three most likely line numbers.
```

### Scores

A Score rates the state on 2 to 10 ordered levels. The answer's `score` is probability-weighted,
so it can fall between levels. `nearest_level()` rounds it when your code needs a single
outcome. `normalized()` scales it to 0–1 so scores with different numbers of levels can be
combined.

```rust
use typesafe_client::{Questions, ScoreAnswer, ScoreQuestion};

let mut questions = Questions::new();
let severity = questions.add(
    "severity",
    ScoreQuestion::new(
        "How severe is the reported issue?",
        ["Cosmetic", "Degraded, with a workaround", "Blocking, no workaround"],
    ),
);

// An answer like the one the API returns:
let severity = ScoreAnswer::new(1.3, 0.62, ["Cosmetic", "Degraded", "Blocking"], [0.0, 0.7, 0.3]);
assert_eq!(severity.nearest_level(), 1);
assert!((severity.normalized() - 0.65).abs() < 1e-9);
```

### Structured state

Give the model named, related context. Refer to parts of it by backticked paths in your
instructions.

```rust
use serde::Serialize;
use typesafe_client::{Content, NoulQuestion, Questions, SystemOneRequest};

#[derive(Serialize)]
struct Ticket {
    subject: String,
    messages: Vec<String>,
}

#[derive(Serialize)]
struct State<'a> {
    ticket: &'a Ticket,
    refund_policy: &'a str,
}

fn main() -> Result<(), typesafe_client::Error> {
    let ticket = Ticket {
        subject: "Duplicate charge".into(),
        messages: vec!["I was charged twice for order A-104.".into()],
    };
    let state = Content::json(&State {
        ticket: &ticket,
        refund_policy: "Duplicate charges are refunded in full.",
    })?;

    let mut questions = Questions::new();
    questions.add(
        "policy_supports_refund",
        NoulQuestion::new("Does `refund_policy` support a refund for `ticket.messages`?"),
    );

    let request = SystemOneRequest::new(state, questions);
    request.validate()?;
    Ok(())
}
```

Object fields are sent in `serde_json` map order, which is sorted by key unless your
application enables `serde_json`'s `preserve_order` feature.

### Many questions in one request

Ask every question you might need in one request, including speculative ones, then use only
the answers that apply. Adding questions barely changes the response time.

```rust
use typesafe_client::{NoulQuestion, Questions};

let hazards = [
    "asks the reader for a password",
    "offers an unexpected prize or payment",
    "pressures the reader to act immediately",
];

let mut questions = Questions::new();
let keys: Vec<_> = hazards
    .iter()
    .map(|hazard| {
        questions.add(
            format!("hazard::{hazard}"),
            NoulQuestion::new(format!("Does `message.body` {hazard}?")),
        )
    })
    .collect();

assert_eq!(questions.len(), keys.len());
```

A request can carry about 32,000 tokens (roughly 150,000 characters), shared by the state and
the questions.

### Configuring the client

`Client::from_env()` reads these variables. `Client::builder()` sets the same values in code,
and explicit settings win.

| Variable | Builder method | Default |
|---|---|---|
| `TYPESAFE_API_KEY` | `api_key` | required |
| `TYPESAFE_BASE_URL` | `base_url` | `https://api.typesafe.ai` |
| `TYPESAFE_DEFAULT_MODEL` | `default_model` | `jev-latest` |

```rust,no_run
use std::time::Duration;
use typesafe_client::{Client, RetryPolicy};

fn main() -> Result<(), typesafe_client::Error> {
    let client = Client::builder()
        .api_key(std::env::var("MY_APP_TYPESAFE_KEY").unwrap_or_default())
        .ignore_env() // don't read TYPESAFE_* variables
        .default_model("jev-latest") // pin a concrete model for results you compare over time
        .timeout(Duration::from_secs(5)) // per attempt
        .retry(RetryPolicy::default().with_max_retries(4))
        .build()?;

    // Calls can override the model, timeout and retry policy:
    let call = client
        .system_one("state", typesafe_client::Questions::new())
        .model("jev-latest")
        .timeout(Duration::from_secs(2));
    drop(call);
    Ok(())
}
```

`Client` is cheap to clone, and clones share one connection pool. A call made with
`system_one` owns its own clone, so it can be spawned with `tokio::spawn(call.send())`, stored,
or cloned to send again. To use a preconfigured `reqwest` client, for example with a proxy,
pass it to `ClientBuilder::http_client`; the crate re-exports the `reqwest` version it uses.

### Retries and timeouts

| Setting | Default |
|---|---|
| Retries after the first attempt | 2 |
| Retried failures | HTTP 408, 429 and 5xx (including 529); connection errors; timeouts |
| Backoff | 0.5 s, doubling up to 5 s, minus up to 25% jitter |
| `retry-after-ms` / `Retry-After` | honored up to 60 s; longer values fall back to the backoff |
| Timeout per attempt | 10 s |
| Deadline for the whole call | 30 s: attempts are cut short to fit it |

Adjust these with `RetryPolicy`'s `with_*` methods, or turn retries off with
`RetryPolicy::disabled()`.

### Handling errors

`Error` separates problems you fix in code from ones worth retrying later.

```rust,no_run
use typesafe_client::{ApiErrorKind, Client, Error, NoulQuestion, Questions};

#[tokio::main]
async fn main() {
    let client = Client::from_env().expect("TYPESAFE_API_KEY is set");
    let mut questions = Questions::new();
    let spam = questions.add(
        "is_spam",
        NoulQuestion::new("Is this message unsolicited advertising?"),
    );

    match client.system_one("Cheap watches, today only!", questions).send().await {
        Ok(response) => {
            let spam = response.answer(&spam).expect("verified responses answer every question");
            println!("spam: {:.2}", spam.noul);
        }
        // Broke a documented limit; nothing was sent.
        Err(Error::InvalidRequest(problem)) => eprintln!("fix the request: {problem}"),
        Err(error) if error.api().is_some_and(|api| api.kind == ApiErrorKind::Authentication) => {
            eprintln!("check TYPESAFE_API_KEY");
        }
        // Still failing after the built-in retries.
        Err(error) if error.is_retryable() => {
            eprintln!("temporary failure: {error} (request id {:?})", error.request_id());
        }
        Err(error) => eprintln!("request failed: {error}"),
    }
}
```

For a rejected request body (HTTP 422), `error.api()` gives the `ApiError`, and its
`field_errors()` list what the server rejected.

### Testing code that uses the client

Let your code depend on `Arc<dyn SystemOne>`. In production that's the `Client`; in tests it's
`FakeSystemOne`.

```toml
[dev-dependencies]
typesafe-client = { version = "0.1", features = ["fake"] }
```

```rust
use std::sync::Arc;
use typesafe_client::fake::FakeSystemOne;
use typesafe_client::{Error, NoulQuestion, Questions, SystemOne};

struct Moderator {
    typesafe: Arc<dyn SystemOne>, // Arc::new(Client::from_env()?) in production
}

impl Moderator {
    async fn should_hide(&self, message: &str) -> Result<bool, Error> {
        let mut questions = Questions::new();
        let spam = questions.add(
            "is_spam",
            NoulQuestion::new("Is this message unsolicited advertising?"),
        );
        let response = self.typesafe.system_one(message, questions).send().await?;
        Ok(response.answer(&spam)?.noul > 0.9)
    }
}

// In your test suite this would be a #[tokio::test].
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let fake = Arc::new(FakeSystemOne::new());
    let moderator = Moderator { typesafe: fake.clone() };

    // Questions without a configured answer are maximally uncertain: a Noul answers 0.5.
    assert!(!moderator.should_hide("See you at lunch").await?);

    fake.set_noul("is_spam", 0.97);
    assert!(moderator.should_hide("Cheap watches, today only!").await?);

    assert_eq!(fake.request_count(), 2);
    assert_eq!(
        fake.last_request().unwrap().state.as_text(),
        Some("Cheap watches, today only!")
    );
    Ok(())
}
```

The fake behaves like a careful API:

- **Configure answers:** `set_noul`, `set_choice`, `set_choice_probabilities`, `set_score` or
  `set_answer`, by question id or key.
- **Script outcomes:** `push_response`, or `push_error` with an `ApiError` or
  `Error::Timeout(TransportError::new("timed out"))`.
- **Inspect what was sent:** `requests`, `last_request`, `request_count`.
- **Checks like the real client:** it validates requests and verifies answers. A configured
  option the question doesn't have fails the call instead of producing an impossible response.

### Using only the types

```toml
[dependencies]
typesafe-client = { version = "0.1", default-features = false }
```

Without `http` you still get questions, keys, answers, validation, response verification and
the `SystemOne` trait. That's useful in a domain crate that shouldn't depend on an HTTP client,
or with your own transport:

```rust
use typesafe_client::{
    CallOptions, Error, ModelList, SystemOne, SystemOneRequest, SystemOneResponse, async_trait,
};

struct MyTransport;

#[async_trait]
impl SystemOne for MyTransport {
    async fn send(
        &self,
        request: &SystemOneRequest,
        _options: &CallOptions,
    ) -> Result<SystemOneResponse, Error> {
        request.validate()?;
        // POST the request (filling in `model` if it is `None`) to
        // `typesafe_client::constants::SYSTEM_ONE_PATH`, turn failures into
        // `ApiError::from_response`, then return `response` after
        // `response.verify(&request.questions)?`.
        unimplemented!()
    }

    async fn list_models(&self) -> Result<ModelList, Error> {
        unimplemented!()
    }
}
```

## Examples

The repository has runnable examples:

- [`triage`]https://github.com/JedimEmO/typesafe-client/blob/main/crates/typesafe-client/examples/triage.rs
  classifies a support ticket with several questions in one request, then routes it in code.
- [`audit`]https://github.com/JedimEmO/typesafe-client/blob/main/crates/typesafe-client/examples/audit.rs
  reviews every source file in a directory for cleanliness and responsibilities, and suggests
  whether to refactor or split each one. `--dry-run` shows the requests without an API key.

```sh
TYPESAFE_API_KEY=... cargo run -p typesafe-client --example triage
TYPESAFE_API_KEY=... cargo run -p typesafe-client --example audit -- crates/typesafe-client/src
```

## Minimum supported Rust version

Rust 1.87. CI checks this version, and raising it counts as a minor change.

## License

Licensed under either of [Apache License, Version 2.0](https://github.com/JedimEmO/typesafe-client/blob/main/LICENSE-APACHE)
or [MIT license](https://github.com/JedimEmO/typesafe-client/blob/main/LICENSE-MIT), at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion
in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above,
without any additional terms or conditions. See
[CONTRIBUTING.md](https://github.com/JedimEmO/typesafe-client/blob/main/CONTRIBUTING.md) for
how to build and test.