> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kinetica.com/llms.txt
> Use this file to discover all available pages before exploring further.

# avro_schema

> avro_schema — Kinetica Rust API

[gpudb\_api](https://docs.kinetica.com/content/api/rust/index.md)
[Source](https://github.com/kineticadb/kinetica-api-rust/blob/master/crates/gpudb-api/src/avro_schema_builder.rs#L385-L414)&#x20;

<pre>`macro_rules! avro_schema {    (for builder:expr) => { ... };    ($builder:expr) => { ... };}`</pre>

Helper macro to define a static Avro schema using the builder pattern.This creates a `OnceLock` that initializes the schema exactly once and returns a `&'static str` reference.

## Limitation: one schema per macro call site, not per type

The plain form `avro_schema!(builder)` expands to a single `static` at the call site. Rust has no generic statics, so **inside a generic function or a generic `impl` every instantiation shares that one static**: the first instantiation to run builds the schema and every other type parameter gets the same string back, even when its builder expression would produce a different schema. Use the plain form only where the schema does not depend on a type parameter.Where it does, use the keyed form `avro_schema!(for T => builder)`, which caches one schema per `T` (keyed by [`TypeId`](https://doc.rust-lang.org/1.98.1/core/any/struct.TypeId.html), so `T: 'static`). Each distinct schema is built once and its string lives for the rest of the program, the same as with the plain form.

## Example

```rust theme={null}
use gpudb_api::{avro_schema, avro_schema_builder::{AvroSchemaBuilder, AvroType}};

fn my_schema() -> &'static str {
    avro_schema!(
        AvroSchemaBuilder::new("MyRecord")
            .int_field("id")
            .string_field("name")
            .bytes_field("payload")
    )
}
```

A schema that varies with a type parameter:

```rust theme={null}
use gpudb_api::{avro_schema, avro_schema_builder::AvroSchemaBuilder};

fn schema_for<T: 'static>() -> &'static str {
    avro_schema!(for T =>
        AvroSchemaBuilder::new(std::any::type_name::<T>().replace("::", "_"))
            .int_field("id")
    )
}

assert_ne!(schema_for::<u8>(), schema_for::<u16>());
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.