Typed Outputs

Parse raw model output into a typed, validated value. See the typed outputs pattern for usage.

techrevati.runtime.output_spec

Typed outputs — validate raw model text into a typed value.

An OutputSpec[T] turns the opaque string a model returns into a typed, validated Python value. The runtime ships three reference implementations:

  • :class:JsonOutputSpec — parse JSON, optionally enforce required keys / a top level type.
  • :class:RegexOutputSpec — match a regex, return its named groups.
  • :class:CallableOutputSpec — wrap any Callable[[str], T] and normalize its failures.

Specs are pure and caller-applied — the runtime does not own the model call, so you parse the result yourself:

spec = JsonOutputSpec(required_keys=("decision", "reason"))
raw, usage = session.run_turn(lambda: call_model(prompt))
parsed = spec.parse(raw)            # OutputValidationError on bad output

Anything that does not satisfy the spec raises :class:OutputValidationError.

OutputValidationError

Bases: ValueError

Raised when raw model output does not satisfy an :class:OutputSpec.

OutputSpec

Bases: Protocol[T_co]

Parse raw model output into a typed value (or raise OutputValidationError).

JsonOutputSpec dataclass

JsonOutputSpec(
    required_keys=(), require_type=None, strip_fences=True
)

Parse JSON output, optionally enforcing required keys and a top-level type.

strip_fences removes a leading/trailing Markdown code fence (```json) that models commonly wrap JSON in.

RegexOutputSpec dataclass

RegexOutputSpec(pattern, flags=0, fullmatch=False)

Match output against a regex and return its named groups as a dict.

Uses :meth:re.Pattern.search by default; set fullmatch=True to require the whole string to match.

CallableOutputSpec

CallableOutputSpec(fn)

Bases: Generic[T]

Wrap an arbitrary Callable[[str], T] as an :class:OutputSpec.

Exceptions raised by the callable are normalized to :class:OutputValidationError (OutputValidationError itself passes through unchanged).