Skip to content

feat: Input field serialization format control via BAML infrastructure #45

Closed

Description

@darinkishore

Problem

Currently, DSRs serializes input field values using a simple approach:

  • Strings → raw string
  • Everything else → serde_json::to_string()

This differs from DSPy's ChatAdapter which has special formatting for certain types (e.g., «blob» markers for string lists). More importantly, we already have BAML's serialization infrastructure (internal_baml_jinja with format filters for yaml/json/toon) but we're not using it for input formatting.

What We Want

Goal API

#[derive(Signature, Clone)]
struct RAG {
    #[input]
    question: String,

    #[input]
    #[format("yaml")]  // or "json", "toon", etc.
    context: Vec<Document>,

    #[output]
    answer: String,
}

When context is serialized for the LLM prompt, it should use BAML's format infrastructure—the same one used for output schema rendering—to serialize the value.

Expected Behavior

Format Serialization
"json" Compact JSON (current default)
"yaml" Human-readable YAML
"toon" Token-efficient TOON format for uniform arrays

Why BAML Infrastructure?

  1. Consistency - Same serializers used for output schemas
  2. Alias preservation - BAML's serializers respect @alias on enums/classes
  3. TOON support - Token-efficient format for tabular data (significant savings)
  4. Already available - internal_baml_jinja is already a dependency

Current State

  • crates/dspy-rs/src/adapter/chat.rs:504-510 - format_baml_value_for_prompt() does basic JSON
  • baml_bridge::internal_baml_jinja - Has format filters but not wired up for input serialization
  • Output schemas already use OutputFormatContent::render() with RenderOptions

Investigation Needed

  1. How to invoke BAML's format filter programmatically (not via template string)
  2. Where the filter is defined in internal_baml_jinja
  3. Whether we can call it directly on BamlValue

Acceptance Criteria

  • #[format("yaml")] attribute works on input fields
  • #[format("json")] attribute works (explicit, same as default)
  • #[format("toon")] attribute works for uniform arrays
  • Uses BAML's existing infrastructure, not a separate serde_yaml dependency
  • Aliases are preserved in serialized output

References

Activity

  1. added
    enhancement
    on Jan 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions