Skip to main content

CompletionRequest

Struct CompletionRequest 

Source
#[non_exhaustive]
pub struct CompletionRequest {
Show 13 fields pub model: Option<String>, pub chat_history: Vec<Message>, pub documents: Vec<Document>, pub tools: Vec<ToolDefinition>, pub temperature: Option<f64>, pub max_tokens: Option<u64>, pub tool_choice: Option<ToolChoice>, pub additional_params: Option<Value>, pub output_schema: Option<Schema>, pub record_telemetry_content: bool, pub accept_unknown_finish_reasons: bool, pub options: GenerationOptions, pub provider_options: ProviderOptions,
}
Expand description

Struct representing a general completion request that can be sent to a completion model provider.

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.
§model: Option<String>

Optional model override for this request.

§chat_history: Vec<Message>

The chat history to be sent to the completion model provider. The very last message is the prompt.

It must hold at least one message, and every user and assistant message must carry content. The field is public, so this is a rule rather than a type guarantee: Self::validate_message_content checks it at the request boundary.

§documents: Vec<Document>

The documents to be sent to the completion model provider

§tools: Vec<ToolDefinition>

The tools to be sent to the completion model provider

§temperature: Option<f64>

The temperature to be sent to the completion model provider

§max_tokens: Option<u64>

The max tokens to be sent to the completion model provider

§tool_choice: Option<ToolChoice>

Whether tools are required to be used by the model provider or not before providing a response.

§additional_params: Option<Value>

Additional provider-specific parameters to be sent to the completion model provider

§output_schema: Option<Schema>

Optional JSON Schema for structured output. When set, providers that support native structured outputs will constrain the model’s response to match this schema.

§record_telemetry_content: bool

Opt-in for sensitive request, response, and tool-content telemetry. Defaults to false and is excluded from serialization. Enabling it can expose prompts, context, tool results, and model output in span attributes and increase telemetry storage costs. Requires explicit caller consent. Agent drivers record normalized content; direct provider coverage varies, especially for streams consumed after the provider returns.

§accept_unknown_finish_reasons: bool

Whether a finish reason outside the normalized vocabulary (FinishReason::Other) ends the turn as a normal stop instead of a failure. Defaults to false. The response carries the choice, so its CompletionResponse::stop, the runtimes and history replay agree. Other also holds genuine failures, such as a malformed tool call or a recitation block, so with this set the tool calls of such a reply run. FinishReason::ContentFilter still fails the turn.

§options: GenerationOptions

Portable generation options. Precedence, lowest first: the mapped options, then Self::provider_options, then additional_params.

§provider_options: ProviderOptions

Typed per-provider options. The wire reads only the entry of its own provider, above the mapped options and below additional_params.

Implementations§

Source§

impl CompletionRequest

Source

pub fn system_instructions(&self) -> Option<&str>

The system instructions of this request: the content of the leading Message::System in chat_history, which is where Self::preamble places it.

Source

pub fn validate_message_content(&self) -> Result<(), ProviderError>

Reject a request with no messages, a user or assistant message with no content, or a tool result with no content blocks. The error is ProviderError::Request and names the role and index of the first offending message.

Every wire rejects an empty turn, so this turns a remote 400 into a local error. It checks the request direction only: a provider may return empty assistant content, which the reply keeps and the runtime judges. System content is a String and is not checked. A tool result holding one empty text block is not empty.

Model::call, Model::stream and their _observed twins run it before encoding, so it covers DynModel, every model the bus serves and the agent runtimes built on them. The OpenAI Responses websocket session sends without the driver and runs it on each send. Code that encodes a request some other way should call it first.

Source

pub fn output_schema_name(&self) -> Option<String>

Extracts a name from the output schema’s "title" field, falling back to "response_schema". Useful for providers that require a name alongside the JSON Schema (e.g., OpenAI).

Source

pub fn normalized_documents(&self) -> Option<Message>

Returns documents normalized into a message (if any). Most providers do not accept documents directly as input, so it needs to convert into a Message so that it can be incorporated into chat_history.

Source§

impl CompletionRequest

Source

pub fn new(prompt: impl Into<Message>) -> Self

A request whose conversation is the one user message prompt, with no preamble, documents or tools. The setters below add to it and check nothing; Self::validate_message_content checks the content when the request is sent.

Each setter changes the request’s public fields as it is called, so order matters where two setters touch the same field: a second Self::preamble adds a second system message, and Self::additional_params with a tools key (or None) replaces provider tools added before it. Set additional_params first.

use rig_core::completion::CompletionRequest;

let request = CompletionRequest::new("Who are you?")
    .preamble("You are a concise assistant.")
    .temperature(0.5);
assert_eq!(request.chat_history.len(), 2);
assert_eq!(request.temperature, Some(0.5));
Source

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

Put preamble first in the conversation, as a Message::System, ahead of any system message already there.

Source

pub fn model<S: Into<String>>(self, model: impl Into<Option<S>>) -> Self

Override the model for this request.

Source

pub fn message(self, message: Message) -> Self

Add message to the conversation, before the prompt (its last message).

Source

pub fn messages(self, messages: impl IntoIterator<Item = Message>) -> Self

Add messages to the conversation in order, before the prompt (its last message).

Source

pub fn document(self, document: Document) -> Self

Add a document.

Source

pub fn documents(self, documents: impl IntoIterator<Item = Document>) -> Self

Add documents in order.

Source

pub fn tool(self, tool: ToolDefinition) -> Self

Add a tool.

Source

pub fn tools(self, tools: Vec<ToolDefinition>) -> Self

Add tools in order.

Source

pub fn provider_tool(self, tool: ProviderToolDefinition) -> Self

Add a provider-hosted tool: appended to additional_params.tools, so a later Self::additional_params with a tools key replaces it.

Source

pub fn provider_tools(self, tools: Vec<ProviderToolDefinition>) -> Self

Add provider-hosted tools in order: appended to additional_params.tools.

Source

pub fn additional_params( self, additional_params: impl Into<Option<Value>>, ) -> Self

Merge provider-specific parameters into the request’s, key by key; None clears them, provider tools included. Provider conversion determines precedence over typed fields, and a key that overrides a typed field this request sets is logged.

Source

pub fn temperature(self, temperature: impl Into<Option<f64>>) -> Self

Set, or with None clear, the temperature.

Source

pub fn max_tokens(self, max_tokens: impl Into<Option<u64>>) -> Self

Set, or with None clear, the output-token limit. Provider-specific defaults and requirements apply.

Source

pub fn tool_choice(self, tool_choice: ToolChoice) -> Self

Set the tool-selection policy.

Source

pub fn output_schema(self, schema: impl Into<Option<Schema>>) -> Self

Set, or with None clear, a native structured-output schema for providers that support one. The returned content is not deserialized.

Source

pub fn record_content_telemetry(self, enabled: bool) -> Self

Opt in to sensitive content telemetry, off by default. See Self::record_telemetry_content for what that exposes.

Source

pub fn accept_unknown_finish_reasons(self, accept: bool) -> Self

Accept, or with false refuse, finish reasons outside the normalized vocabulary as a normal stop. See Self::accept_unknown_finish_reasons for what that lets through.

Source

pub fn options(self, options: GenerationOptions) -> Self

Replace the portable generation options with options, a reusable value. Calls apply in order: this replaces every field, so a shortcut such as Self::seed called before it is lost, and one called after it sets its one field on top.

use rig_core::completion::{CompletionRequest, Effort, GenerationOptions};

let shared = GenerationOptions::new().reasoning(Effort::High).seed(1);
let request = CompletionRequest::new("hi").seed(7).options(shared.clone()).seed(2);
assert_eq!(request.options, shared.seed(2));
Source

pub fn reasoning(self, reasoning: impl Into<Reasoning>) -> Self

Set the reasoning level or budget in Self::options, as GenerationOptions::reasoning does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn cache(self, cache: CacheRetention) -> Self

Set the cache retention in Self::options, as GenerationOptions::cache does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn service_tier(self, tier: ServiceTier) -> Self

Set the service tier in Self::options, as GenerationOptions::service_tier does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn verbosity(self, verbosity: Verbosity) -> Self

Set the answer verbosity in Self::options, as GenerationOptions::verbosity does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn parallel_tool_calls(self, parallel: bool) -> Self

Set whether the model may call several tools in one turn in Self::options, as GenerationOptions::parallel_tool_calls does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn top_p(self, top_p: f64) -> Self

Set the nucleus sampling probability mass in Self::options, as GenerationOptions::top_p does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn seed(self, seed: u64) -> Self

Set the sampling seed in Self::options, as GenerationOptions::seed does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn stop<S: Into<String>>(self, stop: impl IntoIterator<Item = S>) -> Self

Set the stop sequences in Self::options, as GenerationOptions::stop does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn on_unsupported(self, policy: OnUnsupported) -> Self

Set what happens to an option the wire or model cannot honour in Self::options, as GenerationOptions::on_unsupported does, keeping its other fields. See Self::options for the order of calls.

Source

pub fn provider_options(self, options: ProviderOptions) -> Self

Replace the typed per-provider options.

Calls apply in order: this replaces every entry, so an entry set by an earlier Self::provider_option is lost, and a later Self::provider_option replaces its provider’s entry on top.

Source

pub fn provider_option<O: ExtensionOptions>(self, options: O) -> Self

Store options as the entry of their provider (ExtensionOptions::Ext), replacing that provider’s entry and keeping every other, as ProviderOptions::set does. Options that do not serialize fail the request’s encode.

The entry is always stored under O::Ext’s key, the built-in provider the options type belongs to. A third-party provider whose extension reuses a built-in options type (say OpenAiOptions for an OpenAI-compatible gateway) must store them with ProviderOptions::with::<P> instead, or its wire never reads them.

use rig_core::completion::{CompletionRequest, ProviderOptions};
use rig_core::providers::openrouter::extension::{
    OpenRouterExt, OpenRouterOptions, ProviderPreferences,
};

let request = CompletionRequest::new("hi").provider_option(
    OpenRouterOptions::new().provider(ProviderPreferences::new().allow_fallbacks(false)),
);
assert!(request.provider_options.contains::<OpenRouterExt>());
Source

pub fn messages_for_telemetry(&self) -> Vec<Message>

The input messages telemetry records: the conversation with the documents inserted after any leading system messages.

Trait Implementations§

Source§

impl Clone for CompletionRequest

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 CompletionRequest

Source§

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

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

impl<'de> Deserialize<'de> for CompletionRequest

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 From<&str> for CompletionRequest

Source§

fn from(prompt: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Message> for CompletionRequest

Source§

fn from(prompt: Message) -> Self

Converts to this type from the input type.
Source§

impl From<String> for CompletionRequest

Source§

fn from(prompt: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Message>> for CompletionRequest

The conversation as given, ending with the prompt. An empty one fails CompletionRequest::validate_message_content.

Source§

fn from(chat_history: Vec<Message>) -> Self

Converts to this type from the input type.
Source§

impl Serialize for CompletionRequest

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

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