xai-openapi 0.1.1

Rust types for the xAI API (Grok models)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Responses API types for `/v1/responses` endpoint.

use serde::{Deserialize, Serialize};

use crate::prelude::*;

use crate::common::{DebugOutput, IncompleteDetails};
use crate::search::SearchParameters;
use crate::tools::{
    CodeInterpreterCall, CustomToolCall, FileSearchCall, FunctionToolCall, FunctionToolCallOutput,
    McpCall, ModelTool, ModelToolChoice, OutputMessageContent, WebSearchCall,
};
use crate::usage::ModelUsage;

/// The request body for `/v1/responses` endpoint.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ModelRequest {
    /// The input passed to the model. Can be text, image or file.
    pub input: ModelInput,

    /// Model name for the model to use.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,

    /// An alternate way to specify the system prompt.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,

    /// What sampling temperature to use, between 0 and 2.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// An alternative to sampling with temperature, called nucleus sampling.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    /// Max number of tokens that can be generated in a response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_output_tokens: Option<i32>,

    /// If set, partial message deltas will be sent as server-sent events.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    /// Whether to return log probabilities of the output tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<bool>,

    /// Whether to store the input message(s) and model response for later retrieval.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub store: Option<bool>,

    /// The ID of the previous response from the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_response_id: Option<String>,

    /// A list of tools the model may call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<ModelTool>>,

    /// Controls which (if any) tool is called by the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ModelToolChoice>,

    /// Whether to allow the model to run parallel tool calls.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel_tool_calls: Option<bool>,

    /// Set the parameters to be used for searched data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_parameters: Option<SearchParameters>,

    /// Reasoning configuration. Only for reasoning models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<ReasoningConfiguration>,

    /// Settings for customizing a text response from the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<ModelResponseConfiguration>,

    /// What additional output data to include in the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include: Option<Vec<String>>,

    /// Not supported. Only maintained for compatibility reasons.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,

    /// Not supported. Only maintained for compatibility reasons.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<String>,

    /// (Unsupported) Whether to process the response asynchronously in the background.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<bool>,
}

/// Content of the input passed to a `/v1/responses` request.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ModelInput {
    /// Text input.
    Text(String),
    /// A list of input items to the model.
    Parts(Vec<ModelInputPart>),
}

impl Default for ModelInput {
    fn default() -> Self {
        ModelInput::Text(String::new())
    }
}

/// A part of model input.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ModelInputPart {
    /// Message input to the model.
    Message {
        /// The role of the message.
        role: String,
        /// Text, image or audio input.
        content: ModelInputContent,
        /// A unique identifier representing your end-user.
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        /// The type of the message, which is always `message`.
        #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
        message_type: Option<String>,
    },
    /// Previous responses of the model and tool call outputs.
    Item(ModelInputItem),
}

impl Default for ModelInputPart {
    fn default() -> Self {
        ModelInputPart::Message {
            role: "user".to_string(),
            content: ModelInputContent::default(),
            name: None,
            message_type: None,
        }
    }
}

/// Model input content.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ModelInputContent {
    /// Text input.
    Text(String),
    /// A list of input items to the model.
    Parts(Vec<ModelInputContentItem>),
}

impl Default for ModelInputContent {
    fn default() -> Self {
        ModelInputContent::Text(String::new())
    }
}

/// A model input content item.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ModelInputContentItem {
    /// Text input.
    InputText {
        /// Text input.
        text: String,
    },
    /// Image input.
    InputImage {
        /// A public URL of image prompt, only available for vision models.
        image_url: String,
        /// Specifies the detail level of the image.
        #[serde(skip_serializing_if = "Option::is_none")]
        detail: Option<String>,
        /// Only included for compatibility.
        #[serde(skip_serializing_if = "Option::is_none")]
        file_id: Option<String>,
    },
    /// File input.
    InputFile {
        /// The file ID from the Files API.
        file_id: String,
    },
}

impl Default for ModelInputContentItem {
    fn default() -> Self {
        ModelInputContentItem::InputText {
            text: String::new(),
        }
    }
}

/// Model input item (previous outputs or tool call outputs).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ModelInputItem {
    /// Previous model output.
    Output(ModelOutput),
    /// Function tool call output.
    FunctionOutput(FunctionToolCallOutput),
}

impl Default for ModelInputItem {
    fn default() -> Self {
        ModelInputItem::FunctionOutput(FunctionToolCallOutput::default())
    }
}

/// The response body for `/v1/responses` endpoint.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ModelResponse {
    /// Unique ID of the response.
    pub id: String,

    /// The object type of this resource. Always set to `response`.
    pub object: String,

    /// The Unix timestamp (in seconds) for the response creation time.
    pub created_at: i64,

    /// Model name used to generate the response.
    pub model: String,

    /// The response generated by the model.
    pub output: Vec<ModelOutput>,

    /// Whether to allow the model to run parallel tool calls.
    pub parallel_tool_calls: bool,

    /// Settings for customizing a text response from the model.
    pub text: ModelResponseConfiguration,

    /// Controls which (if any) tool is called by the model.
    pub tool_choice: ModelToolChoice,

    /// A list of tools the model may call.
    pub tools: Vec<ModelTool>,

    /// Status of the response. One of `completed`, `in_progress` or `incomplete`.
    pub status: String,

    /// Whether to store the input message(s) and model response for later retrieval.
    pub store: bool,

    /// Only included for compatibility.
    pub metadata: serde_json::Value,

    /// Token usage information.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<ModelUsage>,

    /// What sampling temperature to use, between 0 and 2.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// An alternative to sampling with temperature, called nucleus sampling.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    /// Max number of tokens that can be generated in a response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_output_tokens: Option<i32>,

    /// The ID of the previous response from the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_response_id: Option<String>,

    /// Reasoning configuration. Only for reasoning models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<ReasoningConfiguration>,

    /// A unique identifier representing your end-user.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,

    /// Details about why the response is incomplete.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub incomplete_details: Option<IncompleteDetails>,

    /// Whether to process the response asynchronously in the background.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<bool>,

    /// Debug output. Only available to trusted testers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub debug_output: Option<DebugOutput>,
}

/// Model output.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ModelOutput {
    /// An output message from the model.
    Message(OutputMessage),
    /// A function tool call.
    FunctionCall(FunctionToolCall),
    /// Reasoning done by the model.
    Reasoning(Reasoning),
    /// A web search tool call.
    WebSearch(WebSearchCall),
    /// A file search tool call.
    FileSearch(FileSearchCall),
    /// A code interpreter tool call.
    CodeInterpreter(CodeInterpreterCall),
    /// A MCP tool call.
    Mcp(McpCall),
    /// A custom tool call.
    Custom(CustomToolCall),
}

impl Default for ModelOutput {
    fn default() -> Self {
        ModelOutput::Message(OutputMessage::default())
    }
}

/// An output message from the model.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct OutputMessage {
    /// The type of the output message, which is always `message`.
    #[serde(rename = "type")]
    pub message_type: String,

    /// The role of the output message, which can be `assistant` or `tool`.
    pub role: String,

    /// Content of the output message.
    pub content: Vec<OutputMessageContent>,

    /// The unique ID of the output message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// Status of the item. One of `completed`, `in_progress` or `incomplete`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// Reasoning configuration.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ReasoningConfiguration {
    /// Constrains how hard a reasoning model thinks before responding.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effort: Option<String>,

    /// A summary of the model's reasoning process.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,

    /// Only included for compatibility.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub generate_summary: Option<String>,
}

/// The reasoning done by the model.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Reasoning {
    /// The type of the object, which is always `reasoning`.
    #[serde(rename = "type")]
    pub reasoning_type: String,

    /// The reasoning text contents.
    pub summary: Vec<ReasoningText>,

    /// The unique ID of the reasoning content.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// The encrypted reasoning.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encrypted_content: Option<String>,

    /// Status of the item. One of `completed`, `in_progress` or `incomplete`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// Reasoning text.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ReasoningText {
    /// The type of the object, which is always `summary_text`.
    #[serde(rename = "type")]
    pub text_type: String,

    /// Reasoning done by the model.
    pub text: String,
}

/// Settings for customizing a text response from the model.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ModelResponseConfiguration {
    /// An object specifying the format that the model must output.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<ModelResponseFormat>,
}

/// Response format parameter for structured outputs (Responses API).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ModelResponseFormat {
    /// Specify text response format, always `"text"`.
    #[default]
    Text,
    /// Specify `json_object` response format.
    JsonObject,
    /// Specify `json_schema` response format with a given schema.
    JsonSchema {
        /// A json schema representing the desired response schema.
        schema: serde_json::Value,
        /// Only included for compatibility.
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        /// Only included for compatibility.
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        /// Only included for compatibility.
        #[serde(skip_serializing_if = "Option::is_none")]
        strict: Option<bool>,
    },
}

/// Response to delete a stored completion.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct DeleteStoredCompletionResponse {
    /// The `response_id` to be deleted.
    pub id: String,

    /// The deleted object type, which is always `response`.
    pub object: String,

    /// Whether the response was successfully deleted.
    pub deleted: bool,
}