#4969·baml

[feature] Rust generator: derive Serialize/Deserialize (or an equivalent) on generated client types

Author: BenSpexCreated Sep 22, 2026Updated Sep 22, 2026

Use case

We generate a Rust client from baml_src/ (output_type = "rust", working since #4370 closed) and call it from a small Rust host (ops/flow-rust-host/) that must persist and replay the generated request/response/class types: writing a completed call's typed result to disk, reloading it later for a byte-identical replay test against the Node host, and (eventually) storing it in the same kind of durable run log the JS side already keeps in SQLite. All of that needs the generated types to round-trip through a standard Rust serialization format (JSON via serde_json, matching what the Node/Python hosts already produce and consume).

What we checked

  • The generated Rust client's classes/enums do not derive serde::Serialize/Deserialize (or any other serialization trait) today. Persisting a generated type currently requires hand- written mapping code that re-derives the same shape the generator already knows, purely so it can be handed to serde_json::to_string/from_str.
  • No existing open issue on BoundaryML/baml asks for this (searched serde, rust generator serde, Serialize).
  • Related-but-different: #4815 is about BAML's own baml.json.to_string/from_string failing to round-trip a bigint field inside BAML itself — not about the generated Rust client's types being usable with the host language's own serialization ecosystem, which is this ask.

Desired API sketch

Have the Rust generator add #[derive(serde::Serialize, serde::Deserialize)] (behind a Cargo feature flag if serde should stay optional for consumers who don't need it) to every generated class and enum, the same way most hand-written or prost/protoc-generated Rust API clients do by default or via an opt-in feature:

toml
# Cargo.toml of a project depending on the generated crate
[dependencies]
baml_generated = { path = "...", features = ["serde"] }
rust
// Then, in the consuming host:
let json = serde_json::to_string(&generated_result)?;
let reloaded: GeneratedResultType = serde_json::from_str(&json)?;

Why this matters

Without it, every Rust host built on a generated BAML client either re-implements the same struct shape a second time by hand (duplicating a source of truth the generator already owns, and risking drift whenever baml_src/ changes a field) or writes ad hoc field-by-field serialization code. A derive (even opt-in via a feature flag) removes that duplication for any Rust consumer that wants ordinary JSON persistence of a generated type, which is the common case for any host that logs, caches, or replays BAML call results.