BoundaryML/baml · error · syn::Error

BamlSerde cannot be derived for structs, use…

Error message

BamlSerde cannot be derived for structs, use `#[serde(flatten)]` on dynamic instead.

What it means

Compile-time error from the `BamlSerde` derive macro: it does not support Rust structs. For struct-shaped data the library requires a `dynamic` field marked with `#[serde(flatten)]` so unstructured data is captured there. This is emitted in `derive_serde` when the input is `Data::Struct`.

Solutions

  1. Add a `dynamic` field annotated with `#[serde(flatten)]` to capture extra data
  2. Remove `BamlSerde` from the struct and use plain `serde` derives instead
  3. Convert the struct to an enum if it is meant to be a BAML union (with `union = true` container attr)

Example fix

// before
#[derive(BamlSerde)]
struct Foo { a: i32 }
// after
#[derive(BamlSerde)]
struct Foo { a: i32, #[serde(flatten)] dynamic: Dynamic }
// or use plain serde
#[derive(Serialize, Deserialize)]
struct Foo { a: i32 }
Defensive patterns

Strategy: type-guard

Validate before calling

// compile-time: only derive BamlSerde on enums marked as unions
// struct Foo { a: i32, #[serde(flatten)] dynamic: Dynamic }

Prevention

When it happens

Trigger: Applying `#[derive(BamlSerde)]` to a struct definition, e.g. `#[derive(BamlSerde)] struct Foo { ... }`.

Common situations: Developers assuming BamlSerde works like plain serde derive on structs; converting an existing serde struct to use BamlSerde without reading the union/dynamic requirements.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/0387e34039e004ce. Report an issue: GitHub.

Appendix: source

Thrown at languages/rust/baml-macros/src/baml_serde.rs:11

use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{Data, DataEnum, DeriveInput, Ident, Result};

use crate::shared::{ContainerAttrs, VariantAttrs};

pub(crate) fn derive_serde(input: &DeriveInput) -> Result<TokenStream> {
    let container_attrs = ContainerAttrs::from_attrs(&input.attrs)?;

    match &input.data {
        Data::Struct(..) => Err(syn::Error::new(
            Span::call_site(),
            "BamlSerde cannot be derived for structs, use `#[serde(flatten)]` on dynamic instead.",
        )),
        Data::Enum(data_enum) if container_attrs.union => {
            derive_serde_union(data_enum, &input.ident)
        }
        Data::Enum(..) => Err(syn::Error::new(
            Span::call_site(),
            "BamlSerde cannot be derived for enums, use `#[serde(untagged)]` on dynamic instead.",
        )),
        Data::Union(..) => Err(syn::Error::new(
            Span::call_site(),
            "Rust unions are not supported for this derive macro.",
        )),
    }
}

fn derive_serde_union(data: &DataEnum, ident: &Ident) -> Result<TokenStream> {

View on GitHub (pinned to bd85ce9dee)