Mental model
When you add#[BamlType] to a type used in a #[derive(Signature)] output field:
- The type is rendered into the schema/instructions sent to the model.
- The parser uses the same shape and attributes to parse the model response.
- Attributes like
alias,skip, anddefaultaffect both rendering and parse behavior.
When you need #[BamlType]
Built-in types work without extra annotations:
String,booli8,i16,i32,i64,f32,f64Option<T>,Vec<T>,HashMap<String, T>Box<T>,Arc<T>,Rc<T>
#[BamlType]:
- Your structs
- Your enums
- Nested custom types
What the model sees
Example:- Model sees
userNameandcreatedAt. - Model does not see
user_nameorcreated_at.
Attribute impact (visible behavior)
#[baml(alias = "...")]
- Prompt/schema uses
fullName. - Parser accepts model output keyed by
fullName.
#[baml(rename_all = "...")]
- Prompt/schema keys follow that naming convention.
camelCase, snake_case, PascalCase, kebab-case, SCREAMING_SNAKE_CASE.
#[baml(skip)]
internal_idis omitted from the model-facing schema.
- Field is filled via
Defaulton parse.
#[baml(default)]
- Field is rendered as optional in schema output.
- Missing
retriesparses asDefault::default().
#[baml(name = "...")]
- Typed prompt type labels use
UserProfile. - Hoisted schema rendering also uses
UserProfile.
- Default rendering may inline objects, so class headers can be less visible.
- If you want class headers to be explicit, render with class hoisting enabled.
#[baml(int_repr = "string")]
- Prompt/schema shows
large_idas string-like instead of int-like.
- Helps with integer widths that exceed JSON number precision.
#[baml(map_key_repr = "string" | "pairs")]
- Controls how non-string map keys are represented to the model.
Enums
Simple enums
- Prompt/schema presents the allowed literal values.
Data enums (tagged unions)
- Prompt/schema uses
typediscriminator and variant-specific fields.
Unsupported patterns (compile-time errors)
- Tuple structs:
struct Foo(A, B) - Unit structs:
struct Foo; - Tuple enum variants:
Enum::Var(A) serde_json::Value- Trait objects (
dyn Trait)
Contract tests for these docs
The user-visible claims on this page are locked by tests:- Prompt/render effects:
crates/dspy-rs/tests/test_bamltype_docs_contract.rs - Basic
#[BamlType]end-user contract:crates/dspy-rs/tests/test_bamltype_attr_contract.rs - Unsupported/compile-fail type shapes:
crates/bamltype/tests/ui.rs - Signature macro compile-fail coverage:
crates/dsrs-macros/tests/ui.rs
