Skip to main content

ollama_oxide/inference/
format_setting.rs

1//! Format setting primitive type
2
3use serde::{Deserialize, Serialize};
4
5/// Format setting for generate requests
6///
7/// Controls the output format of the model response.
8/// Can be a string (like "json") or a JSON schema object.
9///
10/// # Examples
11///
12/// ```no_run
13/// use ollama_oxide::FormatSetting;
14///
15/// let json = FormatSetting::json();
16/// let schema = FormatSetting::schema(serde_json::json!({
17///     "type": "object",
18///     "properties": { "name": { "type": "string" } }
19/// }));
20/// ```
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22#[serde(untagged)]
23pub enum FormatSetting {
24    /// Simple format string (e.g., "json")
25    String(String),
26    /// JSON schema object for structured output
27    Schema(serde_json::Value),
28}
29
30impl FormatSetting {
31    /// Create JSON format
32    pub fn json() -> Self {
33        Self::String("json".to_string())
34    }
35
36    /// Create custom format string
37    pub fn string(format: impl Into<String>) -> Self {
38        Self::String(format.into())
39    }
40
41    /// Create schema-based format
42    pub fn schema(schema: serde_json::Value) -> Self {
43        Self::Schema(schema)
44    }
45}
46
47impl From<&str> for FormatSetting {
48    fn from(s: &str) -> Self {
49        Self::String(s.to_string())
50    }
51}
52
53impl From<serde_json::Value> for FormatSetting {
54    fn from(v: serde_json::Value) -> Self {
55        Self::Schema(v)
56    }
57}