

# fastdecisions

[`fastdecisions`](docs/api/fastdecisions.html) provides a single Jev-style async interface for decision models across TypeSafe Jev (`jev-1.14.0`), Google Gemini (`gemini-decision-1`), and OpenAI (`gpt-6-luna`). Pass shared evidence—text, JSON, [`aidialog.msg_parts.Msg`](https://AnswerDotAI.github.io/aidialog/msg_parts.html#msg) chat histories, or media—alongside batched [`noul`](docs/api/types.html#noul) (yes/no probability), [`choice`](docs/api/types.html#choice) (classification), [`score`](docs/api/types.html#score) (rubric rating), or [`box`](docs/api/types.html#box) (bounding box) questions, and swap providers with a single `model=` argument. Each provider adapter runs on `fastspec.OpenAPIClient` with offline bundled specs.

## Install

Run `pip install fastdecisions` or clone from GitHub and install locally (`pip install -e .`).

Set whichever provider key(s) you plan to use: `TYPESAFE_API_KEY` (TypeSafe Jev), `GEMINI_API_KEY` (Google Gemini), or `OPENAI_API_KEY` (OpenAI).

## Quickstart

``` python
from fastdecisions import adecide, decide, Decider, noul, choice, score, box, jev, gem_dec, luna
```

For a one-off yes/no check with a single provider (`TYPESAFE_API_KEY`), pass a bare string as `questions` and read `r.answer.noul`:

``` python
ticket = 'I was charged twice for my subscription this month and nobody has replied yet. Fix this today!'
r_one = await adecide(ticket, 'Is this message about billing?', model=jev)
f"p={r_one.answer.noul:.3f}, confidence={r_one.answer.confidence:.3f}"
```

    'p=0.990, confidence=0.990'

## Decisions across providers

Define questions once using Jev’s canonical schema—plain strings act as [`noul`](docs/api/types.html#noul) yes/no shorthand, `noul(..., true=..., false=...)` adds boundary criteria, [`choice`](docs/api/types.html#choice) accepts either a list of labels or a `{label: description}` dict, and [`score`](docs/api/types.html#score) takes an ordered rubric—then swap `model=` across Jev, Gemini, and GPT-6 Luna:

``` python
qs = dict(billing='Is this message about billing?',
    prior_contact=noul('Has the customer contacted support about this before?',
        true='Mentions a prior attempt, ticket, or waiting for a reply', false='No sign of any previous contact'),
    tone=choice(['angry', 'calm', 'excited'], 'What is the tone of this message?'),
    urgency=score(['Can wait', 'Needs attention this week', 'Needs attention today'], 'How urgent is this message?'))

for m in (jev, gem_dec, luna):
    r = await adecide(ticket, qs, model=m)
    print(f"{m:18} billing={r.billing.noul:.2f}  prior={r.prior_contact.noul:.2f}  tone={r.tone.choice:7}  urgency={r.urgency.score:.2f}  cost=${r.cost:.6f}")
```

    jev-1.14.0         billing=0.98  prior=0.73  tone=angry    urgency=2.00  cost=$0.000018
    gemini-decision-1  billing=0.99  prior=0.92  tone=angry    urgency=1.88  cost=$0.000030
    gpt-6-luna         billing=1.00  prior=1.00  tone=angry    urgency=2.00  cost=$0.000057

Inspect a [`Decision`](docs/api/types.html#decision) bare in a notebook to render a summary table alongside collapsible raw JSON, or access answers by attribute (`r.tone.choice`) or key (`r['tone'].probabilities`):

``` python
r
```

<table>
<thead>
<tr>
<th>Question</th>
<th>Type</th>
<th>Value</th>
<th>Confidence</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>billing</code></td>
<td><a href="docs/api/types.html#noul"><code>noul</code></a></td>
<td>1.000</td>
<td>1.000</td>
</tr>
<tr>
<td><code>prior_contact</code></td>
<td><a href="docs/api/types.html#noul"><code>noul</code></a></td>
<td>1.000</td>
<td>1.000</td>
</tr>
<tr>
<td><code>tone</code></td>
<td><a href="docs/api/types.html#choice"><code>choice</code></a></td>
<td><code>angry</code></td>
<td>1.000</td>
</tr>
<tr>
<td><code>urgency</code></td>
<td><a href="docs/api/types.html#score"><code>score</code></a></td>
<td>2.000</td>
<td>1.000</td>
</tr>
</tbody>
</table>

<details>

<summary>

Details
</summary>

- model: `gpt-6-luna`
- tokens: 575 in / 0 out
- cost: $0.000057
- `billing`: `NoulAnswer(noul=1.0, confidence=1.0, raw={'type': 'predicate', 'name': 'billing', 'probability': 1.0})`
- `prior_contact`: `NoulAnswer(noul=1.0, confidence=1.0, raw={'type': 'predicate', 'name': 'prior_contact', 'probability': 1.0})`
- `tone`: `ChoiceAnswer(choice='angry', confidence=1.0, probabilities={'angry': 1.0, 'calm': 0.0, 'excited': 0.0}, raw={'type': 'choice', 'name': 'tone', 'choice': 'angry', 'probabilities': [{'value': 'angry', 'probability': 1.0}, {'value': 'calm', 'probability': 0.0}, {'value': 'excited', 'probability': 0.0}], 'confidence': 1.0})`
- `urgency`: `ScoreAnswer(score=2.0, confidence=1.0, probabilities={'0': 0.0, '1': 0.0, '2': 1.0}, legend={'0': 'Can wait', '1': 'Needs attention this week', '2': 'Needs attention today'}, raw={'type': 'score', 'name': 'urgency', 'score': 2.0, 'probabilities': [{'value': 0, 'label': 'Can wait', 'probability': 0.0}, {'value': 1, 'label': 'Needs attention this week', 'probability': 0.0}, {'value': 2, 'label': 'Needs attention today', 'probability': 1.0}], 'confidence': 1.0})`

</details>

## Reusable [`Decider`](docs/api/core.html#decider) and batching

Bind a question schema and model once in a [`Decider`](docs/api/core.html#decider) to evaluate many items concurrently with `.batch(...)` while tracking cumulative token `usage` and USD `cost`:

``` python
triage = Decider(dict(billing='Is this about billing?', tone=choice(['angry', 'calm', 'excited'], 'Tone?')), model=jev)
tickets = ['You double-charged my card and I want an immediate refund!',
    'Love the new dark mode, great work team!',
    'Could you explain how to export a CSV from my dashboard?']
for t, res in zip(tickets, await triage.batch(tickets)): print(f"tone={res.tone.choice:7}  billing={res.billing.noul:.2f}  | {t}")
print(f"Total: {triage.usage.prompt_tokens} input tokens, ${triage.cost:.6f}")
```

    tone=angry    billing=0.98  | You double-charged my card and I want an immediate refund!
    tone=excited  billing=0.02  | Love the new dark mode, great work team!
    tone=calm     billing=0.10  | Could you explain how to export a CSV from my dashboard?
    Total: 951 input tokens, $0.000040

## Chat history

Pass [`aidialog.msg_parts.Msg`](https://AnswerDotAI.github.io/aidialog/msg_parts.html#msg) conversations directly as `state`. Each adapter strips internal `Thinking` blocks and converts the history into its provider format (JSON on Jev, structured JSON on Gemini, native Responses items on OpenAI):

``` python
from aidialog.msg_parts import Msg, Text, Thinking
```

``` python
history = [Msg('user', [Text('My parcel arrived smashed.')]),
    Msg('assistant', [Thinking('Check order status first'), Text('I am sorry to hear that. I have issued a full refund to your card.')]),
    Msg('user', [Text('Thanks, that solves it!')])]
for m in (jev, gem_dec, luna):
    r = await adecide(history, dict(resolved='Did the customer confirm their issue is resolved?'), model=m)
    print(f"{m:18} resolved={r.resolved.noul:.3f}")
```

    jev-1.14.0         resolved=0.980
    gemini-decision-1  resolved=0.981
    gpt-6-luna         resolved=1.000

## Vision and bounding boxes

Pass an `InputImage` directly as `state` across all three providers. On Jev, add a [`box`](docs/api/types.html#box) question to locate normalized `[0, 1]` bounding coordinates (Gemini and OpenAI raise `NotImplementedError` for [`box`](docs/api/types.html#box)):

``` python
from PIL import Image as PImg, ImageDraw
from fastcore.utils import img_bytes
from aidialog.msg_parts import InputImage, to_data_url
```

``` python
canvas = PImg.new('RGB', (320, 320), 'white')
ImageDraw.Draw(canvas).rectangle([80, 80, 240, 240], fill='red')
red_img = InputImage(to_data_url(img_bytes(canvas, fmt='PNG')), mime='image/png')

for m in (jev, gem_dec, luna):
    r = await adecide(red_img, dict(has_red='Does this image contain a red rectangle?',
        color=choice(['red', 'blue', 'green'], 'Main shape color?')), model=m, resolution='low')
    print(f"{m:18} has_red={r.has_red.noul:.2f}  color={r.color.choice}")

r_box = await adecide(red_img, dict(rect=box('Locate the red rectangle.')), model=jev)
r_box.rect
```

    jev-1.14.0         has_red=0.87  color=red
    gemini-decision-1  has_red=0.84  color=red
    gpt-6-luna         has_red=1.00  color=red

`BoxAnswer(top=0.17, left=0.23, bottom=0.90, right=0.75, confidence=0.730)`

## Provider capabilities

Provider-specific options pass through `adecide(..., **kwargs)` and are ignored by other adapters (for example `long_context=True` on Jev, `resolution='low'` on Gemini, and `prompt_cache_key` / `prompt_cache_options` on OpenAI). For synchronous code (scripts or notebooks), use the `decide(...)` wrapper.

<table>
<colgroup>
<col style="width: 25%" />
<col style="width: 25%" />
<col style="width: 25%" />
<col style="width: 25%" />
</colgroup>
<thead>
<tr>
<th>Capability</th>
<th>TypeSafe Jev (<code>jev-1.14.0</code>)</th>
<th>Google Gemini (<code>gemini-decision-1</code>)</th>
<th>OpenAI (<code>gpt-6-luna</code>)</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="docs/api/types.html#noul"><code>noul</code></a>, <a href="docs/api/types.html#choice"><code>choice</code></a>, <a href="docs/api/types.html#score"><code>score</code></a></td>
<td>Yes</td>
<td>Yes</td>
<td>Yes (<a href="docs/api/types.html#noul"><code>noul</code></a> criteria folded into <code>instructions</code>)</td>
</tr>
<tr>
<td><a href="docs/api/types.html#box"><code>box</code></a> bounding boxes</td>
<td>Yes</td>
<td>No (<code>NotImplementedError</code>)</td>
<td>No (<code>NotImplementedError</code>)</td>
</tr>
<tr>
<td><code>Msg</code> chat history</td>
<td>JSON in <code>state</code></td>
<td>Structured JSON + media</td>
<td>Native message + tool items</td>
</tr>
<tr>
<td><code>InputImage</code></td>
<td>Yes (PNG/JPEG, 280–1568 px)</td>
<td>Yes (plus <code>resolution</code>)</td>
<td>Yes (plus <code>detail</code>)</td>
</tr>
<tr>
<td><code>InputAudio</code> / <code>InputVideo</code> / <code>InputFile</code></td>
<td>No</td>
<td>Yes</td>
<td>No</td>
</tr>
<tr>
<td>Per-question refusals</td>
<td>Never</td>
<td>Whole-request block (<a href="docs/api/fastdecisions.html#apierror"><code>APIError</code></a>)</td>
<td><a href="docs/api/types.html#refusalanswer"><code>RefusalAnswer</code></a> in <code>answers</code></td>
</tr>
</tbody>
</table>
