Skip to main content

Crate typesayer

Crate typesayer 

Source
Expand description

Native Rust predict engine for structured LLM prediction.

Provides core types for building structured input/output contracts for language model calls, formatting prompts, parsing responses, and composing multi-step LLM programs with introspectable, serializable state.

§Prompt visibility for debugging

ChatAdapter::format emits the fully assembled prompt at TRACE on the target typesayer::adapter::chat::messages — a signature shorthand ("inputs -> outputs") plus messages_json, the provider-agnostic Vec<Message> serialized to JSON (the [[ ## field ## ]] envelope, demo turns, and cache-breakpoint markers). Off by default; an operator opts in:

  • Standalone binaries / examples (env filter reads RUST_LOG): RUST_LOG=typesayer::adapter::chat::messages=trace
  • Applications with a broader filter can pass a complete directive: RUST_LOG="warn,typesayer=info,typesayer::adapter::chat::messages=trace"

Prompts can contain sensitive user data, so this is TRACE-gated and intended for … 2>&1 | tee inspection — never enable it in production log shipping.

§Quick Start

use std::{collections::BTreeMap, sync::Arc};

use modelplease::{DummyLM, ModelId};
use typesayer::{ChatAdapter, Context, Predict};
use typesayer_types::{FieldDef, FieldType, FieldValue, Signature};

let sig = Signature::builder("Answer the question.")
    .input(FieldDef::input("question", FieldType::String, "The question"))
    .output(FieldDef::output("answer", FieldType::String, "The answer"))
    .build()?;

let lm = DummyLM::sequential(vec!["[[ ## answer ## ]]\nParis\n[[ ## completed ## ]]".into()]);
let ctx = Context {
    provider: Arc::new(lm),
    model: ModelId::new("test"),
    adapter: Arc::new(ChatAdapter::default()),
};

let prediction = Predict::new(sig)
    .call(
        &BTreeMap::from([("question".into(), FieldValue::Str("Capital of France?".into()))]),
        &ctx,
    )
    .await?;

assert_eq!(prediction.get::<String>("answer")?, "Paris");

§Modules

Core types (PredictError, Result, FieldType, FieldDef, FieldKind, FieldValue, ObjectField, Signature, SignatureBuilder) live in the typesayer-types crate and are re-exported here.

  • formatFieldSerializer, FieldDeserializer, JsonFieldSerializer, JsonFieldDeserializer
  • adapterAdapter trait, ChatAdapter, Demo
  • predictionPrediction, TryFromFieldValue
  • contextContext
  • predictPredict
  • exampleExample (flat fields + input key separation)
  • moduleModule trait (composition, introspection, state persistence)
  • state — module save/load helpers

Structs§

AwsAccountId
A 12-digit AWS account id. Used as the optional bucket_owner on MediaSource::S3 for cross-account S3Location references.
BootstrapFewShot
Automatic few-shot demo selection via teacher-model bootstrapping.
ChatAdapter
ChatAdapter formats prompts using [[ ## field_name ## ]] section delimiters.
CompileRequest
Per-call inputs for Optimizer::compile.
Context
Holds the language model provider, target model, and adapter for prediction calls.
Demo
A few-shot demonstration example with typed values.
EvaluateConfig
Configuration for the evaluation runner.
EvaluationResult
The result of an evaluation run.
Example
A labeled data point with a declared set of input keys.
ExecutionTrace
An ordered collection of trace entries from a module’s forward pass.
FieldDef
A top-level field definition within a Signature.
HttpsUrl
A URL pinned to the https:// scheme.
JsonFieldDeserializer
JSON-based field deserializer.
JsonFieldSerializer
JSON-based field serializer.
JsonSchemaDefinition
A JSON Schema definition for constructing a Signature at runtime.
LabeledFewShot
Simple few-shot demo assignment from labeled training data.
MIPROv2
MIPROv2 optimizer — joint instruction and demo optimization.
MediaType
An RFC 6838 media type / MIME (type/subtype[;parameters]).
Message
A single message in an LM conversation.
MiproCompileRequest
Per-call inputs for MIPROv2::compile_mipro.
MiproConfig
Pure-value tuning for MIPROv2.
MiproDeps
Behavior-bearing dependencies for MIPROv2. The metric closure is the only thing here — every other knob lives on MiproConfig.
ModelId
Provider model identifier (e.g. "claude-sonnet-4-6", "gpt-4o-mini"). Wraps an arbitrary String so it cannot be passed where an ApiKey is expected.
ObjectField
A field within an Object type.
OneOfDiscriminator
Discriminator hint for a tagged OneOf.
Predict
Orchestrates a structured prediction: format a prompt from a signature, call a language model, and parse the response into typed fields.
Prediction
The result of a Predict::call() invocation.
Progress
A progress update from an optimizer.
ProviderFileId
An opaque, provider-issued file identifier (Anthropic Files API, OpenAI Files API).
S3Uri
A canonical AWS S3 URI: s3://<bucket>/<key>.
Signature
A signature defines the input/output contract for a prediction.
SignatureBuilder
Builder for constructing a Signature.
TraceEntry
A single predictor invocation record.
VariantArm
One arm of a OneOf or AnyOf.

Enums§

AutoMode
Auto mode presets matching DSPy conventions.
CachePlacement
Where ChatAdapter::format inserts the static-prefix cache breakpoint.
CapabilityError
Reasons a request can fail capability validation, by precision.
ContentPart
A single content part within a message.
FieldKind
Whether a field is an input or output of a signature.
FieldType
The type of a field in a signature.
FieldValue
A typed value extracted from an LM completion or supplied as input.
MediaKind
Bucket for media content parts.
MediaSource
Where the bytes of one media content part come from.
PredictError
Errors that can occur in predict operations.
Role
Message role in a conversation.
SourceKind
Discriminant for MediaSource variants, suitable for use in enumset::EnumSet-backed capability masks (e.g. MediaSupport.sources).

Traits§

Adapter
Converts between signatures with typed input values and LM message sequences.
FieldDeserializer
Deserializes raw text from a completion into a FieldValue.
FieldSerializer
Serializes a FieldValue into text for inclusion in a prompt.
Module
A composable unit of LLM-powered logic.
Optimizer
A prompt optimizer that improves a module’s performance by modifying its predictors’ demos, instructions, or both.
TryFromFieldValue
Convert a FieldValue reference into a concrete Rust type.

Functions§

evaluate
Evaluate a module on a dataset using a metric function.
extract_description
Extract a description from a field schema, with enum value appending and JSON Schema constraint-keyword annotations.
field_type_from_schema
Convert a single JSON Schema definition into a FieldType.
is_field_requiredDeprecated
Check if a field schema has required: false (non-standard per-field boolean).
signature_from_json_schema
Construct a Signature from JSON Schema definitions at runtime.

Type Aliases§

MetricFn
A metric function that scores a prediction against an expected example.
ProgressFn
A progress callback invoked during optimization.
Result
A specialized Result type for predict operations.