CompletionRequest

Struct CompletionRequest 

Source
pub struct CompletionRequest {
Show 15 fields pub model: String, pub messages: Vec<Message>, pub system: Option<String>, pub tools: Option<Vec<ToolDefinition>>, pub max_tokens: Option<u32>, pub temperature: Option<f32>, pub top_p: Option<f32>, pub stop_sequences: Option<Vec<String>>, pub stream: bool, pub thinking: Option<ThinkingConfig>, pub response_format: Option<StructuredOutput>, pub prediction: Option<PredictionConfig>, pub system_cache_control: Option<CacheBreakpoint>, pub beta_features: Option<Vec<BetaFeature>>, pub extra: Option<Value>,
}
Expand description

Request to complete a conversation.

Fields§

§model: String

Model identifier (e.g., “claude-sonnet-4-20250514”, “gpt-4o”)

§messages: Vec<Message>

Conversation messages

§system: Option<String>

System prompt (separate from messages for providers that support it)

§tools: Option<Vec<ToolDefinition>>

Available tools for the model to use

§max_tokens: Option<u32>

Maximum tokens to generate

§temperature: Option<f32>

Sampling temperature (0.0 to 2.0)

§top_p: Option<f32>

Top-p sampling

§stop_sequences: Option<Vec<String>>

Stop sequences

§stream: bool

Whether to stream the response

§thinking: Option<ThinkingConfig>

Extended thinking configuration (Anthropic Claude 3.7+)

§response_format: Option<StructuredOutput>

Structured output configuration (OpenAI, Google)

§prediction: Option<PredictionConfig>

Predicted output for speculative decoding (OpenAI)

§system_cache_control: Option<CacheBreakpoint>

Cache control for the system prompt (Anthropic)

§beta_features: Option<Vec<BetaFeature>>

Beta features to enable (Anthropic)

§extra: Option<Value>

Provider-specific options

Implementations§

Source§

impl CompletionRequest

Source

pub fn new(model: impl Into<String>, messages: Vec<Message>) -> Self

Create a new completion request with required fields.

Source

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

Builder method: Set the system prompt.

Source

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

Builder method: Set available tools.

Source

pub fn with_max_tokens(self, max_tokens: u32) -> Self

Builder method: Set max tokens.

Source

pub fn with_temperature(self, temperature: f32) -> Self

Builder method: Set temperature.

Source

pub fn with_top_p(self, top_p: f32) -> Self

Builder method: Set top-p.

Source

pub fn with_stop_sequences(self, stop_sequences: Vec<String>) -> Self

Builder method: Set stop sequences.

Source

pub fn with_streaming(self) -> Self

Builder method: Enable streaming.

Source

pub fn with_thinking(self, budget_tokens: u32) -> Self

Builder method: Enable extended thinking with a token budget.

Extended thinking allows Claude to reason more deeply about complex problems. Available on Claude 3.7+ models.

§Example
let request = CompletionRequest::new(model, messages)
    .with_thinking(10000);  // 10k token budget for thinking
Source

pub fn with_thinking_config(self, config: ThinkingConfig) -> Self

Builder method: Set extended thinking configuration.

Source

pub fn without_thinking(self) -> Self

Builder method: Disable thinking/reasoning.

Useful for getting faster, cheaper responses from reasoning models like Qwen3, DeepSeek-R1, or when using OpenRouter’s reasoning control.

§Example
let request = CompletionRequest::new(model, messages)
    .without_thinking()
    .with_max_tokens(100);  // Now 100 tokens is enough!
Source

pub fn with_thinking_effort(self, effort: ThinkingEffort) -> Self

Builder method: Set thinking effort level.

Controls how much reasoning effort the model uses. Supported by OpenRouter and similar providers.

§Example
let request = CompletionRequest::new(model, messages)
    .with_thinking_effort(ThinkingEffort::Low);
Source

pub fn with_json_schema(self, name: impl Into<String>, schema: Value) -> Self

Builder method: Set structured output with JSON schema.

Guarantees the model output adheres to the specified JSON schema. Supported by OpenAI with 100% reliability.

§Example
let schema = serde_json::json!({
    "type": "object",
    "properties": {"name": {"type": "string"}},
    "required": ["name"]
});
let request = CompletionRequest::new(model, messages)
    .with_json_schema("response", schema);
Source

pub fn with_response_format(self, format: StructuredOutput) -> Self

Builder method: Set structured output configuration.

Source

pub fn with_json_output(self) -> Self

Builder method: Enable JSON object output (basic, no schema).

Source

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

Builder method: Set predicted output for speculative decoding.

Speeds up generation when much of the output is already known. Useful for code editing, document updates, etc.

§Example
let request = CompletionRequest::new(model, messages)
    .with_prediction(existing_code);
Source

pub fn with_system_caching(self) -> Self

Builder method: Enable prompt caching for the system prompt.

Caches the system prompt for 5 minutes to reduce costs on subsequent calls. Available on Anthropic Claude models.

Source

pub fn with_system_caching_extended(self) -> Self

Builder method: Enable extended (1-hour) prompt caching for the system prompt.

Requires the extended-cache-ttl beta feature.

Source

pub fn with_beta_feature(self, feature: BetaFeature) -> Self

Builder method: Add a beta feature.

Source

pub fn with_extended_output(self) -> Self

Builder method: Enable 128K output tokens (Anthropic beta).

Source

pub fn with_interleaved_thinking(self) -> Self

Builder method: Enable interleaved thinking (Claude 4 only).

Source

pub fn with_extra(self, extra: Value) -> Self

Builder method: Set provider-specific extra options.

Source

pub fn has_caching(&self) -> bool

Check if prompt caching is enabled.

Source

pub fn has_thinking(&self) -> bool

Check if extended thinking is enabled.

Source

pub fn has_structured_output(&self) -> bool

Check if structured output is enabled.

Source

pub fn anthropic_beta_headers(&self) -> Vec<&str>

Get the required beta headers for Anthropic.

Trait Implementations§

Source§

impl Clone for CompletionRequest

Source§

fn clone(&self) -> CompletionRequest

Returns a duplicate of the value. Read more
1.0.0 · 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 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<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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<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
Source§

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