pub struct JsonModeConfig {
pub mode: JsonMode,
pub schema: Option<Value>,
pub schema_name: String,
pub strict: bool,
}Expand description
Configuration for JSON mode.
Defines how structured output should be formatted and validated.
Fields§
§mode: JsonModeJSON mode to use
schema: Option<Value>JSON schema for validation (only used for JsonSchema mode)
schema_name: StringName for the schema (used in OpenAI format)
strict: boolWhether to enforce strict schema compliance
Implementations§
Source§impl JsonModeConfig
impl JsonModeConfig
Sourcepub fn json_object() -> Self
pub fn json_object() -> Self
Create a config for simple JSON object mode.
Example:
use ai_lib_rust::structured::JsonModeConfig;
let config = JsonModeConfig::json_object();
let openai_format = config.to_openai_format();
assert_eq!(openai_format["response_format"]["type"], "json_object");Sourcepub fn from_schema(schema: Value, name: impl Into<String>, strict: bool) -> Self
pub fn from_schema(schema: Value, name: impl Into<String>, strict: bool) -> Self
Create a config from a JSON schema.
§Arguments
schema- JSON schema dictionaryname- Schema name (default: “response”)strict- Whether to enforce strict compliance (default: true)
Example:
use ai_lib_rust::structured::{JsonMode, JsonModeConfig};
use serde_json::json;
let schema = json!({
"type": "object",
"properties": {
"name": {"type": "string"}
},
"required": ["name"]
});
let config = JsonModeConfig::from_schema(schema, "test", true);
assert_eq!(config.mode, JsonMode::JsonSchema);Sourcepub fn to_openai_format(&self) -> Value
pub fn to_openai_format(&self) -> Value
Convert to OpenAI API format.
Returns a value suitable for the response_format parameter
in OpenAI’s Chat Completions API.
Example output for JSON mode:
{
"response_format": {
"type": "json_object"
}
}Example output for JSON Schema mode:
{
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "response",
"strict": true,
"schema": { ... }
}
}
}Sourcepub fn to_anthropic_format(&self) -> Value
pub fn to_anthropic_format(&self) -> Value
Convert to Anthropic API format.
Note: Anthropic doesn’t have native JSON mode support. This returns an empty placeholder, and JSON enforcement must be done through system prompt instructions.
Example:
use ai_lib_rust::structured::JsonModeConfig;
use serde_json::json;
let config = JsonModeConfig::json_object();
let anthropic_format = config.to_anthropic_format();
assert_eq!(anthropic_format, json!({}));Trait Implementations§
Source§impl Clone for JsonModeConfig
impl Clone for JsonModeConfig
Source§fn clone(&self) -> JsonModeConfig
fn clone(&self) -> JsonModeConfig
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for JsonModeConfig
impl Debug for JsonModeConfig
Source§impl PartialEq for JsonModeConfig
impl PartialEq for JsonModeConfig
impl Eq for JsonModeConfig
impl StructuralPartialEq for JsonModeConfig
Auto Trait Implementations§
impl Freeze for JsonModeConfig
impl RefUnwindSafe for JsonModeConfig
impl Send for JsonModeConfig
impl Sync for JsonModeConfig
impl Unpin for JsonModeConfig
impl UnwindSafe for JsonModeConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.