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 descriptionignore_serde: Disable serde integrationrename_all: Naming convention (e.g., “camelCase”, “snake_case”)rename_all_with: Custom renaming functioncrate_path: Custom crate path specificationnullable: Mark entire structure as nullable
§Field/Variant Attributes
description: Field-specific documentationformat: Schema format specification (e.g., “date-time”, “email”)type: Specific schema typeas_schema: Custom schema generation functionas_schema_generic: Generic custom schema functionrequired: Force requirement statusmin/max_items: Array size constraintsnullable: Mark item as nullableskip: Exclude field from schema
§Important Notes
- Recursive Types: Not supported due to JSON Schema limitations
- Serde Integration: Use
AsSchemaWithSerdefor complex serde representations (e.g with Tuple structs) - Type-Format Compatibility: Mismatches like
r#type="String" format="float"throw compile errors rename_allandrename_all_withare mutually exclusive
Derive Macros§
- AsSchema
- Derive macro for AsSchema trait.
- AsSchema
With Serde - Hybrid derive macro combining custom schema generation with Serde deserialization