dsir 0.1.1

DSPy-shaped library for programming language models in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::any::TypeId;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};

use bamltype::baml_types::BamlValue;
use bamltype::baml_types::TypeIR;
use bamltype::build_type_ir_from_shape;
use bamltype::facet::{Def, Field, Shape, Type, UserType};
use bamltype::internal_baml_jinja::types::OutputFormatContent;

use crate::{Constraint, ConstraintKind, ConstraintSpec, Signature};

/// Dotted path to a field within a signature, accounting for `#[flatten]` nesting.
///
/// A field `answer` at the top level has path `["answer"]`. A field `reasoning` inside
/// a flattened `WithReasoning` wrapper has path `["inner", "reasoning"]` (or however the
/// flatten tree is structured). Used by the adapter for path-aware parsing and by
/// [`SignatureSchema`] for field lookup.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FieldPath {
    parts: Vec<&'static str>,
}

impl FieldPath {
    pub fn new(parts: impl IntoIterator<Item = &'static str>) -> Self {
        Self {
            parts: parts.into_iter().collect(),
        }
    }

    pub fn push(&mut self, part: &'static str) {
        self.parts.push(part);
    }

    pub fn iter(&self) -> impl Iterator<Item = &'static str> + '_ {
        self.parts.iter().copied()
    }

    pub fn display(&self) -> String {
        self.parts.join(".")
    }

    pub fn is_empty(&self) -> bool {
        self.parts.is_empty()
    }
}

/// Static metadata for a single signature field, emitted by `#[derive(Signature)]`.
///
/// Carries the Rust field name, optional LM-facing alias, constraint specs, and
/// input render hints. Fed into [`SignatureSchema`] construction alongside Facet shape data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputRenderSpec {
    /// Default behavior: strings are raw, non-strings are rendered as JSON.
    Default,
    /// Explicit format hint (`#[format("json" | "yaml" | "toon")]`).
    Format(&'static str),
    /// Custom Jinja template (`#[render(jinja = "...")]`).
    Jinja(&'static str),
}

#[derive(Debug, Clone, Copy)]
pub struct FieldMetadataSpec {
    /// The Rust field name as written in the signature struct.
    pub rust_name: &'static str,
    /// Optional alias for the LM prompt (e.g. `#[rename = "query"]` on a `question` field).
    pub alias: Option<&'static str>,
    /// Constraint specs from `#[check(...)]` and `#[assert(...)]` attributes.
    pub constraints: &'static [ConstraintSpec],
    /// Input rendering policy for this field.
    pub input_render: InputRenderSpec,
}

/// Complete schema for a single field in a signature, combining Facet shape data with metadata.
///
/// Used by the adapter for prompt formatting and response parsing, and by the optimizer
/// for field inspection.
#[derive(Debug, Clone)]
pub struct FieldSchema {
    /// The field name shown to the LM (may differ from Rust name via aliasing).
    pub lm_name: &'static str,
    /// The dotted Rust path (e.g. `"inner.reasoning"` for flattened fields).
    pub rust_name: String,
    /// Documentation extracted from the field's doc comment.
    pub docs: String,
    /// Type representation used for edge validation and output format generation.
    pub type_ir: TypeIR,
    /// The Facet shape of this field's type.
    ///
    /// `None` for runtime [`crate::DynSignature`] fields (TypeIR is the source of truth).
    pub shape: Option<&'static Shape>,
    /// Path through the flatten tree to reach this field.
    pub path: FieldPath,
    /// Constraints declared on this field.
    pub constraints: &'static [ConstraintSpec],
    /// Input rendering policy.
    pub input_render: InputRenderSpec,
}

impl FieldSchema {
    pub fn path(&self) -> &FieldPath {
        &self.path
    }

    pub fn shape(&self) -> Option<&'static Shape> {
        self.shape
    }
}

/// Cached field-level schema for a [`Signature`], built from Facet shapes.
///
/// The shared backbone of the system. Every path that needs to know about a signature's
/// fields reads from here — the adapter formatting prompts, the graph validating edges,
/// optimizers inspecting structure. Built once per `Signature` type (keyed by `TypeId`),
/// leaked into `'static`, never mutated after init.
///
/// Contains the flattened list of input and output fields with their LM-facing names,
/// Rust paths (accounting for `#[flatten]`), type info, docs, and constraints. Derived
/// from Facet shape metadata at runtime, not from macro-emitted static arrays — Facet
/// is the single source of truth for type structure.
///
/// Access via [`SignatureSchema::of::<S>()`](SignatureSchema::of) or [`Signature::schema()`].
#[derive(Debug, Clone)]
pub struct SignatureSchema {
    instruction: &'static str,
    input_fields: Box<[FieldSchema]>,
    output_fields: Box<[FieldSchema]>,
    output_format: Arc<OutputFormatContent>,
}

impl SignatureSchema {
    /// Returns the cached schema for signature `S`, building it on first access.
    ///
    /// # Panics
    ///
    /// Panics if the schema can't be built (e.g. the input/output shapes aren't structs).
    pub fn of<S: Signature>() -> &'static Self {
        static CACHE: OnceLock<Mutex<HashMap<TypeId, &'static SignatureSchema>>> = OnceLock::new();

        let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
        {
            let guard = cache.lock().expect("schema cache lock poisoned");
            if let Some(schema) = guard.get(&TypeId::of::<S>()) {
                return schema;
            }
        }

        let built = Self::build::<S>().unwrap_or_else(|err| {
            panic!(
                "failed to build SignatureSchema for `{}`: {err}",
                std::any::type_name::<S>()
            )
        });
        let leaked = Box::leak(Box::new(built));

        let mut guard = cache.lock().expect("schema cache lock poisoned");
        guard.entry(TypeId::of::<S>()).or_insert(leaked)
    }

    fn build<S: Signature>() -> Result<Self, String> {
        let mut input_fields = collect_fields(
            "input",
            S::input_shape(),
            S::input_field_metadata(),
            S::instruction(),
        )?;
        let mut output_fields = collect_fields(
            "output",
            S::output_shape(),
            S::output_field_metadata(),
            S::instruction(),
        )?;

        ensure_unique_lm_names("input", &input_fields)?;
        ensure_unique_lm_names("output", &output_fields)?;

        // Keep declaration order deterministic.
        input_fields.shrink_to_fit();
        output_fields.shrink_to_fit();

        Ok(Self {
            instruction: S::instruction(),
            input_fields: input_fields.into_boxed_slice(),
            output_fields: output_fields.into_boxed_slice(),
            output_format: Arc::new(<S::Output as crate::BamlType>::baml_output_format().clone()),
        })
    }

    pub fn instruction(&self) -> &'static str {
        self.instruction
    }

    pub fn input_fields(&self) -> &[FieldSchema] {
        &self.input_fields
    }

    pub fn output_fields(&self) -> &[FieldSchema] {
        &self.output_fields
    }

    pub fn output_format(&self) -> &OutputFormatContent {
        &self.output_format
    }

    pub fn navigate_field<'a>(
        &self,
        path: &FieldPath,
        root: &'a BamlValue,
    ) -> Option<&'a BamlValue> {
        let mut current = root;
        for part in path.iter() {
            current = match current {
                BamlValue::Class(_, map) | BamlValue::Map(map) => map.get(part)?,
                _ => return None,
            };
        }
        Some(current)
    }

    pub fn field_by_rust<'a>(&'a self, rust_name: &str) -> Option<&'a FieldSchema> {
        self.input_fields()
            .iter()
            .chain(self.output_fields().iter())
            .find(|field| field.rust_name == rust_name)
    }

    pub fn input_field_by_rust<'a>(&'a self, rust_name: &str) -> Option<&'a FieldSchema> {
        self.input_fields()
            .iter()
            .find(|field| field.rust_name == rust_name)
    }

    pub fn output_field_by_rust<'a>(&'a self, rust_name: &str) -> Option<&'a FieldSchema> {
        self.output_fields()
            .iter()
            .find(|field| field.rust_name == rust_name)
    }

    pub fn with_fields(
        &self,
        input_fields: Vec<FieldSchema>,
        output_fields: Vec<FieldSchema>,
    ) -> Self {
        Self {
            instruction: self.instruction,
            input_fields: input_fields.into_boxed_slice(),
            output_fields: output_fields.into_boxed_slice(),
            output_format: Arc::clone(&self.output_format),
        }
    }

    /// Build a schema from runtime field definitions (dynamic signatures).
    pub fn from_dyn_parts(
        instruction: &'static str,
        input_fields: Vec<FieldSchema>,
        output_fields: Vec<FieldSchema>,
        output_format: OutputFormatContent,
    ) -> Result<Self, String> {
        ensure_unique_lm_names("input", &input_fields)?;
        ensure_unique_lm_names("output", &output_fields)?;
        Ok(Self {
            instruction,
            input_fields: input_fields.into_boxed_slice(),
            output_fields: output_fields.into_boxed_slice(),
            output_format: Arc::new(output_format),
        })
    }

    pub fn field_paths(&self) -> impl Iterator<Item = &FieldPath> {
        self.input_fields
            .iter()
            .chain(self.output_fields.iter())
            .map(|field| &field.path)
    }
}

fn collect_fields(
    side: &'static str,
    root_shape: &'static Shape,
    metadata: &'static [FieldMetadataSpec],
    instruction: &'static str,
) -> Result<Vec<FieldSchema>, String> {
    let struct_type = match &root_shape.ty {
        Type::User(UserType::Struct(struct_type)) => struct_type,
        _ => {
            return Err(format!(
                "{side} shape for instruction `{instruction}` must be a struct; got `{}`",
                root_shape.type_identifier
            ));
        }
    };

    let mut metadata_by_name: HashMap<&'static str, &'static FieldMetadataSpec> = HashMap::new();
    for item in metadata {
        metadata_by_name.insert(item.rust_name, item);
    }

    let mut fields = Vec::new();
    for field in struct_type.fields.iter() {
        if field.should_skip_deserializing() {
            continue;
        }
        let path = FieldPath::new([field.name]);
        let field_meta = metadata_by_name.get(field.name).copied();
        emit_field(field, path, field_meta, &metadata_by_name, &mut fields)?;
    }

    Ok(fields)
}

fn emit_field(
    field: &'static Field,
    path: FieldPath,
    inherited: Option<&FieldMetadataSpec>,
    metadata_by_name: &HashMap<&'static str, &'static FieldMetadataSpec>,
    out: &mut Vec<FieldSchema>,
) -> Result<(), String> {
    if field.should_skip_deserializing() {
        return Ok(());
    }

    if field.is_flattened() {
        let shape = flatten_target(field.shape());
        let struct_type = match &shape.ty {
            Type::User(UserType::Struct(struct_type)) => struct_type,
            _ => {
                return Err(format!(
                    "flattened field `{}` points to non-struct shape `{}`",
                    path.display(),
                    shape.type_identifier
                ));
            }
        };

        for nested in struct_type.fields.iter() {
            if nested.should_skip_deserializing() {
                continue;
            }
            let mut nested_path = path.clone();
            nested_path.push(nested.name);
            let nested_meta = metadata_by_name.get(nested.name).copied().or(inherited);
            emit_field(nested, nested_path, nested_meta, metadata_by_name, out)?;
        }

        return Ok(());
    }

    let mut type_ir = build_type_ir_from_shape(field.shape());
    let constraints = inherited.map(|meta| meta.constraints).unwrap_or(&[]);
    if !constraints.is_empty() {
        type_ir
            .meta_mut()
            .constraints
            .extend(constraints.iter().map(to_baml_constraint));
    }

    let docs = doc_lines(field.doc);
    let lm_name = inherited
        .and_then(|meta| meta.alias)
        .unwrap_or_else(|| field.effective_name());
    let input_render = inherited
        .map(|meta| meta.input_render)
        .unwrap_or(InputRenderSpec::Default);

    out.push(FieldSchema {
        lm_name,
        rust_name: path.display(),
        docs,
        type_ir,
        shape: Some(field.shape()),
        path,
        constraints,
        input_render,
    });

    Ok(())
}

fn flatten_target(mut shape: &'static Shape) -> &'static Shape {
    loop {
        match &shape.def {
            Def::Option(option_def) => shape = option_def.t,
            Def::Pointer(pointer_def) => {
                if let Some(inner) = pointer_def.pointee {
                    shape = inner;
                } else {
                    return shape;
                }
            }
            _ => return shape,
        }
    }
}

fn doc_lines(lines: &'static [&'static str]) -> String {
    lines
        .iter()
        .map(|line| line.trim())
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join("\n")
}

fn to_baml_constraint(constraint: &ConstraintSpec) -> Constraint {
    match constraint.kind {
        ConstraintKind::Check => Constraint::new_check(constraint.label, constraint.expression),
        ConstraintKind::Assert => Constraint::new_assert(constraint.label, constraint.expression),
    }
}

fn ensure_unique_lm_names(side: &'static str, fields: &[FieldSchema]) -> Result<(), String> {
    let mut by_alias: HashMap<&str, &FieldSchema> = HashMap::new();
    for field in fields {
        if let Some(previous) = by_alias.insert(field.lm_name, field) {
            return Err(format!(
                "{side} field alias collision for `{}` between `{}` and `{}`",
                field.lm_name,
                previous.path.display(),
                field.path.display()
            ));
        }
    }
    Ok(())
}