google-ai-schema-derive

Crates.iogoogle-ai-schema-derive
lib.rsgoogle-ai-schema-derive
version
sourcesrc
created_at2025-05-07 20:16:18.127125+00
updated_at2025-05-07 20:16:18.127125+00
descriptionType-safe schema generation for Google AI API interactions
homepage
repositoryhttps://github.com/veecore/google-ai-rs
max_upload_size
id1664425
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Victor (veecore)

documentation

https://docs.rs/google-ai-schema-derive

README

Schema-Derive for Google AI (Rust)

Type-Safe Schema Generation for Gemini API Interactions

Purpose-Built for Google AI

1. Gemini Schema Compliance

#[derive(AsSchema, Deserialize)]
#[schema(description = "Chemical element analysis")]
struct ElementAnalysis {
    #[schema(description = "Element symbol")]
    symbol: String,
    
    #[schema(description = "Atomic mass with units")]
    #[serde(rename = "atomicMass")]
    mass: String,
}

Generates JSON schemas that exactly match Gemini's API requirements:

  • Automatic description propagation

  • Google-specific type validations

  • Required field enforcement

3. Optimized Serde Interop

#[derive(AsSchema, Serialize)]
#[schema(rename_all = "kebab-case")]
struct AnalysisRequest {
    #[serde(rename = "inputData")] // Mirrored in schema
    input_data: String,
}
  • Automatic alignment with Serde's rename/skip

Core Features

Schema Attributes

description required format rename ignore_serde rename_all nullable type as_schema required skip

Usage Example

#[derive(AsSchema, Deserialize)]
#[schema(rename_all = "camelCase")]
#[schema(description = "API response structure")]
struct GeminiResponse {
    #[schema(description = "Confidence score 0-1")]
    confidence: f64,
    
    #[schema(
        description = "MIME type validated content",
    )]
    content_type: String,
}

// Automatically generates compatible schema:
/*
{
  "type": "object",
  "title": "GeminiResponse",
  "description": "API response structure",
  "properties": {
    "confidence": {
      "type": "number",
      "format": "float"
      "description": "Confidence score 0-1"
    },
    "contentType": {
      "type": "string",
      "description": "MIME type validated content",
    }
  },
  "required": ["confidence", "contentType"]
}
*/

Compliance Notes

Gemini-Specific Rules

  1. Description Requirements
#[derive(AsSchema)]
struct Invalid {
    #[schema(description = "")] // Error: Empty description
    field: String,
}

Limitations

  1. Recursive Structures
#[derive(AsSchema)]
struct Node {
    children: Vec<Node>, // Error: Stack overflow
}

Part of the Google AI Rust Toolkit

"Finally makes Gemini schema authoring feel like native Rust" - Library Author

Commit count: 0

cargo fmt