Domain package¶
Kind: reference. This page is generated from the docstrings of
typevet.domain. The API index lists the other packages.
typevet.domain
¶
Pure domain types for typed generation and candidate scoring.
Examples:
from typevet.domain import GenerationRequest, compile_json_schema
schema = {
"type": "object",
"properties": {"ok": {"type": "boolean"}},
"required": ["ok"],
}
req = GenerationRequest(
prompt="Classify sentiment.",
schema=schema,
model="gemma-4-31b-24gib-kv11-decoder",
)
assert compile_json_schema(schema)[0].syntax == "Bool"
See Also
- typevet.domain.models: Request and result dataclasses
- typevet.domain.errors: Generation failures
- typevet.domain.decisions: Decision types and dependency layers
- typevet.domain.decision_compile: JSON Schema compilation
- typevet.domain.media: Image inputs and the media marker
- typevet.domain.question_schema: Question records to JSON Schema
Attributes:
| Name | Type | Description |
|---|---|---|
Decision |
type
|
One compiled TypeLLM field from JSON Schema. |
BackendBusyError |
type
|
llama.cpp router busy with another model. |
BackendHttpError |
type
|
llama.cpp HTTP status 400 or above. |
CandidateScoringRequest |
type
|
Prompt and candidate tokens to score. |
CALIBRATION_CLIP |
float
|
Clip bound for calibration map inputs and outputs. |
CALIBRATION_MAP_SCHEMA |
str
|
Schema id of a calibration map document. |
CalibrationMap |
type
|
One validated calibration map. |
CalibrationRecord |
type
|
Raw and calibrated value of one Noul answer. |
calibration_map_from_mapping |
callable
|
Validate a parsed map document. |
CalibrationMapError |
type
|
Calibration map is malformed or refused. |
CalibrationDigestError |
type
|
Map file digest is missing, malformed or wrong. |
CalibrationTaskMismatchError |
type
|
Map was fitted for another task. |
CalibrationModelMismatchError |
type
|
Map was fitted for another model or backend. |
CalibrationTargetError |
type
|
Map targets a Choice or Score question. |
ImageInput |
type
|
One image to condition a judgment on. |
MEDIA_MARKER |
str
|
Documented media placeholder in a scoring prefix. |
SUPPORTED_IMAGE_MIME_TYPES |
frozenset
|
Accepted v1 image mime types. |
count_media_markers |
callable
|
Count media markers in a prefix. |
CandidateScoringResult |
type
|
Fail-closed scored candidates. |
CategoricalExecutionResult |
type
|
Greedy categorical execute outcome. |
OffOptionReceipt |
type
|
Off-option mass, threshold and guard flag. |
apply_off_option_threshold |
callable
|
Flag a result whose off-option mass is above a caller threshold. |
check_off_option_threshold |
callable
|
Reject an invalid threshold. |
DecisionExecutionError |
type
|
Categorical execute rejected inputs. |
GemmaTemplateError |
type
|
Gemma served-template or answer-prefix violation. |
GenerationError |
type
|
Base failure for a generation call. |
GenerationUnsupportedCapabilityError |
type
|
Backend cannot honor the ask. |
TransportError |
type
|
HTTP client failure before a response. |
GenerationRequest |
type
|
Prompt, schema and model ask. |
GenerationResult |
type
|
Validated structured value. |
JudgmentResponse |
type
|
Typed answers from a judgment call. |
MAX_ENUM_CHOICES |
int
|
Upper bound on enum size when compiling. |
Noul |
type
|
Yes/no judgment question. |
Question |
type
|
Union of judgment question types. |
MAX_PERMUTATIONS |
int
|
Upper bound on enum permutation budget. |
SchemaError |
type
|
Invalid or unsupported schema for compilation. |
SchemaValidationError |
type
|
Output failed the requested schema. |
ScoringError |
type
|
Base failure for a candidate-scoring call. |
compile_json_schema |
callable
|
Compile object schema to decisions. |
dependency_layers |
callable
|
Topological layers for decision dependencies. |
execute_categorical_decision |
callable
|
Choice/Bool execution through the injected scoring port; no I/O of its own. |
bind_control_candidates |
callable
|
Ordinal control tokens for native labels. |
judgment_original_labels |
callable
|
Ordered labels for a native question. |
normalize_question |
callable
|
Native question to executable Decision. |
compile_question_records |
callable
|
Question records to decisions. |
question_record_to_property |
callable
|
One question record to a JSON Schema property. |
question_records_to_json_schema |
callable
|
Question records to an object schema. |
TextParts |
type
|
Validated option block and context templates; its
|
DEFAULT_OPTION_BLOCK |
str
|
Option block template used when unset. |
DEFAULT_CONTEXT_TEMPLATE |
str
|
Context template used when unset. |
validate_option_block |
callable
|
Refuse a bad option block template. |
validate_context_template |
callable
|
Refuse a bad context template. |
Attributes¶
Answer
module-attribute
¶
Answer = NoulAnswer | ChoiceAnswer | ScoreAnswer
Union type for all judgment answers.
Classes¶
CalibrationMap
dataclass
¶
CalibrationMap(
method: str,
parameters: CalibrationParameters,
fitted_on: FittedOn,
evaluation: CalibrationEvaluation,
producer: str,
sha256: str,
levels: int | None = None,
)
One validated calibration map for one task and one question.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
|
parameters |
CalibrationParameters
|
Fitted parameters for |
fitted_on |
FittedOn
|
Task, receipt, model and backend of the fit. |
evaluation |
CalibrationEvaluation
|
Held-out metrics, recorded only. |
producer |
str
|
The |
sha256 |
str
|
The sha256 of the map file bytes. |
levels |
int | None
|
The level count of a Score map; |
Examples:
Methods:¶
apply
¶
Map one probability through the fitted function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
A probability in |
required |
Returns:
| Type | Description |
|---|---|
float
|
The calibrated probability, clipped to ``[CALIBRATION_CLIP, |
float
|
1 - CALIBRATION_CLIP]``. |
Raises:
| Type | Description |
|---|---|
CalibrationMapError
|
|
apply_levels
¶
Map each level probability, then rescale the levels to sum to 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probabilities
|
Mapping[int, float]
|
The level probabilities of one Score answer. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, float]
|
The calibrated level probabilities, keyed as the input. |
Raises:
| Type | Description |
|---|---|
CalibrationMapError
|
A level probability is not a finite number
in |
CalibrationRecord
dataclass
¶
CalibrationRecord(
raw: float,
calibrated: float,
method: str,
map_sha256: str,
raw_levels: dict[int, float] | None = None,
calibrated_levels: dict[int, float] | None = None,
)
The raw and calibrated value of one Noul or Score answer.
Attributes:
| Name | Type | Description |
|---|---|---|
raw |
float
|
The Noul probability or the Score score that the inner judgment returned. |
calibrated |
float
|
The Noul probability or the Score score after the map. |
method |
str
|
The map method. |
map_sha256 |
str
|
The sha256 of the map file bytes. |
raw_levels |
dict[int, float] | None
|
The level probabilities that
the inner judgment returned; |
calibrated_levels |
dict[int, float] | None
|
The level probabilities
after the map and the rescale; |
Examples:
record = CalibrationRecord(
raw=0.6, calibrated=0.45, method="platt", map_sha256="0" * 64
)
assert record.raw == 0.6
CandidateScoringRequest
dataclass
¶
CandidateScoringRequest(
model: str,
prefix: str,
candidates: tuple[CandidateTokenSpec, ...],
stage: ScoreStage = PRE_SAMPLING,
media: tuple[ImageInput, ...] = (),
)
Identify model, prefix, candidates, and required score stage.
Complete candidate coverage is required: adapters must return a score for every requested label and must not invent scores for omitted labels.
prefix must hold exactly one MEDIA_MARKER for each entry in
media. An empty media tuple is the text-only ask.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
Backend model id or alias. |
prefix |
str
|
Rendered prompt prefix ending before candidate tokens. |
candidates |
tuple[CandidateTokenSpec, ...]
|
Ordered candidate set. |
stage |
ScoreStage
|
Required logprob extraction stage. |
media |
tuple[ImageInput, ...]
|
Images the prefix marks, in order. |
Examples:
request = CandidateScoringRequest(
model="m",
prefix="Q:",
candidates=(CandidateTokenSpec("a", (1,)),),
)
assert request.stage is ScoreStage.PRE_SAMPLING
Methods:¶
__post_init__
¶
Reject empty fields, duplicate ids, bad markers or non-image media.
Stores media as a tuple, so a list input becomes immutable.
Raises:
| Type | Description |
|---|---|
ScoringValidationError
|
When the ask violates coverage rules or the
media marker count does not match |
TypeError
|
When a media item is not an |
CandidateTokenSpec
dataclass
¶
One labeled candidate as an ordered token-id sequence.
Attributes:
| Name | Type | Description |
|---|---|---|
label |
str
|
Stable candidate id (for example an enum string). |
token_ids |
tuple[int, ...]
|
Encoded token ids for this candidate. |
Examples:
Methods:¶
__post_init__
¶
Reject empty labels or invalid token-id sequences.
Raises:
| Type | Description |
|---|---|
ScoringValidationError
|
When label or token ids are invalid. |
CandidateScoringResult
dataclass
¶
CandidateScoringResult(
model: str,
stage: ScoreStage,
candidates: tuple[ScoredCandidate, ...],
usage: TokenUsage = TokenUsage(),
termination: ScoringTermination | None = None,
off_option_mass: float | None = None,
)
Complete identity-preserving scores for every requested candidate.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
Model id that produced the scores. |
stage |
ScoreStage
|
Stage used to obtain logprobs. |
candidates |
tuple[ScoredCandidate, ...]
|
Scores in request order. |
usage |
TokenUsage
|
Optional token usage metadata. |
termination |
ScoringTermination | None
|
Optional stop metadata. |
off_option_mass |
float | None
|
Probability mass that the model put
outside the candidate tokens, in |
Examples:
result = CandidateScoringResult(
model="m",
stage=ScoreStage.PRE_SAMPLING,
candidates=(ScoredCandidate("a", (1,), -1.0),),
)
assert len(result.candidates) == 1
assert result.off_option_mass is None
ScoredCandidate
dataclass
¶
One candidate label, token ids, and raw logprob.
Attributes:
| Name | Type | Description |
|---|---|---|
label |
str
|
Candidate id matching the request label. |
token_ids |
tuple[int, ...]
|
Token sequence matching the request. |
logprob |
float
|
Raw pre-sampling logprob for this candidate. |
Examples:
ScoringTermination
dataclass
¶
CategoricalExecutionResult
dataclass
¶
CategoricalExecutionResult(
decision: Decision,
value: Any,
probabilities: tuple[tuple[Any, float], ...],
logprobs: tuple[float, ...],
model: str,
usage: TokenUsage,
off_option: OffOptionReceipt = OffOptionReceipt(),
)
Greedy categorical decision outcome with full softmax distribution.
Attributes:
| Name | Type | Description |
|---|---|---|
decision |
Decision
|
The categorical field that was executed. |
value |
Any
|
Selected choice value from |
probabilities |
tuple[tuple[Any, float], ...]
|
Ordered choice/probability
pairs aligned with |
logprobs |
tuple[float, ...]
|
Raw pre-sampling logprobs in choice order. |
model |
str
|
Model id from the scoring port result. |
usage |
TokenUsage
|
Token usage metadata from the scoring port. |
off_option |
OffOptionReceipt
|
Off-option mass, caller threshold and
guard flag; execute sets no threshold, so the flag is |
Examples:
Decision
dataclass
¶
Decision(
name: str,
question: str,
choices: tuple[Any, ...],
syntax: str = "Choice",
numeric_type: str | None = None,
minimum: int | float | None = None,
maximum: int | float | None = None,
text_type: bool = False,
max_length: int | None = None,
permutations: int | str = 1,
return_probabilities: bool = False,
depends_on: tuple[str, ...] | None = None,
nullable: bool = False,
)
One tokenizer-independent field compiled from JSON Schema.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Property name in the output object. |
question |
str
|
Model-facing prompt for this field. |
choices |
tuple[Any, ...]
|
Closed choices; empty for open numeric or text. |
syntax |
str
|
TypeLLM syntax label (Choice, Bool, Integer, Number, Text). |
numeric_type |
str | None
|
|
minimum |
int | float | None
|
Lower bound for open numeric fields. |
maximum |
int | float | None
|
Upper bound for open numeric fields. |
text_type |
bool
|
True when the field is open-ended text. |
max_length |
int | None
|
Optional max string length for text fields. |
permutations |
int | str
|
Enum permutation budget or |
return_probabilities |
bool
|
Whether to request choice probabilities. |
depends_on |
tuple[str, ...] | None
|
Prior fields that must be set first. |
nullable |
bool
|
Whether JSON null is allowed for this field. |
Examples:
from typevet.domain.decisions import Decision
Decision("x", "Pick one.", ("a", "b"), syntax="Choice")
SchemaError
¶
BackendBusyError
¶
BackendBusyError(
message: str,
*,
status_code: int,
body_snippet: str,
retry_after_seconds: float | None = None,
rate_limit: Mapping[str, str] | None = None,
request_id: str | None = None,
)
Bases: BackendHttpError
A llama.cpp router refused to load the model while another model runs.
With autoload off, the router answers HTTP 400 model is not
loaded. The adapter raises this error when another model holds a router
slot and no slot is free, so it does not evict that model (#461).
Attributes:
| Name | Type | Description |
|---|---|---|
status_code |
int
|
HTTP status from the router, 400. |
body_snippet |
str
|
Truncated response body text for diagnostics. |
Examples:
from typevet.domain.errors import BackendBusyError
raise BackendBusyError(
"llama.cpp HTTP 400: model is not loaded",
status_code=400,
body_snippet="model is not loaded",
)
BackendHttpError
¶
BackendHttpError(
message: str,
*,
status_code: int,
body_snippet: str,
retry_after_seconds: float | None = None,
rate_limit: Mapping[str, str] | None = None,
request_id: str | None = None,
)
Bases: GenerationError
The backend returned an HTTP error status (400 or above) or a redirect.
The vLLM clients that backend_settings builds do not follow redirects;
a 3xx raises this error with the 3xx status and no Location value.
Attributes:
| Name | Type | Description |
|---|---|---|
status_code |
int
|
HTTP status from the router or the gateway. |
body_snippet |
str
|
Truncated response body text for diagnostics. Empty when the body is HTML, which a gateway error page often is. |
retry_after_seconds |
float | None
|
Wait that |
rate_limit |
Mapping[str, str]
|
Read-only |
request_id |
str | None
|
Request-id header value of the failed
request (#356). |
Examples:
from typevet.domain.errors import BackendHttpError
raise BackendHttpError(
"llama.cpp HTTP 500: internal",
status_code=500,
body_snippet="internal",
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable summary including status and snippet. |
required |
status_code
|
int
|
HTTP status from the backend or the gateway. |
required |
body_snippet
|
str
|
Truncated response body text. |
required |
retry_after_seconds
|
float | None
|
Parsed |
None
|
rate_limit
|
Mapping[str, str] | None
|
Rate-limit response headers; stored as a read-only copy. |
None
|
request_id
|
str | None
|
Request-id header value of the failed request. |
None
|
Methods:¶
CalibrationDigestError
¶
Bases: CalibrationMapError
The sha256 of the map file is missing, malformed or does not match.
Examples:
CalibrationMapError
¶
Bases: JudgmentError
A calibration map is malformed or cannot apply to this judgment.
Messages name fields and rules only. They never carry a probability, a digest, a task id, a model id or a backend name.
Examples:
CalibrationModelMismatchError
¶
Bases: CalibrationMapError
The map was fitted on another model or another serving backend.
Examples:
CalibrationTargetError
¶
CalibrationTaskMismatchError
¶
DecisionExecutionError
¶
GemmaTemplateError
¶
GenerationError
¶
GenerationUnsupportedCapabilityError
¶
Bases: GenerationError
The generation backend cannot honor a part of the request.
An adapter raises it before any HTTP call, for example when a request carries images the backend adapter cannot send. The adapter never drops the unsupported part silently.
Examples:
JudgmentError
¶
JudgmentValidationError
¶
SchemaValidationError
¶
Bases: GenerationError
The model output did not validate against the requested schema.
Attributes:
| Name | Type | Description |
|---|---|---|
payload |
object | None
|
Parsed value that failed validation, when set. |
args |
tuple
|
Standard exception args (message first). |
Examples:
from typevet.domain.errors import SchemaValidationError
raise SchemaValidationError("missing key", payload={})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable validation summary. |
required |
payload
|
object | None
|
Parsed value that failed validation, when available. |
None
|
Methods:¶
ScoringError
¶
ScoringUnsupportedCapabilityError
¶
ScoringValidationError
¶
TransportError
¶
Bases: GenerationError
The HTTP client failed before a usable llama.cpp response arrived.
Attributes:
| Name | Type | Description |
|---|---|---|
status_code |
None
|
Always |
body_snippet |
None
|
Always |
request_id |
str | None
|
Request-id header value of the failed vLLM
request, or |
Examples:
from typevet.domain.errors import TransportError
raise TransportError("llama.cpp request failed: connection refused")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable summary of the client failure. |
required |
request_id
|
str | None
|
Request-id header value of the failed request. |
None
|
Methods:¶
ChoiceAnswer
dataclass
¶
ChoiceAnswer(
choice: str,
confidence: float,
probabilities: dict[str, float],
off_option_flag: bool = False,
)
A selected choice with probabilities and confidence.
Attributes:
| Name | Type | Description |
|---|---|---|
choice |
str
|
The name of the selected option. |
confidence |
float
|
Confidence in the selected choice, from 0 to 1. |
probabilities |
dict[str, float]
|
Probability of each choice by name. |
off_option_flag |
bool
|
|
Examples:
NoulAnswer
dataclass
¶
A yes/no answer with probability of true.
Attributes:
| Name | Type | Description |
|---|---|---|
noul |
float
|
Probability of a yes answer, from 0 to 1. |
off_option_flag |
bool
|
|
Examples:
ScoreAnswer
dataclass
¶
ScoreAnswer(
score: float,
confidence: float,
legend: dict[int, str],
probabilities: dict[int, float],
off_option_flag: bool = False,
)
A scored response with rubric and probabilities.
Attributes:
| Name | Type | Description |
|---|---|---|
score |
float
|
Expected score within the legend range. |
confidence |
float
|
Confidence in the score, from 0 to 1. |
legend |
dict[int, str]
|
Rubric descriptions keyed by integer level. |
probabilities |
dict[int, float]
|
Probability of each level. |
off_option_flag |
bool
|
|
Examples:
answer = ScoreAnswer(
score=2.0,
confidence=0.9,
legend={0: "poor", 1: "fair", 2: "good"},
probabilities={0: 0.1, 1: 0.2, 2: 0.7},
)
assert set(answer.legend) == set(answer.probabilities)
Choice
¶
Choice(
*,
criteria: Mapping[str, str | dict[str, Any] | Sequence[Any] | None],
instructions: str | dict[str, Any] | Sequence[Any] | None = None,
)
A question that selects between named alternatives.
Construction is keyword-only.
Attributes:
| Name | Type | Description |
|---|---|---|
criteria |
Mapping[str, str | dict | Sequence | None]
|
Labels mapped to
descriptions or |
instructions |
str | dict | Sequence | None
|
The question to ask. |
Examples:
question = Choice(
criteria={"a": "Option A", "b": "Option B"},
instructions="Choose one:",
)
assert len(question.criteria) == 2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
criteria
|
Mapping[str, str | dict[str, Any] | Sequence[Any] | None]
|
Labels mapped to descriptions. |
required |
instructions
|
str | dict[str, Any] | Sequence[Any] | None
|
The question to ask. |
None
|
Noul
¶
Noul(
*,
instructions: str | dict[str, Any] | Sequence[Any] | None = None,
criteria: dict[str, Any] | None = None,
)
A yes/no question with optional descriptions for either outcome.
Construction is keyword-only.
Attributes:
| Name | Type | Description |
|---|---|---|
instructions |
str | dict | Sequence | None
|
Question or statement to evaluate. |
criteria |
dict | None
|
Optional |
Examples:
question = Noul(
instructions="Is this valid?",
criteria={"true": "Valid", "false": "Invalid"},
)
assert question.instructions is not None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instructions
|
str | dict[str, Any] | Sequence[Any] | None
|
The yes/no question or statement to evaluate. |
None
|
criteria
|
dict[str, Any] | None
|
Optional descriptions of the yes and no outcomes. |
None
|
Score
¶
Score(
*,
criteria: Sequence[str | dict[str, Any] | Sequence[Any]],
instructions: str | dict[str, Any] | Sequence[Any] | None = None,
)
A question that assigns a score using an ordered rubric.
Construction is keyword-only.
Attributes:
| Name | Type | Description |
|---|---|---|
criteria |
Sequence[str | dict | Sequence]
|
Ordered rubric descriptions. |
instructions |
str | dict | Sequence | None
|
What the model should rate. |
Examples:
question = Score(
criteria=["Poor", "Fair", "Good"],
instructions="Rate the response:",
)
assert len(question.criteria) >= 2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
criteria
|
Sequence[str | dict[str, Any] | Sequence[Any]]
|
Ordered descriptions, one per score from zero. |
required |
instructions
|
str | dict[str, Any] | Sequence[Any] | None
|
What the model should rate. |
None
|
JudgmentResponse
dataclass
¶
JudgmentResponse(
model: str,
usage: TokenUsage = TokenUsage(),
answers: dict[str, Answer] = dict(),
off_option: dict[str, OffOptionReceipt] = dict(),
calibration: dict[str, CalibrationRecord] = dict(),
request_ids: dict[str, str | None] = dict(),
text_parts: dict[str, str] = dict(),
)
Answers grouped by question type with model and optional usage metadata.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
The model id that produced the answers. |
usage |
TokenUsage
|
Token usage metadata for the call. |
answers |
dict[str, Answer]
|
Answer objects keyed by question name. |
off_option |
dict[str, OffOptionReceipt]
|
Off-option receipt keyed by question name; empty when the adapter does not report one. |
calibration |
dict[str, CalibrationRecord]
|
Raw and calibrated values keyed by question name, for each answer that a calibration map changed. Empty when no map applied. |
request_ids |
dict[str, str | None]
|
Request-id header value of the scoring request for each question, keyed by question name (#356). When one question sends more than one request, the last one is kept. Empty when no request id was sent. |
text_parts |
dict[str, str]
|
The |
Examples:
from typevet.domain.judgment_answers import NoulAnswer
response = JudgmentResponse(
model="local-test",
answers={"q1": NoulAnswer(noul=0.5)},
)
assert response.model == "local-test"
Attributes¶
nouls
property
¶
nouls: dict[str, NoulAnswer]
Return yes/no answers keyed by question id.
Returns:
| Type | Description |
|---|---|
dict[str, NoulAnswer]
|
Subset of |
choices
property
¶
choices: dict[str, ChoiceAnswer]
Return choice answers keyed by question id.
Returns:
| Type | Description |
|---|---|
dict[str, ChoiceAnswer]
|
Subset of |
scores
property
¶
scores: dict[str, ScoreAnswer]
Return score answers keyed by question id.
Returns:
| Type | Description |
|---|---|
dict[str, ScoreAnswer]
|
Subset of |
OffOptionReceipt
dataclass
¶
OffOptionReceipt(
off_option_mass: float | None = None,
off_option_threshold: float | None = None,
off_option_flag: bool = False,
)
Off-option mass, the caller threshold and the guard flag for one answer.
The flag is True only when both values are known and the mass is
above the threshold. A None mass never sets the flag.
Attributes:
| Name | Type | Description |
|---|---|---|
off_option_mass |
float | None
|
Probability mass outside the option
set, or |
off_option_threshold |
float | None
|
Caller threshold, or |
off_option_flag |
bool
|
|
Examples:
receipt = OffOptionReceipt.evaluate(mass=0.4, threshold=0.3)
assert receipt.off_option_flag is True
unknown = OffOptionReceipt.evaluate(mass=None, threshold=0.0)
assert unknown.off_option_flag is False
Methods:¶
evaluate
classmethod
¶
evaluate(*, mass: float | None, threshold: float | None) -> OffOptionReceipt
Build a receipt and set the flag from the mass and the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mass
|
float | None
|
Off-option mass from the scoring result, or |
required |
threshold
|
float | None
|
Caller threshold, or |
required |
Returns:
| Type | Description |
|---|---|
OffOptionReceipt
|
Receipt whose flag is |
as_dict
¶
Return the receipt as a JSON-ready mapping with explicit names.
Returns:
| Type | Description |
|---|---|
dict[str, float | bool | None]
|
|
dict[str, float | bool | None]
|
|
TokenUsage
dataclass
¶
Token counts for one judgment call when an adapter reports them.
Attributes:
| Name | Type | Description |
|---|---|---|
input_tokens |
int | None
|
Prompt tokens, when known. |
output_tokens |
int | None
|
Completion tokens, when known. |
Examples:
ImageInput
dataclass
¶
One decoded image to condition a judgment on.
The domain holds bytes only. URL and file resolution belongs to an adapter at the edge.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
bytes
|
Encoded image bytes in |
mime_type |
str
|
One of |
Examples:
Methods:¶
__post_init__
¶
Reject empty bytes and mime types outside the v1 set.
Raises:
| Type | Description |
|---|---|
ScoringValidationError
|
When bytes are empty or the mime type is
not in |
GenerationRequest
dataclass
¶
GenerationRequest(
prompt: str,
schema: Mapping[str, Any],
model: str,
media: tuple[ImageInput, ...] = (),
)
One typed generation ask.
Attributes:
| Name | Type | Description |
|---|---|---|
prompt |
str
|
Natural-language instruction for the model. |
schema |
Mapping[str, Any]
|
JSON Schema object as a mapping. |
model |
str
|
Backend model id or alias. |
media |
tuple[ImageInput, ...]
|
Images the prompt marks, one
|
Examples:
from typevet.domain.models import GenerationRequest
GenerationRequest(
prompt="Return JSON.",
schema={"type": "object", "additionalProperties": False},
model="fake",
)
Methods:¶
__post_init__
¶
Reject a blank field, a non-object schema root or a marker mismatch.
Stores media as a tuple, so a list input becomes immutable.
Raises:
| Type | Description |
|---|---|
ValueError
|
When prompt or model is blank, schema type is not
object, or the media marker count differs from |
TypeError
|
When schema is not a mapping, or a media item is not
an |
GenerationResult
dataclass
¶
A value that validated against the request schema.
Attributes:
| Name | Type | Description |
|---|---|---|
value |
Mapping[str, Any]
|
Validated JSON-compatible mapping. |
model |
str
|
Model id that produced the value. |
raw_text |
str | None
|
Optional raw model text before parse. |
Examples:
from typevet.domain.models import GenerationResult
GenerationResult(value={"ok": True}, model="fake")
ScoreStage
¶
Bases: StrEnum
When logits are read relative to sampler state.
Attributes:
| Name | Type | Description |
|---|---|---|
PRE_SAMPLING |
ScoreStage
|
Softmax over full pre-sample logits (#116). |
POST_SAMPLING |
ScoreStage
|
After sampler candidate list (not M1 stock). |
Examples:
TextParts
dataclass
¶
Caller templates for the option block and the context, validated.
None keeps the default template for that part.
Attributes:
| Name | Type | Description |
|---|---|---|
option_block |
str | None
|
Option block template, or |
context_template |
str | None
|
Context template, or |
Examples:
Methods:¶
__post_init__
¶
Refuse a template that breaks a rule of its part.
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
A set template breaks a rule. The message names the part and the rule, never the template text. |
receipt
¶
Return the text_parts receipt block.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
One entry per part: the SHA-256 hex digest of the UTF-8 template |
dict[str, str]
|
text, or |
Functions:¶
calibration_map_from_mapping
¶
calibration_map_from_mapping(
data: Mapping[str, object], *, sha256: str
) -> CalibrationMap
Validate a parsed map document and build a CalibrationMap.
An absent or null levels field gives a Noul map. An integer of 2 or
more gives a Score map with that level count. The receipt digest is
stored in lower case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, object]
|
The parsed JSON document. |
required |
sha256
|
str
|
The verified sha256 of the file bytes. |
required |
Returns:
| Type | Description |
|---|---|
CalibrationMap
|
The validated map. |
Raises:
| Type | Description |
|---|---|
CalibrationDigestError
|
|
CalibrationMapError
|
A required field is missing or invalid, or
|
build_and_validate_result
¶
build_and_validate_result(
request: CandidateScoringRequest,
*,
raw_logprobs: Mapping[str, float],
model: str,
stage: ScoreStage | None = None,
usage: TokenUsage | None = None,
termination: ScoringTermination | None = None,
off_option_mass: float | None = None,
) -> CandidateScoringResult
Build a result with request order and fail-closed coverage checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
CandidateScoringRequest
|
Original scoring ask. |
required |
raw_logprobs
|
Mapping[str, float]
|
Mapping from candidate label to raw logprob. |
required |
model
|
str
|
Model id recorded on the result. |
required |
stage
|
ScoreStage | None
|
Stage recorded on the result; defaults to |
None
|
usage
|
TokenUsage | None
|
Optional token usage metadata. |
None
|
termination
|
ScoringTermination | None
|
Optional termination metadata. |
None
|
off_option_mass
|
float | None
|
Probability mass outside the candidate tokens, or
|
None
|
Returns:
| Type | Description |
|---|---|
CandidateScoringResult
|
Validated |
Raises:
| Type | Description |
|---|---|
ScoringValidationError
|
Missing, duplicate, or unexpected labels,
non-finite logprobs, a positive logprob above |
compile_json_schema
¶
compile_json_schema(schema: Mapping[str, Any]) -> list[Decision]
Compile an ordered JSON Schema object into TypeLLM decisions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Mapping[str, Any]
|
Root object schema with |
required |
Returns:
| Type | Description |
|---|---|
list[Decision]
|
Decisions in property declaration order, with |
list[Decision]
|
and dependency layers validated. |
Raises:
| Type | Description |
|---|---|
SchemaError
|
When the schema is invalid or unsupported. |
NotImplementedError
|
For unsupported types without a finite enum. |
Examples:
apply_off_option_threshold
¶
apply_off_option_threshold(
result: CategoricalExecutionResult, threshold: float | None
) -> CategoricalExecutionResult
Set the caller threshold on the receipt and flag a mass above it.
The guard flags and does not raise. A None mass never sets the flag.
A None threshold keeps the guard off and the flag False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
CategoricalExecutionResult
|
Outcome from |
required |
threshold
|
float | None
|
Caller threshold, or |
required |
Returns:
| Type | Description |
|---|---|
CategoricalExecutionResult
|
A copy of |
CategoricalExecutionResult
|
the flag. |
Raises:
| Type | Description |
|---|---|
DecisionExecutionError
|
|
check_off_option_threshold
¶
Reject a threshold that is not None or a number in [0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
object
|
Caller threshold for the off-option mass. |
required |
Raises:
| Type | Description |
|---|---|
DecisionExecutionError
|
A bool, a non-number, NaN, or a value
outside |
execute_categorical_decision
¶
execute_categorical_decision(
decision: Decision,
*,
prefix: str,
candidates: tuple[CandidateTokenSpec, ...],
port: CandidateScoringPort,
model: str,
temperature: float = 1.0,
media: tuple[ImageInput, ...] = (),
) -> CategoricalExecutionResult
Score candidates, softmax logprobs, and pick the greedy choice.
Probabilities are the conditional distribution over the declared candidate
set at temperature temperature (default 1) via log-sum-exp softmax.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decision
|
Decision
|
Closed categorical field ( |
required |
prefix
|
str
|
Rendered prompt prefix before candidate tokens. |
required |
candidates
|
tuple[CandidateTokenSpec, ...]
|
Single-token specs aligned 1:1 with |
required |
port
|
CandidateScoringPort
|
Scoring port; |
required |
model
|
str
|
Model id forwarded to the scoring request. |
required |
temperature
|
float
|
Softmax temperature; must be finite and strictly positive. |
1.0
|
media
|
tuple[ImageInput, ...]
|
Images the |
()
|
Returns:
| Type | Description |
|---|---|
CategoricalExecutionResult
|
|
CategoricalExecutionResult
|
value, full probability table, raw logprobs and off-option receipt. |
Raises:
| Type | Description |
|---|---|
DecisionExecutionError
|
Unsupported syntax, nullable field, permutations
other than |
ScoringValidationError
|
Propagated when the port result does not match
the exact request (stage, count, label order, token ids) or holds
a non-finite logprob, or when |
dependency_layers
¶
Return stable topological layers and validate dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decisions
|
Sequence[Decision]
|
Compiled decisions, typically from |
required |
Returns:
| Type | Description |
|---|---|
list[list[Decision]]
|
Lists of decisions that may run in parallel within each layer. |
Raises:
| Type | Description |
|---|---|
SchemaError
|
For unknown dependencies, self-deps, or cycles. |
Examples:
bind_control_candidates
¶
bind_control_candidates(
original_labels: Sequence[Any], tokenize_content: Callable[[str], Sequence[int]]
) -> tuple[CandidateTokenSpec, ...]
Bind ordinal control strings to original labels via tokenize_content.
Uses control_binding_pairs for ("0", label) … ordering, then attaches
single-token ids from tokenize_content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_labels
|
Sequence[Any]
|
Labels preserved on each |
required |
tokenize_content
|
Callable[[str], Sequence[int]]
|
Tokenizer hook; each control |
required |
Returns:
| Type | Description |
|---|---|
tuple[CandidateTokenSpec, ...]
|
Single-token candidate specs in original-label order. |
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
Duplicate or empty labels, more than 36
labels, or multi-token controls.
When leading controls are single tokens and a later one is not, the
message states the tokenizer capacity: |
judgment_original_labels
¶
judgment_original_labels(question: Question) -> tuple[str, ...]
Return ordered original labels for control binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
Question
|
Native Noul, Choice, or Score question. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
Original label strings in scoring order. |
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
Empty or duplicate labels, or too few score levels. |
normalize_choice
¶
Map a Choice question to a Choice Decision preserving key order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
Choice
|
Multi-option judgment question. |
required |
field_name
|
str
|
Compiled property name on the output object. |
required |
Returns:
| Type | Description |
|---|---|
Decision
|
Executable Choice decision with criteria key order preserved. |
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
Invalid instructions or empty criteria. |
normalize_noul
¶
Map a Noul question to a Bool Decision with (False, True) choices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
Noul
|
Yes/no judgment question. |
required |
field_name
|
str
|
Compiled property name on the output object. |
required |
Returns:
| Type | Description |
|---|---|
Decision
|
Executable Bool decision with |
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
When |
normalize_question
¶
Normalize any supported native question to an executable Decision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
Question
|
Native Noul, Choice, or Score question. |
required |
field_name
|
str
|
Compiled property name on the output object. |
required |
Returns:
| Type | Description |
|---|---|
Decision
|
Executable decision for categorical scoring. |
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
Unsupported question type or invalid payload. |
normalize_score
¶
Map a Score rubric to a Choice Decision over 0..n-1 levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
Score
|
Ordered rubric judgment question. |
required |
field_name
|
str
|
Compiled property name on the output object. |
required |
Returns:
| Type | Description |
|---|---|
Decision
|
Executable Choice decision whose choices are integer rubric indices. |
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
Invalid instructions or fewer than two levels. |
question_types
¶
Map each question id to its wire type name.
Typed questions map to noul, choice or score. Raw wire dictionaries
map to their type value when that value is a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
questions
|
Mapping[str, Any]
|
Question objects or raw wire dictionaries keyed by id. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str | None]
|
The id to type mapping; |
count_media_markers
¶
Count MEDIA_MARKER occurrences in a rendered prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Rendered prompt prefix. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of documented media markers in |
Examples:
compile_question_records
¶
compile_question_records(
records: Sequence[Mapping[str, Any]], *, additional_properties: bool = False
) -> list[Decision]
Compile question records to TypeLLM Decision values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
Sequence[Mapping[str, Any]]
|
Loader-shaped question list. |
required |
additional_properties
|
bool
|
Forwarded to |
False
|
Returns:
| Type | Description |
|---|---|
list[Decision]
|
Decisions from |
question_record_to_property
¶
Map one System One-style question record to a JSON Schema property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Mapping[str, Any]
|
Mapping with |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, dict[str, Any]]
|
Field name and property schema object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When the record shape or labels are invalid. |
TypeError
|
When |
question_records_to_json_schema
¶
question_records_to_json_schema(
records: Sequence[Mapping[str, Any]], *, additional_properties: bool = False
) -> dict[str, Any]
Build a root object schema from ordered question records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
Sequence[Mapping[str, Any]]
|
Non-empty sequence of question mappings (loader export order). |
required |
additional_properties
|
bool
|
Value for |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Object schema with |
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
validate_context_template
¶
Refuse a context_template template that breaks a rule.
Placeholders: {context} and {field_block} are both required, and
each appears once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str
|
Context template text. |
required |
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
The template breaks a rule. The message names the part and the rule, never the template text. |
validate_option_block
¶
Refuse an option_block template that breaks a rule.
Placeholders: {question}, {control}, {label},
{description} and {answer_rule} are required; {name} is
optional. Each appears at most once. {control}, {label} and
{description} sit on one option line, which repeats once per
option; the other placeholders sit off it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str
|
Option block template text. |
required |
Raises:
| Type | Description |
|---|---|
JudgmentValidationError
|
The template breaks a rule. The message names the part and the rule, never the template text. |