Skip to main content

ModelSpec

Struct ModelSpec 

Source
#[non_exhaustive]
pub struct ModelSpec {
Show 14 fields pub id: String, pub provider: ProviderId, pub display_name: String, pub context_window: Option<u32>, pub max_output_tokens: Option<u32>, pub input: Modalities, pub reasoning: ReasoningSupport, pub caching: CacheSupport, pub tools: bool, pub structured_output: bool, pub pricing: Option<Pricing>, pub deprecated: bool, pub sampling: Option<Sampling>, pub compat: Compat,
}
Expand description

One model’s facts: limits, input modalities, the reasoning and caching it takes, and its prices. Catalog builds one per row of its data; ModelSpec::new and the with_* setters build one in code, for a model the catalog does not list, and Catalog::insert adds it.

It serializes field by field and reads back the same. For the override file’s row shape, use Self::to_row_json.

use rig_core::catalog::{Catalog, ModelSpec, Pricing};
use rig_core::providers::registry::ProviderId;

let ollama = ProviderId::catalog("ollama").ok_or("a known vendor")?;
let mut catalog = Catalog::builtin().clone();
catalog.insert(
    ModelSpec::new(ollama, "qwen3:4b")
        .with_context_window(32_768)
        .with_tools(true)
        .with_pricing(Pricing::new(0.0, 0.0)),
);
let qwen = catalog.get(ollama, "qwen3:4b").ok_or("inserted")?.spec;
assert_eq!(qwen.context_window, Some(32_768));

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§id: String

The provider’s own model id.

§provider: ProviderId

The provider serving the model under Self::id, in the protocol family the model is reached by: its vendor’s own (ProviderId::catalog) unless the catalog row names another under rig.format. Self::format reads it.

§display_name: String

A human-readable name.

§context_window: Option<u32>

The most tokens the model reads and writes in one request, if known.

§max_output_tokens: Option<u32>

The most tokens the model writes in one reply, if known.

§input: Modalities

What the model reads.

§reasoning: ReasoningSupport

The reasoning the model takes.

§caching: CacheSupport

The cache retentions the model honours.

§tools: bool

Whether the model calls tools.

§structured_output: bool

Whether the model constrains its output to a JSON schema.

§pricing: Option<Pricing>

Prices in USD per million tokens, if known.

§deprecated: bool

Whether the provider has deprecated the model.

§sampling: Option<Sampling>

When the model takes sampling parameters (temperature, top_p, top_logprobs, logprobs), or None when unknown.

§compat: Compat

Wire facts the encoders read that no portable field holds.

Implementations§

Source§

impl ModelSpec

Source

pub fn new(provider: ProviderId, id: impl Into<String>) -> Self

A spec for provider’s model id with nothing else known: its id as its name, no limits, text input only, no reasoning, unknown caching, no tools or structured output, no prices (None, never zero), not deprecated, unknown sampling and default Compat.

Source

pub fn format(&self) -> Option<Format>

The protocol family a connection to this model speaks unless told otherwise, or None for a provider the registry cannot configure. To reach the model in another family its vendor is registered for, build the spec with that ProviderId or pass the family to ConnectOptions::format.

Source

pub fn reference(&self) -> String

The vendor/model reference Catalog::resolve reads back to this model.

Source

pub fn default_options(&self, reasoning: Option<Reasoning>) -> GenerationOptions

The options of a request to this model with reasoning (None for the provider’s default): that reasoning, and the short prompt cache when Self::caching lists it. A model whose caching the catalog does not know gets no cache option, which it could refuse.

Source

pub fn with_display_name(self, name: impl Into<String>) -> Self

This spec named name.

Source

pub fn with_context_window(self, tokens: u32) -> Self

This spec with a context window of tokens.

Source

pub fn with_max_output_tokens(self, tokens: u32) -> Self

This spec writing at most tokens in one reply.

Source

pub fn with_reasoning(self, reasoning: ReasoningSupport) -> Self

This spec taking reasoning.

Source

pub fn with_caching(self, caching: CacheSupport) -> Self

This spec honouring caching.

Source

pub fn with_tools(self, tools: bool) -> Self

This spec with tool calling set to tools.

Source

pub fn with_pricing(self, pricing: Pricing) -> Self

This spec priced at pricing.

Source

pub fn with_sampling(self, sampling: Sampling) -> Self

This spec taking sampling parameters as sampling says.

Source

pub fn with_compat(self, compat: Compat) -> Self

This spec with the wire facts compat.

Source

pub fn to_row_json(&self) -> Value

This spec as one models.dev-shaped row of an override file, with rig’s facts under rig. Read back under its vendor key, the row builds this spec again. A fact the spec does not know (a None limit, price, sampling rule or reasoning default, or empty caching) is left out, so laid over another row it keeps that row’s value.

Source

pub fn validate( &self, options: &GenerationOptions, ) -> Result<(), UnsupportedOption>

Checks options against the rules Self::refusals applies, and returns the first refusal. It checks only what options holds, so it never refuses temperature, which a request carries outside its options; Self::refusals checks the whole request.

§Errors

The first option the model cannot take. The error names the option, the provider’s vendor and this model.

Source

pub fn refusals(&self, request: &CompletionRequest) -> Vec<UnsupportedOption>

Every option of request the model cannot take, by the rules every completion wire also applies before it encodes, so a request this refuses is refused by the model’s wire too:

A request whose GenerationOptions are default is not checked, as GenerationOptions::is_default says, so this is empty for it. A wire refuses more than this: what its route or API cannot carry (DynModel::check reports both).

Source

pub fn fit(&self, request: &mut CompletionRequest) -> Vec<UnsupportedOption>

Fits request to this model: takes out each option it would refuse (Self::refusals) and leaves everything else as it is, so a request built or edited for another model keeps what this one takes. A refused reasoning setting becomes the provider default (None) first, since whether the model reasons decides whether it takes temperature and top_p. Returns what was taken out, in that order; afterwards Self::refusals of the request is empty.

use rig_core::catalog::{ModelSpec, ReasoningSupport};
use rig_core::completion::{CompletionRequest, Effort, GenerationOptions, Reasoning};
use rig_core::providers::registry::ProviderId;

let vendor = ProviderId::catalog("ollama").ok_or("a known vendor")?;
let plain = ModelSpec::new(vendor, "plain").with_reasoning(ReasoningSupport::None);
let options = GenerationOptions::default().reasoning(Reasoning::Effort(Effort::High));
let mut request = CompletionRequest::new("hi").options(options.seed(7));
let dropped = plain.fit(&mut request);
assert_eq!(dropped.len(), 1);
assert_eq!((request.options.reasoning, request.options.seed), (None, Some(7)));

Trait Implementations§

Source§

impl Clone for ModelSpec

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModelSpec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ModelSpec

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a> From<&'a ModelSpec> for ModelSelector<'a>

Source§

fn from(spec: &'a ModelSpec) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for ModelSpec

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for ModelSpec

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for ModelSpec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoToolOutput for T
where T: Serialize + 'static,

Source§

fn into_tool_output(self) -> Result<ToolOutput, ToolExecutionError>

Convert this value without routing structured data through a string.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more