Skip to main content

objectiveai_sdk/agent/completions/request/
response_format.rs

1//! Response format configuration for agent completions.
2
3use std::hash::{Hash, Hasher};
4use indexmap::IndexMap;
5use serde::{Deserialize, Serialize};
6use schemars::JsonSchema;
7
8/// The format of the model's response.
9#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
10#[serde(tag = "type", rename_all = "snake_case")]
11#[schemars(rename = "agent.completions.request.ResponseFormat")]
12pub enum ResponseFormat {
13    /// Plain text response (default).
14    #[schemars(title = "Text")]
15    Text,
16    /// Response must be valid JSON.
17    #[schemars(title = "JsonObject")]
18    JsonObject,
19    /// Response must conform to a JSON schema.
20    #[schemars(title = "JsonSchema")]
21    JsonSchema {
22        /// The JSON Schema definition.
23        schema: IndexMap<String, serde_json::Value>,
24    },
25    /// Response must conform to a grammar.
26    #[schemars(title = "Grammar")]
27    Grammar { grammar: String },
28    /// Response must be valid Python code.
29    #[schemars(title = "Python")]
30    Python,
31    /// The final assistant message will contain this tool call
32    #[schemars(title = "ToolCall")]
33    ToolCall {
34        /// The name of the tool.
35        name: String,
36        /// A description of the tool.
37        description: String,
38        /// The JSON Schema definition.
39        schema: IndexMap<String, serde_json::Value>,
40        /// Whether the tool MUST be called.
41        #[serde(skip_serializing_if = "Option::is_none")]
42        #[schemars(extend("omitempty" = true))]
43        required: Option<bool>,
44    },
45}
46
47impl ResponseFormat {
48    pub fn tool_key(&self) -> String {
49        let json = serde_json::to_string(self).unwrap_or_default();
50        let mut h = std::collections::hash_map::DefaultHasher::new();
51        json.hash(&mut h);
52        format!("{:x}", h.finish())
53    }
54}
55
56/// A map from agent ID to response format, allowing per-agent configuration.
57pub type PerAgentResponseFormat = IndexMap<String, ResponseFormat>;
58
59/// Either a single response format or a per-agent map.
60#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
61#[serde(untagged)]
62#[schemars(rename = "agent.completions.request.ResponseFormatParam")]
63pub enum ResponseFormatParam {
64    /// A single response format applied to all agents.
65    #[schemars(title = "Single")]
66    Single(ResponseFormat),
67    /// Per-agent response formats, keyed by agent ID.
68    #[schemars(title = "PerAgent")]
69    PerAgent(PerAgentResponseFormat),
70}