#codegen #schema #derive #interaction #api-bindings #google-ai

macro google-ai-schema-derive

Type-safe schema generation for Google AI API interactions

1 unstable release

new 0.1.0 May 7, 2025

#1822 in Parser implementations


Used in google-ai-rs

MIT license

49KB
1K SLoC

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

Dependencies

~215–650KB
~16K SLoC