aix.sampling

Repeated sampling: n answers to one prompt, drawn as fast as the provider allows.

Asking the same question several times is the basic variance-reduction (and stability-measurement) tool for LLM judgements. sample() does it in one of two ways and records which, because they are not the same statistical object:

  • "provider_n" – one request with n choices (one prompt evaluation, n decodes). Cheapest, and used when LiteLLM reports the model supports n.

  • "independent" – n separate requests, run concurrently. Works with every provider; each sample also gets its own usage.

If a provider silently returns fewer choices than asked, the shortfall is drawn independently and the batch is marked "mixed".

>>> answers = sample("Is a tomato a fruit? Answer yes or no.", n=5)
>>> answers.texts
('Yes', 'Yes', 'Yes', 'yes', 'Yes')
>>> answers.strategy, answers.usage.total_tokens
('provider_n', 52)

Functions

sample(prompt, *, n[, temperature, seed, ...])

Draw n answers to prompt, each with its finish reason and usage.

concurrent_map(func, items, *, concurrency)

[func(x) for x in items], run on up to concurrency threads, in order.

check_positive_int(name, value)

int(value) if it is a positive integer (not a bool), else ValueError.

forget_n_refusals([model])

Forget which models were found to refuse a provider-side n.

Classes

Sample(text, payload[, finish_reason, ...])

One drawn answer.

Samples([samples, usage, n_requests, ...])

The list of Sample returned by sample(), plus batch facts.

Exceptions

SamplingError(message, *[, samples])

A request inside sample() failed (with on_error="raise").

class aix.sampling.Sample(text, payload, finish_reason=None, usage=None, strategy='independent', seed=None, error=None, system_fingerprint=None)[source]

Bases: object

One drawn answer.

text

The raw text of this choice ("" if its request failed).

Type:

str

payload

parse(text) when a parse function was given, else text; None if the request or the parsing failed (see error).

Type:

Any

finish_reason

Why this choice stopped. "length" = truncated, "content_filter" = refused/filtered; check it before treating the payload as a judgement.

Type:

Optional[str]

usage

Tokens for the request that produced only this sample, or None when it shared a provider-side n request (whose usage cannot be split per choice; see Samples.usage).

Type:

Optional[Usage]

strategy

How this sample was drawn: "provider_n" or "independent".

Type:

str

seed

The seed sent with its request, if any.

Type:

Optional[int]

error

The exception this sample’s request raised (with on_error="record") or that parse raised on its text.

Type:

Optional[Exception]

system_fingerprint

The serving backend’s fingerprint for its request, when reported. Seeded samples are only comparable across equal fingerprints (see aix.supports() for whether seed is honoured at all).

Type:

Optional[str]

property ok: bool

True when the request succeeded and the text parsed (if asked).

class aix.sampling.Samples(samples=(), *, strategy, usage=None, n_requests=0, n_usage_reports=0, fallback_error=None)[source]

Bases: list

The list of Sample returned by sample(), plus batch facts.

It is a plain list otherwise: slicing or concatenating gives a list without the batch attributes, which describe the whole draw only.

strategy

"provider_n", "independent" or "mixed" (provider n topped up with independent calls).

usage

Total tokens over every request that reported usage, or None if none did. Compare n_requests with n_usage_reports to see whether it is complete.

n_requests

How many requests were made (schema repair requests included).

n_usage_reports

How many of them reported usage (a completion’s repair requests are counted with it when it reported usage). A provider request refused with a 400 (see fallback_error) reports none.

fallback_error

With strategy="auto": the error that made the provider-side n request fall back to independent requests (LiteLLM refusing n, or the provider rejecting it with a 400), else None.

property errors: tuple[Exception, ...]

Every recorded error (request or parse), in sample order.

exception aix.sampling.SamplingError(message, *, samples=None)[source]

Bases: Exception

A request inside sample() failed (with on_error="raise").

No request is launched after the first failure, and the ones already in flight are awaited, so the work already paid for is not lost: samples holds every request that was made, failed ones included (as samples whose error is set) – possibly fewer than n. The first failure is __cause__.

aix.sampling.check_positive_int(name, value)[source]

int(value) if it is a positive integer (not a bool), else ValueError.

>>> check_positive_int("n", 3)
3
>>> check_positive_int("n", 0)
Traceback (most recent call last):
...
ValueError: n must be a positive integer, got 0
Return type:

int

aix.sampling.concurrent_map(func, items, *, concurrency)[source]

[func(x) for x in items], run on up to concurrency threads, in order.

Fails fast like the serial loop would: once a call raises, calls not yet started are cancelled (those already running finish), and the first exception in item order propagates.

>>> concurrent_map(lambda x: x * x, range(5), concurrency=3)
[0, 1, 4, 9, 16]
Return type:

list[R]

aix.sampling.forget_n_refusals(model=None)[source]

Forget which models were found to refuse a provider-side n.

sample(strategy="auto") remembers, per resolved model id (and endpoint kwargs) and for the life of the process, that a provider refused n with a 400 and a plain request then succeeded. That is a heuristic: a 400 caused by n combined with another argument is remembered too. Call this if a provider has since started accepting n. model=None forgets every model.

>>> forget_n_refusals()
Return type:

None

aix.sampling.sample(prompt, *, n, temperature=None, seed=None, concurrency=None, model=None, strategy='auto', parse=None, response_schema=None, on_error='raise', api_key=None, **kwargs)[source]

Draw n answers to prompt, each with its finish reason and usage.

Parameters:
  • prompt (Union[str, Iterable[dict]]) – A string or a list of message dicts, as for aix.chat().

  • n (int) – How many samples to draw.

  • temperature (float) – Sampling temperature (None = configured default). At 0 the samples mostly coincide, which is rarely what you want here.

  • seed (int) – Base seed. A provider-side n request sends it as is; each independent request sends a seed derived from (seed, index) so independent samples are not forced identical and runs with different base seeds do not overlap. Support is provider-dependent.

  • concurrency (int) – Max simultaneous independent requests (None = config chat.sample_concurrency).

  • model (str) – Model id or alias (None = configured default).

  • strategy (Literal['auto', 'provider', 'independent']) – "auto" (provider n when supported, else independent; also falls back to independent if LiteLLM or the provider refuses n – for a provider 400 the first independent request is sent alone as a probe, and if it fails too the original 400 is raised; see Samples.fallback_error), "provider" (force one n request) or "independent". n=1 is always a single plain request, recorded "independent".

  • response_schema (Union[dict, type]) – A JSON Schema dict or Pydantic model class, enforced and validated per request as in aix.complete(); each payload is the validated value, and a choice still invalid after repairs records its SchemaValidationError as error. Exclusive with parse.

  • parse (Callable[[str], Any]) – Optional text -> payload (e.g. json.loads). A failure is recorded on that sample (payload=None, error=...), not raised.

  • on_error (Literal['raise', 'record']) – When a request fails: "raise" (default) raises SamplingError once the launched requests finish, carrying the partial batch; "record" returns the batch with the failure on that sample’s error.

  • api_key (str) – Explicit API key, as for aix.chat().

  • **kwargs – Passed to every request (e.g. max_tokens, response_format).

Return type:

Samples

Returns:

Samples – a list of Sample with batch .strategy, .usage, .n_requests.

Raises:
  • ValueError – For a non-positive n / concurrency, an unknown strategy / on_error, or a reserved kwarg (messages, stream).

  • SamplingError – A request failed and on_error="raise".