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: StringModel 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: boolWhether 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
impl CompletionRequest
Sourcepub fn new(model: impl Into<String>, messages: Vec<Message>) -> Self
pub fn new(model: impl Into<String>, messages: Vec<Message>) -> Self
Create a new completion request with required fields.
Sourcepub fn with_system(self, system: impl Into<String>) -> Self
pub fn with_system(self, system: impl Into<String>) -> Self
Builder method: Set the system prompt.
Sourcepub fn with_tools(self, tools: Vec<ToolDefinition>) -> Self
pub fn with_tools(self, tools: Vec<ToolDefinition>) -> Self
Builder method: Set available tools.
Sourcepub fn with_max_tokens(self, max_tokens: u32) -> Self
pub fn with_max_tokens(self, max_tokens: u32) -> Self
Builder method: Set max tokens.
Sourcepub fn with_temperature(self, temperature: f32) -> Self
pub fn with_temperature(self, temperature: f32) -> Self
Builder method: Set temperature.
Sourcepub fn with_top_p(self, top_p: f32) -> Self
pub fn with_top_p(self, top_p: f32) -> Self
Builder method: Set top-p.
Sourcepub fn with_stop_sequences(self, stop_sequences: Vec<String>) -> Self
pub fn with_stop_sequences(self, stop_sequences: Vec<String>) -> Self
Builder method: Set stop sequences.
Sourcepub fn with_streaming(self) -> Self
pub fn with_streaming(self) -> Self
Builder method: Enable streaming.
Sourcepub fn with_thinking(self, budget_tokens: u32) -> Self
pub fn with_thinking(self, budget_tokens: u32) -> Self
Sourcepub fn with_thinking_config(self, config: ThinkingConfig) -> Self
pub fn with_thinking_config(self, config: ThinkingConfig) -> Self
Builder method: Set extended thinking configuration.
Sourcepub fn without_thinking(self) -> Self
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!Sourcepub fn with_thinking_effort(self, effort: ThinkingEffort) -> Self
pub fn with_thinking_effort(self, effort: ThinkingEffort) -> Self
Sourcepub fn with_json_schema(self, name: impl Into<String>, schema: Value) -> Self
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);Sourcepub fn with_response_format(self, format: StructuredOutput) -> Self
pub fn with_response_format(self, format: StructuredOutput) -> Self
Builder method: Set structured output configuration.
Sourcepub fn with_json_output(self) -> Self
pub fn with_json_output(self) -> Self
Builder method: Enable JSON object output (basic, no schema).
Sourcepub fn with_prediction(self, predicted_content: impl Into<String>) -> Self
pub fn with_prediction(self, predicted_content: impl Into<String>) -> Self
Sourcepub fn with_system_caching(self) -> Self
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.
Sourcepub fn with_system_caching_extended(self) -> Self
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.
Sourcepub fn with_beta_feature(self, feature: BetaFeature) -> Self
pub fn with_beta_feature(self, feature: BetaFeature) -> Self
Builder method: Add a beta feature.
Sourcepub fn with_extended_output(self) -> Self
pub fn with_extended_output(self) -> Self
Builder method: Enable 128K output tokens (Anthropic beta).
Sourcepub fn with_interleaved_thinking(self) -> Self
pub fn with_interleaved_thinking(self) -> Self
Builder method: Enable interleaved thinking (Claude 4 only).
Sourcepub fn with_extra(self, extra: Value) -> Self
pub fn with_extra(self, extra: Value) -> Self
Builder method: Set provider-specific extra options.
Sourcepub fn has_caching(&self) -> bool
pub fn has_caching(&self) -> bool
Check if prompt caching is enabled.
Sourcepub fn has_thinking(&self) -> bool
pub fn has_thinking(&self) -> bool
Check if extended thinking is enabled.
Sourcepub fn has_structured_output(&self) -> bool
pub fn has_structured_output(&self) -> bool
Check if structured output is enabled.
Sourcepub fn anthropic_beta_headers(&self) -> Vec<&str>
pub fn anthropic_beta_headers(&self) -> Vec<&str>
Get the required beta headers for Anthropic.
Trait Implementations§
Source§impl Clone for CompletionRequest
impl Clone for CompletionRequest
Source§fn clone(&self) -> CompletionRequest
fn clone(&self) -> CompletionRequest
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more