simplify_baml 0.2.0

Simplified BAML runtime for structured LLM outputs using native Rust types with macros
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
/// JSON parser with type coercion
///
/// This module implements lenient JSON parsing and type coercion.
/// It attempts to parse LLM outputs that may not be perfectly formatted JSON,
/// and coerces the values to match the expected BAML types.

use anyhow::{Context, Result};
use serde_json::Value as JsonValue;
use std::collections::HashMap;

use crate::ir::{BamlValue, FieldType, IR};

/// Normalize Unicode curly/smart quotes to ASCII quotes ONLY outside string literals
/// LLMs sometimes use curly quotes for JSON structure, which breaks parsing.
/// But curly quotes INSIDE strings should be preserved (escaped if needed).
fn normalize_quotes(text: &str) -> String {
    let mut result = String::with_capacity(text.len());
    let mut in_string = false;
    let mut escape_next = false;

    for c in text.chars() {
        if escape_next {
            result.push(c);
            escape_next = false;
            continue;
        }

        if c == '\\' && in_string {
            result.push(c);
            escape_next = true;
            continue;
        }

        if c == '"' {
            in_string = !in_string;
            result.push(c);
            continue;
        }

        if in_string {
            // Inside a string, escape curly quotes instead of converting them
            match c {
                '\u{201C}' | '\u{201D}' => result.push_str("\\\""),
                '\u{2018}' | '\u{2019}' => result.push('\''),
                '\n' => result.push_str("\\n"),
                '\r' => result.push_str("\\r"),
                '\t' => result.push_str("\\t"),
                _ => result.push(c),
            }
        } else {
            // Outside strings, convert curly quotes to ASCII for JSON structure
            match c {
                '\u{201C}' | '\u{201D}' => result.push('"'),
                '\u{2018}' | '\u{2019}' => result.push('\''),
                _ => result.push(c),
            }
        }
    }

    result
}

/// Find the bounds of a JSON object using string-aware brace matching
fn find_json_object_bounds(text: &str) -> Option<(usize, usize)> {
    find_json_bounds(text, '{', '}')
}

/// Find the bounds of a JSON array using string-aware bracket matching
fn find_json_array_bounds(text: &str) -> Option<(usize, usize)> {
    find_json_bounds(text, '[', ']')
}

/// Find matching JSON structure bounds, properly handling strings
/// Returns byte indices suitable for string slicing
fn find_json_bounds(text: &str, open_char: char, close_char: char) -> Option<(usize, usize)> {
    let mut start_pos = None;
    let mut depth = 0;
    let mut in_string = false;
    let mut escape_next = false;

    for (byte_idx, c) in text.char_indices() {
        if escape_next {
            escape_next = false;
            continue;
        }

        if c == '\\' && in_string {
            escape_next = true;
            continue;
        }

        if c == '"' {
            in_string = !in_string;
            continue;
        }

        if in_string {
            continue;
        }

        if c == open_char {
            if start_pos.is_none() {
                start_pos = Some(byte_idx);
            }
            depth += 1;
        } else if c == close_char {
            depth -= 1;
            if depth == 0 && start_pos.is_some() {
                // Return byte index of the closing char + its byte length
                return Some((start_pos.unwrap(), byte_idx + c.len_utf8() - 1));
            }
        }
    }

    None
}

pub struct Parser<'a> {
    ir: &'a IR,
}

impl<'a> Parser<'a> {
    pub fn new(ir: &'a IR) -> Self {
        Self { ir }
    }

    /// Parse and coerce a raw LLM response string to a BamlValue
    ///
    /// # Arguments
    /// * `raw_response` - The raw string from the LLM
    /// * `target_type` - The expected output type
    ///
    /// # Returns
    /// The parsed and coerced BamlValue
    pub fn parse(&self, raw_response: &str, target_type: &FieldType) -> Result<BamlValue> {
        // Step 0: Normalize curly/smart quotes to ASCII quotes
        // LLMs sometimes output Unicode quotes which break JSON parsing
        let normalized = normalize_quotes(raw_response);

        // Step 1: Extract JSON from the response (lenient extraction)
        let json_str = self.extract_json(&normalized)?;

        // Step 2: Parse JSON
        let json_value: JsonValue = serde_json::from_str(&json_str)
            .context("Failed to parse JSON from LLM response")?;

        // Step 3: Coerce to target type
        self.coerce(&json_value, target_type)
    }

    /// Extract JSON from a response that may contain markdown code blocks or extra text
    ///
    /// NOTE: We first check if the response is already valid JSON before attempting
    /// markdown extraction. This prevents issues where valid JSON containing embedded
    /// markdown code blocks (e.g., in a string field) would be incorrectly extracted.
    fn extract_json(&self, response: &str) -> Result<String> {
        let response = response.trim();

        // First, try to parse the response as-is to see if it's already valid JSON
        // This handles the common case where LLM returns raw JSON without markdown
        if serde_json::from_str::<serde_json::Value>(response).is_ok() {
            return Ok(response.to_string());
        }

        // Try to find JSON boundaries using string-aware matching
        // This handles cases where there's extra text before/after the JSON
        let array_bounds = find_json_array_bounds(response);
        let object_bounds = find_json_object_bounds(response);

        // Return whichever starts first (to handle arrays containing objects correctly)
        match (array_bounds, object_bounds) {
            (Some((arr_start, arr_end)), Some((obj_start, obj_end))) => {
                let extracted = if arr_start <= obj_start {
                    &response[arr_start..=arr_end]
                } else {
                    &response[obj_start..=obj_end]
                };
                // Verify extracted content is valid JSON before returning
                if serde_json::from_str::<serde_json::Value>(extracted).is_ok() {
                    return Ok(extracted.to_string());
                }
            }
            (Some((start, end)), None) => {
                let extracted = &response[start..=end];
                if serde_json::from_str::<serde_json::Value>(extracted).is_ok() {
                    return Ok(extracted.to_string());
                }
            }
            (None, Some((start, end))) => {
                let extracted = &response[start..=end];
                if serde_json::from_str::<serde_json::Value>(extracted).is_ok() {
                    return Ok(extracted.to_string());
                }
            }
            (None, None) => {}
        }

        // Only now try markdown extraction as a fallback
        // This handles responses wrapped in ```json code blocks
        let response_lower = response.to_lowercase();
        if let Some(start) = response_lower.find("```json") {
            let json_start = start + 7; // len("```json")
            // Find the closing ``` after the opening one
            if let Some(end_offset) = response[json_start..].find("```") {
                let json_end = json_start + end_offset;
                return Ok(response[json_start..json_end].trim().to_string());
            }
        }

        // Check for code block without language specifier
        if let Some(start) = response.find("```") {
            if let Some(end) = response[start + 3..].find("```") {
                let json_start = start + 3;
                let json_end = start + 3 + end;
                let content = response[json_start..json_end].trim();
                // Only use this if it looks like JSON
                if content.starts_with('{') || content.starts_with('[') {
                    return Ok(content.to_string());
                }
            }
        }

        // If nothing found, assume the whole response is JSON
        Ok(response.to_string())
    }

    /// Coerce a JSON value to match the target BAML type
    fn coerce(&self, value: &JsonValue, target_type: &FieldType) -> Result<BamlValue> {
        match target_type {
            FieldType::String => self.coerce_string(value),
            FieldType::Int => self.coerce_int(value),
            FieldType::Float => self.coerce_float(value),
            FieldType::Bool => self.coerce_bool(value),
            FieldType::Enum(enum_name) => self.coerce_enum(value, enum_name),
            FieldType::Class(class_name) => {
                // Try as class first, then fall back to enum
                // This handles the case where macros generate Class for all custom types
                if self.ir.find_class(class_name).is_some() {
                    self.coerce_class(value, class_name)
                } else if self.ir.find_enum(class_name).is_some() {
                    self.coerce_enum(value, class_name)
                } else {
                    anyhow::bail!("Type '{}' not found (neither class nor enum)", class_name)
                }
            }
            FieldType::List(inner) => self.coerce_list(value, inner),
            FieldType::Map(k, v) => self.coerce_map(value, k, v),
            FieldType::Union(types) => self.coerce_union(value, types),
            FieldType::TaggedEnum(name) => self.coerce_tagged_enum(value, name),
        }
    }

    fn coerce_string(&self, value: &JsonValue) -> Result<BamlValue> {
        match value {
            JsonValue::String(s) => Ok(BamlValue::String(s.clone())),
            JsonValue::Number(n) => Ok(BamlValue::String(n.to_string())),
            JsonValue::Bool(b) => Ok(BamlValue::String(b.to_string())),
            JsonValue::Null => Ok(BamlValue::String("".to_string())),
            JsonValue::Object(obj) => {
                // LLM may wrap the value in an object like { "value": "text" } or { "string": "text" }
                // Try common field names
                for field_name in ["value", "Value", "string", "String", "text", "Text", "result", "Result"] {
                    if let Some(inner) = obj.get(field_name) {
                        return self.coerce_string(inner);
                    }
                }
                // If object has only one field, use that
                if obj.len() == 1 {
                    if let Some((_, inner)) = obj.iter().next() {
                        return self.coerce_string(inner);
                    }
                }
                anyhow::bail!("Cannot coerce object to string: {:?}", value)
            }
            _ => anyhow::bail!("Cannot coerce {:?} to string", value),
        }
    }

    fn coerce_int(&self, value: &JsonValue) -> Result<BamlValue> {
        match value {
            JsonValue::Number(n) => {
                if let Some(i) = n.as_i64() {
                    Ok(BamlValue::Int(i))
                } else if let Some(f) = n.as_f64() {
                    if f.fract() != 0.0 {
                        anyhow::bail!("Cannot coerce non-integral float {} to int", f)
                    }
                    if f > i64::MAX as f64 || f < i64::MIN as f64 {
                        anyhow::bail!("Float {} overflows i64 range", f)
                    }
                    Ok(BamlValue::Int(f as i64))
                } else {
                    anyhow::bail!("Cannot coerce number to int")
                }
            }
            JsonValue::String(s) => {
                let i = s.parse::<i64>()
                    .context("Cannot parse string as int")?;
                Ok(BamlValue::Int(i))
            }
            JsonValue::Object(obj) => {
                // LLM may wrap the value in an object like { "value": 42 } or { "int": 42 }
                // Try common field names
                for field_name in ["value", "Value", "int", "Int", "number", "Number", "result", "Result"] {
                    if let Some(inner) = obj.get(field_name) {
                        return self.coerce_int(inner);
                    }
                }
                // If object has only one field, use that
                if obj.len() == 1 {
                    if let Some((_, inner)) = obj.iter().next() {
                        return self.coerce_int(inner);
                    }
                }
                anyhow::bail!("Cannot coerce object to int: {:?}", value)
            }
            _ => anyhow::bail!("Cannot coerce {:?} to int", value),
        }
    }

    fn coerce_float(&self, value: &JsonValue) -> Result<BamlValue> {
        match value {
            JsonValue::Number(n) => {
                if let Some(f) = n.as_f64() {
                    Ok(BamlValue::Float(f))
                } else {
                    anyhow::bail!("Cannot coerce number to float")
                }
            }
            JsonValue::String(s) => {
                let f = s.parse::<f64>()
                    .context("Cannot parse string as float")?;
                Ok(BamlValue::Float(f))
            }
            JsonValue::Object(obj) => {
                // LLM may wrap the value in an object like { "value": 3.14 } or { "float": 3.14 }
                // Try common field names
                for field_name in ["value", "Value", "float", "Float", "number", "Number", "result", "Result"] {
                    if let Some(inner) = obj.get(field_name) {
                        return self.coerce_float(inner);
                    }
                }
                // If object has only one field, use that
                if obj.len() == 1 {
                    if let Some((_, inner)) = obj.iter().next() {
                        return self.coerce_float(inner);
                    }
                }
                anyhow::bail!("Cannot coerce object to float: {:?}", value)
            }
            _ => anyhow::bail!("Cannot coerce {:?} to float", value),
        }
    }

    fn coerce_bool(&self, value: &JsonValue) -> Result<BamlValue> {
        match value {
            JsonValue::Bool(b) => Ok(BamlValue::Bool(*b)),
            JsonValue::String(s) => {
                let s_lower = s.to_lowercase();
                if s_lower == "true" || s_lower == "yes" || s_lower == "1" {
                    Ok(BamlValue::Bool(true))
                } else if s_lower == "false" || s_lower == "no" || s_lower == "0" {
                    Ok(BamlValue::Bool(false))
                } else {
                    anyhow::bail!("Cannot parse '{}' as bool", s)
                }
            }
            JsonValue::Number(n) => {
                if let Some(i) = n.as_i64() {
                    Ok(BamlValue::Bool(i != 0))
                } else {
                    anyhow::bail!("Cannot coerce number to bool")
                }
            }
            JsonValue::Object(obj) => {
                // LLM may wrap the value in an object like { "value": true } or { "bool": true }
                // Try common field names
                for field_name in ["value", "Value", "bool", "Bool", "result", "Result"] {
                    if let Some(inner) = obj.get(field_name) {
                        return self.coerce_bool(inner);
                    }
                }
                // If object has only one field, use that
                if obj.len() == 1 {
                    if let Some((_, inner)) = obj.iter().next() {
                        return self.coerce_bool(inner);
                    }
                }
                anyhow::bail!("Cannot coerce object to bool: {:?}", value)
            }
            _ => anyhow::bail!("Cannot coerce {:?} to bool", value),
        }
    }

    fn coerce_enum(&self, value: &JsonValue, enum_name: &str) -> Result<BamlValue> {
        let e = self.ir.find_enum(enum_name)
            .ok_or_else(|| anyhow::anyhow!("Enum '{}' not found", enum_name))?;

        let str_value = match value {
            JsonValue::String(s) => s.clone(),
            JsonValue::Number(n) => n.to_string(),
            JsonValue::Bool(b) => b.to_string(),
            JsonValue::Null => anyhow::bail!("Cannot coerce null to enum '{}'", enum_name),
            JsonValue::Array(_) => anyhow::bail!("Cannot coerce array to enum '{}': arrays are not valid enum values", enum_name),
            JsonValue::Object(_) => anyhow::bail!("Cannot coerce object to enum '{}': objects are not valid enum values", enum_name),
        };

        // Check if the value is a valid enum variant
        if e.values.contains(&str_value) {
            Ok(BamlValue::String(str_value))
        } else {
            // Try case-insensitive match
            let lower = str_value.to_lowercase();
            for variant in &e.values {
                if variant.to_lowercase() == lower {
                    return Ok(BamlValue::String(variant.clone()));
                }
            }
            anyhow::bail!("'{}' is not a valid variant of enum '{}'", str_value, enum_name)
        }
    }

    fn coerce_tagged_enum(&self, value: &JsonValue, enum_name: &str) -> Result<BamlValue> {
        let te = self.ir.find_tagged_enum(enum_name)
            .ok_or_else(|| anyhow::anyhow!("Tagged enum '{}' not found", enum_name))?;

        let obj = value.as_object()
            .ok_or_else(|| anyhow::anyhow!("Expected object for tagged enum '{}'", enum_name))?;

        // Get the tag value
        let tag_value = obj.get(&te.tag_field)
            .ok_or_else(|| anyhow::anyhow!("Missing tag field '{}' for tagged enum '{}'", te.tag_field, enum_name))?;

        let tag_str = tag_value.as_str()
            .ok_or_else(|| anyhow::anyhow!("Tag field '{}' must be a string", te.tag_field))?;

        // Find matching variant (case-insensitive)
        let variant = te.variants.iter()
            .find(|v| v.name.eq_ignore_ascii_case(tag_str))
            .ok_or_else(|| anyhow::anyhow!("'{}' is not a valid variant of tagged enum '{}'", tag_str, enum_name))?;

        // Build result map with the normalized tag and all variant fields
        let mut result = HashMap::new();
        result.insert(te.tag_field.clone(), BamlValue::String(variant.name.clone()));

        for field in &variant.fields {
            if let Some(field_value) = obj.get(&field.name) {
                if field_value.is_null() {
                    if !field.optional {
                        anyhow::bail!(
                            "Field '{}' in variant '{}' is required but got null",
                            field.name,
                            variant.name
                        );
                    }
                } else {
                    let coerced = self.coerce(field_value, &field.field_type)?;
                    result.insert(field.name.clone(), coerced);
                }
            } else if !field.optional {
                anyhow::bail!("Missing required field '{}' in variant '{}'", field.name, variant.name);
            }
        }

        Ok(BamlValue::Map(result))
    }

    fn coerce_class(&self, value: &JsonValue, class_name: &str) -> Result<BamlValue> {
        let class = self.ir.find_class(class_name)
            .ok_or_else(|| anyhow::anyhow!("Class '{}' not found", class_name))?;

        let obj = value.as_object()
            .ok_or_else(|| anyhow::anyhow!("Expected object for class '{}'", class_name))?;

        let mut result = HashMap::new();

        for field in &class.fields {
            if let Some(field_value) = obj.get(&field.name) {
                if field_value.is_null() {
                    if !field.optional {
                        anyhow::bail!(
                            "Field '{}' in class '{}' is required but got null",
                            field.name,
                            class_name
                        );
                    }
                    // Skip null values for optional fields (don't add to result)
                } else {
                    let coerced = self.coerce(field_value, &field.field_type)?;
                    result.insert(field.name.clone(), coerced);
                }
            } else if !field.optional {
                anyhow::bail!("Missing required field '{}' in class '{}'", field.name, class_name);
            }
        }

        Ok(BamlValue::Map(result))
    }

    fn coerce_list(&self, value: &JsonValue, inner_type: &FieldType) -> Result<BamlValue> {
        if let Some(arr) = value.as_array() {
            let coerced: Result<Vec<BamlValue>> = arr.iter()
                .map(|item| self.coerce(item, inner_type))
                .collect();
            Ok(BamlValue::List(coerced?))
        } else {
            // Scalar-to-list coercion: wrap non-array value in singleton list
            let coerced = self.coerce(value, inner_type)
                .context("Failed to coerce scalar to list element")?;
            Ok(BamlValue::List(vec![coerced]))
        }
    }

    fn coerce_map(&self, value: &JsonValue, key_type: &FieldType, value_type: &FieldType) -> Result<BamlValue> {
        let obj = value.as_object()
            .ok_or_else(|| anyhow::anyhow!("Expected object for map"))?;

        self.validate_map_key_type(key_type)?;

        let coerced: Result<HashMap<String, BamlValue>> = obj.iter()
            .map(|(k, v)| {
                self.coerce_map_key(k, key_type)?;
                self.coerce(v, value_type)
                    .map(|coerced_v| (k.clone(), coerced_v))
            })
            .collect();

        Ok(BamlValue::Map(coerced?))
    }

    fn validate_map_key_type(&self, key_type: &FieldType) -> Result<()> {
        match key_type {
            FieldType::String | FieldType::Int => Ok(()),
            _ => anyhow::bail!(
                "Unsupported map key type: {:?}. Only String and Int keys are supported.",
                key_type
            ),
        }
    }

    fn coerce_map_key(&self, key: &str, key_type: &FieldType) -> Result<()> {
        match key_type {
            FieldType::String => Ok(()),
            FieldType::Int => {
                key.parse::<i64>()
                    .map_err(|_| anyhow::anyhow!("Map key '{}' cannot be parsed as integer", key))?;
                Ok(())
            }
            _ => anyhow::bail!("Unsupported map key type: {:?}", key_type),
        }
    }

    fn coerce_union(&self, value: &JsonValue, types: &[FieldType]) -> Result<BamlValue> {
        // Try each type in order until one succeeds
        for t in types {
            if let Ok(coerced) = self.coerce(value, t) {
                return Ok(coerced);
            }
        }
        anyhow::bail!("Cannot coerce {:?} to any of the union types", value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::*;

    #[test]
    fn test_extract_json_from_markdown() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"
        Here's the result:
        ```json
        {"name": "John", "age": 30}
        ```
        "#;

        let json = parser.extract_json(response).unwrap();
        assert_eq!(json.trim(), r#"{"name": "John", "age": 30}"#);
    }

    #[test]
    fn test_extract_json_from_uppercase_markdown() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"
        Here's the result:
        ```JSON
        {"name": "John", "age": 30}
        ```
        "#;

        let json = parser.extract_json(response).unwrap();
        assert_eq!(json.trim(), r#"{"name": "John", "age": 30}"#);
    }

    #[test]
    fn test_extract_json_from_mixed_case_markdown() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"
        ```Json
        {"name": "Alice"}
        ```
        "#;

        let json = parser.extract_json(response).unwrap();
        assert_eq!(json.trim(), r#"{"name": "Alice"}"#);
    }

    #[test]
    fn test_coerce_int_from_string() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value = JsonValue::String("42".to_string());
        let result = parser.coerce_int(&value).unwrap();

        assert_eq!(result.as_int(), Some(42));
    }

    #[test]
    fn test_parse_class() {
        let mut ir = IR::new();
        ir.classes.push(Class {
            name: "Person".to_string(),
            description: None,
            fields: vec![
                Field {
                    name: "name".to_string(),
                    field_type: FieldType::String,
                    optional: false,
                description: None,
                },
                Field {
                    name: "age".to_string(),
                    field_type: FieldType::Int,
                    optional: false,
                description: None,
                },
            ],
        });

        let parser = Parser::new(&ir);
        let response = r#"{"name": "John", "age": 30}"#;

        let result = parser.parse(response, &FieldType::Class("Person".to_string())).unwrap();

        if let BamlValue::Map(map) = result {
            assert_eq!(map.get("name").and_then(|v| v.as_string()), Some("John"));
            assert_eq!(map.get("age").and_then(|v| v.as_int()), Some(30));
        } else {
            panic!("Expected Map");
        }
    }

    #[test]
    fn test_extract_json_with_braces_in_string() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"{"text": "use { for scope and } to close"}"#;
        let json = parser.extract_json(response).unwrap();
        assert_eq!(json, r#"{"text": "use { for scope and } to close"}"#);

        let parsed: JsonValue = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["text"], "use { for scope and } to close");
    }

    #[test]
    fn test_extract_json_with_brackets_in_string() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"{"code": "let arr = [1, 2, 3];"}"#;
        let json = parser.extract_json(response).unwrap();
        assert_eq!(json, r#"{"code": "let arr = [1, 2, 3];"}"#);

        let parsed: JsonValue = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["code"], "let arr = [1, 2, 3];");
    }

    #[test]
    fn test_extract_json_with_nested_braces_in_string() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"Here's the result: {"message": "JSON example: {\"key\": \"value\"}"}"#;
        let json = parser.extract_json(response).unwrap();
        
        let parsed: JsonValue = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["message"], r#"JSON example: {"key": "value"}"#);
    }

    #[test]
    fn test_extract_json_complex_string_content() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"Output: {"code": "fn main() { println!(\"hello\"); }", "lang": "rust"}"#;
        let json = parser.extract_json(response).unwrap();
        
        let parsed: JsonValue = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["code"], "fn main() { println!(\"hello\"); }");
        assert_eq!(parsed["lang"], "rust");
    }

    #[test]
    fn test_extract_json_array() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"[{"name": "Regina"}, {"name": "Guaguaxuan"}]"#;
        let json = parser.extract_json(response).unwrap();
        println!("Extracted JSON: {}", json);
        
        let parsed: JsonValue = serde_json::from_str(&json).unwrap();
        assert!(parsed.is_array());
        assert_eq!(parsed.as_array().unwrap().len(), 2);
    }

    #[test]
    fn test_coerce_int_from_integral_float() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value: JsonValue = serde_json::from_str("3.0").unwrap();
        let result = parser.coerce_int(&value).unwrap();
        assert_eq!(result.as_int(), Some(3));
    }

    #[test]
    fn test_coerce_int_rejects_non_integral_float() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value: JsonValue = serde_json::from_str("3.5").unwrap();
        let result = parser.coerce_int(&value);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("non-integral"));
    }

    #[test]
    fn test_coerce_int_rejects_overflow() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let large_float = (i64::MAX as f64) * 2.0;
        let value: JsonValue = serde_json::from_str(&format!("{:.0}", large_float)).unwrap();
        let result = parser.coerce_int(&value);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("overflow"));
    }

    #[test]
    fn test_coerce_enum_rejects_array() {
        let mut ir = IR::new();
        ir.enums.push(Enum {
            name: "Status".to_string(),
            values: vec!["Active".to_string(), "Inactive".to_string()],
            description: None,
        });

        let parser = Parser::new(&ir);
        let value: JsonValue = serde_json::from_str(r#"[1, 2]"#).unwrap();
        let result = parser.coerce_enum(&value, "Status");
        
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("array"), "Error should mention array: {}", err_msg);
    }

    #[test]
    fn test_coerce_enum_rejects_object() {
        let mut ir = IR::new();
        ir.enums.push(Enum {
            name: "Status".to_string(),
            values: vec!["Active".to_string(), "Inactive".to_string()],
            description: None,
        });

        let parser = Parser::new(&ir);
        let value: JsonValue = serde_json::from_str(r#"{"key": "value"}"#).unwrap();
        let result = parser.coerce_enum(&value, "Status");
        
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("object"), "Error should mention object: {}", err_msg);
    }

    #[test]
    fn test_coerce_enum_rejects_null() {
        let mut ir = IR::new();
        ir.enums.push(Enum {
            name: "Status".to_string(),
            values: vec!["Active".to_string(), "Inactive".to_string()],
            description: None,
        });

        let parser = Parser::new(&ir);
        let value = JsonValue::Null;
        let result = parser.coerce_enum(&value, "Status");
        
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("null"), "Error should mention null: {}", err_msg);
    }

    #[test]
    fn test_coerce_enum_accepts_valid_string() {
        let mut ir = IR::new();
        ir.enums.push(Enum {
            name: "Status".to_string(),
            values: vec!["Active".to_string(), "Inactive".to_string()],
            description: None,
        });

        let parser = Parser::new(&ir);
        let value = JsonValue::String("Active".to_string());
        let result = parser.coerce_enum(&value, "Status").unwrap();
        
        assert_eq!(result.as_string(), Some("Active"));
    }

    #[test]
    fn test_coerce_map_with_string_keys() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value: JsonValue = serde_json::from_str(r#"{"a": 1, "b": 2}"#).unwrap();
        let key_type = FieldType::String;
        let value_type = FieldType::Int;
        let result = parser.coerce_map(&value, &key_type, &value_type).unwrap();
        
        if let BamlValue::Map(map) = result {
            assert_eq!(map.len(), 2);
            assert_eq!(map.get("a").unwrap().as_int(), Some(1));
            assert_eq!(map.get("b").unwrap().as_int(), Some(2));
        } else {
            panic!("Expected Map");
        }
    }

    #[test]
    fn test_coerce_map_with_int_keys_valid() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value: JsonValue = serde_json::from_str(r#"{"1": "one", "2": "two"}"#).unwrap();
        let key_type = FieldType::Int;
        let value_type = FieldType::String;
        let result = parser.coerce_map(&value, &key_type, &value_type).unwrap();
        
        if let BamlValue::Map(map) = result {
            assert_eq!(map.len(), 2);
            assert_eq!(map.get("1").unwrap().as_string(), Some("one"));
            assert_eq!(map.get("2").unwrap().as_string(), Some("two"));
        } else {
            panic!("Expected Map");
        }
    }

    #[test]
    fn test_coerce_map_with_int_keys_invalid() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value: JsonValue = serde_json::from_str(r#"{"not_a_number": 1}"#).unwrap();
        let key_type = FieldType::Int;
        let value_type = FieldType::Int;
        let result = parser.coerce_map(&value, &key_type, &value_type);
        
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("not_a_number"), "Error should mention the invalid key: {}", err_msg);
        assert!(err_msg.contains("integer"), "Error should mention integer: {}", err_msg);
    }

    #[test]
    fn test_coerce_map_rejects_unsupported_key_type() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value: JsonValue = serde_json::from_str(r#"{"a": 1}"#).unwrap();
        let key_type = FieldType::Bool;
        let value_type = FieldType::Int;
        let result = parser.coerce_map(&value, &key_type, &value_type);
        
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("Unsupported map key type"), "Error should mention unsupported key type: {}", err_msg);
    }

    #[test]
    fn test_scalar_to_list_coercion_int() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value: JsonValue = serde_json::from_str("5").unwrap();
        let result = parser.coerce_list(&value, &FieldType::Int).unwrap();

        if let BamlValue::List(list) = result {
            assert_eq!(list.len(), 1);
            assert_eq!(list[0].as_int(), Some(5));
        } else {
            panic!("Expected List");
        }
    }

    #[test]
    fn test_scalar_to_list_coercion_string() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value = JsonValue::String("hello".to_string());
        let result = parser.coerce_list(&value, &FieldType::String).unwrap();

        if let BamlValue::List(list) = result {
            assert_eq!(list.len(), 1);
            assert_eq!(list[0].as_string(), Some("hello"));
        } else {
            panic!("Expected List");
        }
    }

    #[test]
    fn test_scalar_to_list_coercion_object_to_class_list() {
        let mut ir = IR::new();
        ir.classes.push(Class {
            name: "Item".to_string(),
            description: None,
            fields: vec![Field {
                name: "id".to_string(),
                field_type: FieldType::Int,
                optional: false,
                description: None,
            }],
        });

        let parser = Parser::new(&ir);
        let value: JsonValue = serde_json::from_str(r#"{"id": 42}"#).unwrap();
        let result = parser.coerce_list(&value, &FieldType::Class("Item".to_string())).unwrap();

        if let BamlValue::List(list) = result {
            assert_eq!(list.len(), 1);
            if let BamlValue::Map(map) = &list[0] {
                assert_eq!(map.get("id").unwrap().as_int(), Some(42));
            } else {
                panic!("Expected Map inside List");
            }
        } else {
            panic!("Expected List");
        }
    }

    #[test]
    fn test_scalar_to_list_coercion_fails_on_type_mismatch() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let value = JsonValue::String("not_a_number".to_string());
        let result = parser.coerce_list(&value, &FieldType::Int);

        assert!(result.is_err());
    }

    #[test]
    fn test_empty_object_for_class_with_required_fields() {
        let mut ir = IR::new();
        ir.classes.push(Class {
            name: "Person".to_string(),
            description: None,
            fields: vec![
                Field {
                    name: "name".to_string(),
                    field_type: FieldType::String,
                    optional: false,
                    description: None,
                },
            ],
        });

        let parser = Parser::new(&ir);
        let value: JsonValue = serde_json::from_str(r#"{}"#).unwrap();
        let result = parser.coerce(&value, &FieldType::Class("Person".to_string()));

        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("Missing required field"), "Error should mention missing field: {}", err_msg);
        assert!(err_msg.contains("name"), "Error should mention the field name: {}", err_msg);
    }

    #[test]
    fn test_empty_object_for_class_with_only_optional_fields() {
        let mut ir = IR::new();
        ir.classes.push(Class {
            name: "OptionalPerson".to_string(),
            description: None,
            fields: vec![
                Field {
                    name: "nickname".to_string(),
                    field_type: FieldType::String,
                    optional: true,
                    description: None,
                },
            ],
        });

        let parser = Parser::new(&ir);
        let value: JsonValue = serde_json::from_str(r#"{}"#).unwrap();
        let result = parser.coerce(&value, &FieldType::Class("OptionalPerson".to_string()));

        assert!(result.is_ok(), "Empty object should be valid when all fields are optional");
        if let BamlValue::Map(map) = result.unwrap() {
            assert!(map.is_empty(), "Map should be empty");
        } else {
            panic!("Expected Map");
        }
    }

    #[test]
    fn test_extract_json_with_extra_closing_braces() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"{"a": 1}}"#;
        let json = parser.extract_json(response).unwrap();
        assert_eq!(json, r#"{"a": 1}"#);

        let parsed: JsonValue = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["a"], 1);
    }

    #[test]
    fn test_extract_json_with_multiple_extra_closing_braces() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"{"nested": {"b": 2}}}}}"#;
        let json = parser.extract_json(response).unwrap();
        assert_eq!(json, r#"{"nested": {"b": 2}}"#);

        let parsed: JsonValue = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["nested"]["b"], 2);
    }

    #[test]
    fn test_extract_json_array_with_extra_closing_brackets() {
        let ir = IR::new();
        let parser = Parser::new(&ir);

        let response = r#"[1, 2, 3]]]"#;
        let json = parser.extract_json(response).unwrap();
        assert_eq!(json, r#"[1, 2, 3]"#);

        let parsed: JsonValue = serde_json::from_str(&json).unwrap();
        assert!(parsed.is_array());
        assert_eq!(parsed.as_array().unwrap().len(), 3);
    }

    #[test]
    fn test_curly_quotes_inside_string_are_escaped() {
        // Curly quotes inside strings should be escaped, not converted
        // Input: {"message": "blend of "cat" and "ethos""}
        let input = "{\"message\": \"blend of \u{201C}cat\u{201D} and \u{201C}ethos\u{201D}\"}";
        let normalized = normalize_quotes(input);
        // The curly quotes inside the string should become escaped quotes
        assert!(normalized.contains(r#"blend of \"cat\" and \"ethos\""#), "normalized was: {}", normalized);
        // Should be valid JSON
        let parsed: JsonValue = serde_json::from_str(&normalized).unwrap();
        assert!(parsed["message"].as_str().unwrap().contains("cat"));
    }

    #[test]
    fn test_curly_quotes_for_json_structure_are_converted() {
        // Curly quotes used for JSON structure should become ASCII quotes
        let input = "{ \u{201C}tool\u{201D}: \u{201C}Bash\u{201D} }";
        let normalized = normalize_quotes(input);
        assert_eq!(normalized, r#"{ "tool": "Bash" }"#);
    }

    #[test]
    fn test_literal_newlines_in_string_are_escaped() {
        let input = "{\"message\": \"Hello\nWorld\"}";
        let normalized = normalize_quotes(input);
        assert_eq!(normalized, r#"{"message": "Hello\nWorld"}"#);
        let parsed: JsonValue = serde_json::from_str(&normalized).unwrap();
        assert_eq!(parsed["message"], "Hello\nWorld");
    }

    #[test]
    fn test_final_response_exact_failure_case() {
        // This is the exact JSON that was failing in tao
        let input = r#"{
  "tool": "FinalResponse",
  "message": "Your current username is \"catethos\". It appears to be a playful blend of two words: \"cat\" and \"ethos.\" \"Cat\" evokes the image of a curious, independent feline, while \"ethos\" refers to the characteristic spirit, values, or beliefs of a community or individual. Put together, \"catethos\" suggests a personal philosophy that values curiosity, agility, and perhaps a touch of mischievous independence–much like a cat navigating the world on its own terms."
}"#;

        let ir = IR::new();
        let parser = Parser::new(&ir);

        // First check extract_json works
        let extracted = parser.extract_json(input);
        assert!(extracted.is_ok(), "extract_json failed: {:?}", extracted.err());

        // Check serde can parse it
        let json_str = extracted.unwrap();
        let parsed: Result<JsonValue, _> = serde_json::from_str(&json_str);
        assert!(parsed.is_ok(), "serde_json failed on: {}\nError: {:?}", json_str, parsed.err());

        let value = parsed.unwrap();
        assert_eq!(value["tool"], "FinalResponse");
        assert!(value["message"].as_str().unwrap().contains("catethos"));
    }

    #[test]
    fn test_json_with_embedded_markdown_code_blocks() {
        // This is the failure case: valid JSON with embedded markdown code blocks in a string field
        // The parser should NOT try to extract from these embedded code blocks
        let input = r#"{"tool": "FinalResponse", "message": "Here's a random rechart configuration in JSON format:\n\n```json\n{\n  \"data\": [\n    { \"name\": \"Jan\", \"sales\": 4000 }\n  ]\n}\n```\n\nThis represents a typical rechart configuration."}"#;

        let ir = IR::new();
        let parser = Parser::new(&ir);

        // extract_json should return the entire input since it's valid JSON
        let extracted = parser.extract_json(input);
        assert!(extracted.is_ok(), "extract_json failed: {:?}", extracted.err());

        let json_str = extracted.unwrap();
        let parsed: Result<JsonValue, _> = serde_json::from_str(&json_str);
        assert!(parsed.is_ok(), "serde_json failed: {:?}", parsed.err());

        let value = parsed.unwrap();
        assert_eq!(value["tool"], "FinalResponse");
        let msg = value["message"].as_str().unwrap();
        assert!(msg.contains("```json"), "message should contain the embedded code block");
        assert!(msg.contains("rechart configuration"), "message should contain the description");
    }

    #[test]
    fn test_markdown_wrapped_json_still_works() {
        // When the LLM actually wraps JSON in markdown, extraction should still work
        let input = "```json\n{\"tool\": \"FinalResponse\", \"message\": \"Hello\"}\n```";

        let ir = IR::new();
        let parser = Parser::new(&ir);

        let extracted = parser.extract_json(input);
        assert!(extracted.is_ok(), "extract_json failed: {:?}", extracted.err());

        let json_str = extracted.unwrap();
        let parsed: Result<JsonValue, _> = serde_json::from_str(&json_str);
        assert!(parsed.is_ok(), "serde_json failed: {:?}", parsed.err());

        let value = parsed.unwrap();
        assert_eq!(value["tool"], "FinalResponse");
        assert_eq!(value["message"], "Hello");
    }
}