Crate google_ai_schema_derive

Crate google_ai_schema_derive 

Source
Expand description

Schema derivation framework for Google AI APIs

Provides procedural macros for generating JSON schemas that comply with Google’s Generative AI API specifications. Enables type-safe API interactions through compile-time schema validation.

§Key Features

  • Schema-GSMA Compliance: Derive schemas matching Gemini API requirements
  • Serde Integration: Automatic alignment with deserialization
  • Validation Rules: Enforce Google-specific schema constraints at compile time
  • Attribute-based Customization: Fine-tune schema generation through extensive attributes
  • Type Safety: Compile-time validation of schema constraints

§Core Macros

  • #[derive(AsSchema)]: Main derivation macro for schema generation
  • #[derive(AsSchemaWithSerde)]: Enhanced version with deeper Serde integration

§Attribute Reference

§Container Attributes (struct/enum level)

  • description: Overall schema description
  • ignore_serde: Disable serde integration
  • rename_all: Naming convention (e.g., “camelCase”, “snake_case”)
  • rename_all_with: Custom renaming function
  • crate_path: Custom crate path specification
  • nullable: Mark entire structure as nullable

§Field/Variant Attributes

  • description: Field-specific documentation
  • format: Schema format specification (e.g., “date-time”, “email”)
  • type: Specific schema type
  • as_schema: Custom schema generation function
  • as_schema_generic: Generic custom schema function
  • required: Force requirement status
  • min/max_items: Array size constraints
  • nullable: Mark item as nullable
  • skip: Exclude field from schema

§Important Notes

  • Recursive Types: Not supported due to JSON Schema limitations
  • Serde Integration: Use AsSchemaWithSerde for complex serde representations (e.g with Tuple structs)
  • Type-Format Compatibility: Mismatches like r#type="String" format="float" throw compile errors
  • rename_all and rename_all_with are mutually exclusive

Derive Macros§

AsSchema
Derive macro for AsSchema trait.
AsSchemaWithSerde
Hybrid derive macro combining custom schema generation with Serde deserialization