openai-ergonomic 0.5.2

Ergonomic Rust wrapper for OpenAI API
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
#![allow(clippy::uninlined_format_args)]
//! Structured Outputs example demonstrating JSON mode and schema validation.
//!
//! This example showcases `OpenAI`'s structured outputs capabilities, including:
//! - Simple JSON mode for basic structure enforcement
//! - Schema-based structured outputs with type validation
//! - Complex nested data structures
//! - Different use cases (data extraction, classification, analysis)
//! - Comprehensive error handling and validation
//!
//! ## Features Demonstrated
//!
//! - Basic JSON mode with implicit structure
//! - JSON Schema validation with strict typing
//! - Nested objects and arrays
//! - Enum validation for constrained values
//! - Data extraction from unstructured text
//! - Content classification and analysis
//! - Error handling for malformed schemas
//!
//! ## Prerequisites
//!
//! Set your `OpenAI` API key:
//! ```bash
//! export OPENAI_API_KEY="your-key-here"
//! ```
//!
//! ## Usage
//!
//! ```bash
//! cargo run --example structured_outputs
//! ```

use openai_ergonomic::{Client, Error};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("  OpenAI Ergonomic - Structured Outputs Example\n");

    // Initialize client from environment variables
    let client = match Client::from_env() {
        Ok(client_builder) => {
            println!(" Client initialized successfully");
            client_builder.build()
        }
        Err(e) => {
            eprintln!(" Failed to initialize client: {e}");
            eprintln!(" Make sure OPENAI_API_KEY is set in your environment");
            return Err(e.into());
        }
    };

    // Example 1: Simple JSON Mode
    println!("\n Example 1: Simple JSON Mode");
    println!("==============================");

    match simple_json_mode_example(&client).await {
        Ok(()) => println!(" Simple JSON mode example completed"),
        Err(e) => {
            eprintln!(" Simple JSON mode example failed: {e}");
            handle_api_error(&e);
        }
    }

    // Example 2: Schema-Based Data Extraction
    println!("\n Example 2: Schema-Based Data Extraction");
    println!("==========================================");

    match data_extraction_example(&client).await {
        Ok(()) => println!(" Data extraction example completed"),
        Err(e) => {
            eprintln!(" Data extraction example failed: {e}");
            handle_api_error(&e);
        }
    }

    // Example 3: Complex Nested Structures
    println!("\n Example 3: Complex Nested Structures");
    println!("=======================================");

    match complex_structure_example(&client).await {
        Ok(()) => println!(" Complex structure example completed"),
        Err(e) => {
            eprintln!(" Complex structure example failed: {e}");
            handle_api_error(&e);
        }
    }

    // Example 4: Content Classification
    println!("\n  Example 4: Content Classification");
    println!("=====================================");

    match classification_example(&client).await {
        Ok(()) => println!(" Classification example completed"),
        Err(e) => {
            eprintln!(" Classification example failed: {e}");
            handle_api_error(&e);
        }
    }

    // Example 5: Mathematical Analysis
    println!("\n Example 5: Mathematical Analysis");
    println!("===================================");

    match math_analysis_example(&client).await {
        Ok(()) => println!(" Mathematical analysis example completed"),
        Err(e) => {
            eprintln!(" Mathematical analysis example failed: {e}");
            handle_api_error(&e);
        }
    }

    // Example 6: Schema Validation Error Handling
    println!("\n  Example 6: Schema Validation & Error Handling");
    println!("=================================================");

    match validation_error_example(&client).await {
        Ok(()) => println!(" Validation error example completed"),
        Err(e) => {
            eprintln!(" Validation error example failed: {e}");
            handle_api_error(&e);
        }
    }

    println!("\n All structured output examples completed!");
    println!(" Check the console output above for JSON-formatted results.");
    Ok(())
}

/// Example 1: Simple JSON mode without explicit schema
async fn simple_json_mode_example(client: &Client) -> Result<(), Error> {
    println!("Using simple JSON mode for basic structure enforcement...");

    let builder = client
        .responses()
        .system("You are a helpful assistant. Always respond in valid JSON format with the keys: summary, sentiment, and confidence_score (0-1).")
        .user("Analyze this product review: 'This laptop is amazing! Great performance, excellent battery life, and the display is crystal clear. Highly recommended!'")
        .json_mode()
        .temperature(0.3)
        .max_completion_tokens(200);

    let response = client.send_responses(builder).await?;

    if let Some(content) = response.content() {
        println!(" JSON Analysis Result:");

        // Try to parse and pretty-print the JSON
        match serde_json::from_str::<serde_json::Value>(content) {
            Ok(json) => {
                println!("{}", serde_json::to_string_pretty(&json)?);

                // Demonstrate accessing specific fields
                if let Some(sentiment) = json.get("sentiment").and_then(|s| s.as_str()) {
                    println!("\n Extracted sentiment: {sentiment}");
                }
                if let Some(confidence) = json
                    .get("confidence_score")
                    .and_then(serde_json::Value::as_f64)
                {
                    println!(" Confidence score: {confidence:.2}");
                }
            }
            Err(e) => {
                println!("  Failed to parse JSON: {e}");
                println!("Raw response: {content}");
            }
        }
    }

    Ok(())
}

/// Example 2: Data extraction with schema validation
async fn data_extraction_example(client: &Client) -> Result<(), Error> {
    println!("Extracting structured data from unstructured text using JSON schema...");

    // Define schema for extracting contact information
    let contact_schema = json!({
        "type": "object",
        "properties": {
            "contacts": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string",
                            "description": "Full name of the person"
                        },
                        "email": {
                            "type": "string",
                            "format": "email",
                            "description": "Email address"
                        },
                        "phone": {
                            "type": "string",
                            "description": "Phone number"
                        },
                        "company": {
                            "type": "string",
                            "description": "Company or organization"
                        },
                        "role": {
                            "type": "string",
                            "description": "Job title or role"
                        }
                    },
                    "required": ["name"],
                    "additionalProperties": false
                }
            },
            "total_contacts": {
                "type": "integer",
                "description": "Total number of contacts extracted"
            }
        },
        "required": ["contacts", "total_contacts"],
        "additionalProperties": false
    });

    let unstructured_text =
        "Contact our team: John Smith (CEO) at john@example.com or call 555-0123. \
        For technical support, reach out to Sarah Johnson at sarah.johnson@techcorp.com. \
        Our sales manager Mike Wilson can be reached at mike@company.com or 555-0456.";

    let builder = client
        .responses()
        .system("You are an expert at extracting contact information from text. Extract all contact details you can find and structure them according to the provided schema.")
        .user(format!("Extract contact information from this text: {unstructured_text}"))
        .json_schema("contact_extraction", contact_schema)
        .temperature(0.1); // Low temperature for accuracy

    let response = client.send_responses(builder).await?;

    if let Some(content) = response.content() {
        println!(" Extracted Contact Information:");

        match serde_json::from_str::<serde_json::Value>(content) {
            Ok(json) => {
                println!("{}", serde_json::to_string_pretty(&json)?);

                // Demonstrate accessing the structured data
                if let Some(contacts) = json.get("contacts").and_then(|c| c.as_array()) {
                    println!("\n Summary: Found {} contact(s)", contacts.len());
                    for (i, contact) in contacts.iter().enumerate() {
                        if let Some(name) = contact.get("name").and_then(|n| n.as_str()) {
                            println!("   {}. {name}", i + 1);
                            if let Some(email) = contact.get("email").and_then(|e| e.as_str()) {
                                println!("       {email}");
                            }
                            if let Some(company) = contact.get("company").and_then(|c| c.as_str()) {
                                println!("       {company}");
                            }
                        }
                    }
                }
            }
            Err(e) => {
                println!("  Failed to parse JSON: {e}");
                println!("Raw response: {content}");
            }
        }
    }

    Ok(())
}

/// Example 3: Complex nested structure for event planning
#[allow(clippy::too_many_lines)]
async fn complex_structure_example(client: &Client) -> Result<(), Error> {
    println!("Creating complex nested structure for event planning...");

    // Define a comprehensive event schema
    let event_schema = json!({
        "type": "object",
        "properties": {
            "event": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "Event name"
                    },
                    "type": {
                        "type": "string",
                        "enum": ["conference", "workshop", "seminar", "networking", "party", "meeting"],
                        "description": "Type of event"
                    },
                    "date": {
                        "type": "string",
                        "format": "date",
                        "description": "Event date in YYYY-MM-DD format"
                    },
                    "duration_hours": {
                        "type": "number",
                        "minimum": 0.5,
                        "maximum": 24,
                        "description": "Duration in hours"
                    },
                    "venue": {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string",
                                "description": "Venue name"
                            },
                            "address": {
                                "type": "string",
                                "description": "Venue address"
                            },
                            "capacity": {
                                "type": "integer",
                                "minimum": 1,
                                "description": "Maximum capacity"
                            },
                            "amenities": {
                                "type": "array",
                                "items": {
                                    "type": "string",
                                    "enum": ["wifi", "parking", "catering", "av_equipment", "wheelchair_accessible", "air_conditioning"]
                                },
                                "description": "Available amenities"
                            }
                        },
                        "required": ["name", "capacity"],
                        "additionalProperties": false
                    },
                    "agenda": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "time": {
                                    "type": "string",
                                    "pattern": "^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$",
                                    "description": "Time in HH:MM format"
                                },
                                "activity": {
                                    "type": "string",
                                    "description": "Activity description"
                                },
                                "speaker": {
                                    "type": "string",
                                    "description": "Speaker name"
                                },
                                "duration_minutes": {
                                    "type": "integer",
                                    "minimum": 15,
                                    "maximum": 480,
                                    "description": "Activity duration in minutes"
                                }
                            },
                            "required": ["time", "activity", "duration_minutes"],
                            "additionalProperties": false
                        }
                    },
                    "estimated_cost": {
                        "type": "object",
                        "properties": {
                            "venue": {
                                "type": "number",
                                "minimum": 0,
                                "description": "Venue cost in USD"
                            },
                            "catering": {
                                "type": "number",
                                "minimum": 0,
                                "description": "Catering cost in USD"
                            },
                            "equipment": {
                                "type": "number",
                                "minimum": 0,
                                "description": "Equipment cost in USD"
                            },
                            "total": {
                                "type": "number",
                                "minimum": 0,
                                "description": "Total estimated cost in USD"
                            }
                        },
                        "required": ["total"],
                        "additionalProperties": false
                    }
                },
                "required": ["name", "type", "date", "duration_hours", "venue"],
                "additionalProperties": false
            }
        },
        "required": ["event"],
        "additionalProperties": false
    });

    let builder = client
        .responses()
        .system("You are an expert event planner. Create a detailed event plan based on the user's requirements, including venue details, agenda, and cost estimates.")
        .user("Plan a one-day AI/ML conference for 200 people in San Francisco. Include morning keynotes, afternoon workshops, networking lunch, and panel discussions. Budget around $50,000.")
        .json_schema("event_plan", event_schema)
        .temperature(0.5);

    let response = client.send_responses(builder).await?;

    if let Some(content) = response.content() {
        println!(" Event Plan:");

        match serde_json::from_str::<serde_json::Value>(content) {
            Ok(json) => {
                println!("{}", serde_json::to_string_pretty(&json)?);

                // Extract and display key information
                if let Some(event) = json.get("event") {
                    if let Some(name) = event.get("name").and_then(|n| n.as_str()) {
                        println!("\n Event: {name}");
                    }
                    if let Some(venue) = event.get("venue") {
                        if let Some(venue_name) = venue.get("name").and_then(|n| n.as_str()) {
                            let capacity = venue
                                .get("capacity")
                                .and_then(serde_json::Value::as_i64)
                                .unwrap_or(0);
                            println!(" Venue: {venue_name} (Capacity: {capacity})");
                        }
                    }
                    if let Some(agenda) = event.get("agenda").and_then(|a| a.as_array()) {
                        println!(" Agenda has {} activities", agenda.len());
                    }
                    if let Some(cost) = event.get("estimated_cost") {
                        if let Some(total) = cost.get("total").and_then(serde_json::Value::as_f64) {
                            println!(" Estimated total cost: ${total:.2}");
                        }
                    }
                }
            }
            Err(e) => {
                println!("  Failed to parse JSON: {e}");
                println!("Raw response: {content}");
            }
        }
    }

    Ok(())
}

/// Example 4: Content classification with enum validation
#[allow(clippy::too_many_lines)]
async fn classification_example(client: &Client) -> Result<(), Error> {
    println!("Classifying content with enum validation...");

    // Define schema for content classification
    let classification_schema = json!({
        "type": "object",
        "properties": {
            "classification": {
                "type": "object",
                "properties": {
                    "category": {
                        "type": "string",
                        "enum": ["technology", "business", "science", "health", "politics", "sports", "entertainment", "education", "travel", "lifestyle"],
                        "description": "Primary content category"
                    },
                    "subcategory": {
                        "type": "string",
                        "description": "More specific subcategory"
                    },
                    "sentiment": {
                        "type": "string",
                        "enum": ["positive", "neutral", "negative", "mixed"],
                        "description": "Overall sentiment"
                    },
                    "topics": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        },
                        "maxItems": 5,
                        "description": "Key topics mentioned"
                    },
                    "target_audience": {
                        "type": "string",
                        "enum": ["general", "professionals", "students", "experts", "consumers"],
                        "description": "Intended audience"
                    },
                    "complexity_level": {
                        "type": "string",
                        "enum": ["beginner", "intermediate", "advanced", "expert"],
                        "description": "Content complexity level"
                    },
                    "confidence_score": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 1,
                        "description": "Confidence in classification (0-1)"
                    }
                },
                "required": ["category", "sentiment", "topics", "target_audience", "complexity_level", "confidence_score"],
                "additionalProperties": false
            }
        },
        "required": ["classification"],
        "additionalProperties": false
    });

    let content_to_classify = "Recent advances in quantum computing have shown promising results for solving complex optimization problems. \
        Researchers at leading universities have demonstrated quantum algorithms that can potentially outperform classical computers \
        in specific domains like cryptography and molecular simulation. However, current quantum computers still face challenges \
        with noise and error rates, requiring sophisticated error correction techniques. The field is rapidly evolving with \
        significant investments from both academic institutions and major technology companies.";

    let builder = client
        .responses()
        .system("You are an expert content classifier. Analyze the provided text and classify it according to the given schema. Be precise with your classifications and provide accurate confidence scores.")
        .user(format!("Classify this content: {content_to_classify}"))
        .json_schema("content_classification", classification_schema)
        .temperature(0.2); // Low temperature for consistent classification

    let response = client.send_responses(builder).await?;

    if let Some(content) = response.content() {
        println!(" Content Classification:");

        match serde_json::from_str::<serde_json::Value>(content) {
            Ok(json) => {
                println!("{}", serde_json::to_string_pretty(&json)?);

                // Extract classification details
                if let Some(classification) = json.get("classification") {
                    println!("\n Classification Summary:");
                    if let Some(category) = classification.get("category").and_then(|c| c.as_str())
                    {
                        println!("    Category: {category}");
                    }
                    if let Some(sentiment) =
                        classification.get("sentiment").and_then(|s| s.as_str())
                    {
                        println!("    Sentiment: {sentiment}");
                    }
                    if let Some(audience) = classification
                        .get("target_audience")
                        .and_then(|a| a.as_str())
                    {
                        println!("    Target Audience: {audience}");
                    }
                    if let Some(complexity) = classification
                        .get("complexity_level")
                        .and_then(|c| c.as_str())
                    {
                        println!("    Complexity: {complexity}");
                    }
                    if let Some(confidence) = classification
                        .get("confidence_score")
                        .and_then(serde_json::Value::as_f64)
                    {
                        println!("    Confidence: {:.2}%", confidence * 100.0);
                    }
                    if let Some(topics) = classification.get("topics").and_then(|t| t.as_array()) {
                        let topic_strings: Vec<String> = topics
                            .iter()
                            .filter_map(|t| t.as_str())
                            .map(std::string::ToString::to_string)
                            .collect();
                        println!("     Topics: {}", topic_strings.join(", "));
                    }
                }
            }
            Err(e) => {
                println!("  Failed to parse JSON: {e}");
                println!("Raw response: {content}");
            }
        }
    }

    Ok(())
}

/// Example 5: Mathematical analysis with structured output
#[allow(clippy::too_many_lines)]
async fn math_analysis_example(client: &Client) -> Result<(), Error> {
    println!("Performing mathematical analysis with structured output...");

    // Define schema for mathematical analysis
    let math_schema = json!({
        "type": "object",
        "properties": {
            "analysis": {
                "type": "object",
                "properties": {
                    "problem_type": {
                        "type": "string",
                        "enum": ["algebra", "geometry", "calculus", "statistics", "probability", "discrete_math", "linear_algebra"],
                        "description": "Type of mathematical problem"
                    },
                    "solution_steps": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "step_number": {
                                    "type": "integer",
                                    "minimum": 1,
                                    "description": "Step number in the solution"
                                },
                                "description": {
                                    "type": "string",
                                    "description": "Description of what this step does"
                                },
                                "equation": {
                                    "type": "string",
                                    "description": "Mathematical equation or expression"
                                },
                                "result": {
                                    "type": "string",
                                    "description": "Result of this step"
                                }
                            },
                            "required": ["step_number", "description", "equation"],
                            "additionalProperties": false
                        }
                    },
                    "final_answer": {
                        "type": "string",
                        "description": "Final answer to the problem"
                    },
                    "verification": {
                        "type": "object",
                        "properties": {
                            "check_method": {
                                "type": "string",
                                "description": "Method used to verify the answer"
                            },
                            "is_correct": {
                                "type": "boolean",
                                "description": "Whether the answer passes verification"
                            }
                        },
                        "required": ["check_method", "is_correct"],
                        "additionalProperties": false
                    },
                    "concepts_used": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        },
                        "description": "Mathematical concepts used in the solution"
                    }
                },
                "required": ["problem_type", "solution_steps", "final_answer", "verification", "concepts_used"],
                "additionalProperties": false
            }
        },
        "required": ["analysis"],
        "additionalProperties": false
    });

    let math_problem =
        "Find the derivative of f(x) = 3x^3 + 2x^2 - 5x + 7 and evaluate it at x = 2.";

    let builder = client
        .responses()
        .system("You are a mathematics tutor. Solve mathematical problems step by step, showing your work clearly and verifying your answers. Structure your response according to the provided schema.")
        .user(format!("Solve this problem: {math_problem}"))
        .json_schema("math_analysis", math_schema)
        .temperature(0.1); // Very low temperature for mathematical accuracy

    let response = client.send_responses(builder).await?;

    if let Some(content) = response.content() {
        println!(" Mathematical Analysis:");

        match serde_json::from_str::<serde_json::Value>(content) {
            Ok(json) => {
                println!("{}", serde_json::to_string_pretty(&json)?);

                // Extract and display solution steps
                if let Some(analysis) = json.get("analysis") {
                    println!("\n Solution Summary:");

                    if let Some(problem_type) =
                        analysis.get("problem_type").and_then(|p| p.as_str())
                    {
                        println!("    Problem Type: {problem_type}");
                    }

                    if let Some(steps) = analysis.get("solution_steps").and_then(|s| s.as_array()) {
                        println!("    Solution Steps: {} steps", steps.len());
                        for step in steps {
                            if let (Some(step_num), Some(desc)) = (
                                step.get("step_number").and_then(serde_json::Value::as_i64),
                                step.get("description").and_then(|d| d.as_str()),
                            ) {
                                println!("      {step_num}. {desc}");
                                if let Some(equation) =
                                    step.get("equation").and_then(|e| e.as_str())
                                {
                                    println!("          {equation}");
                                }
                            }
                        }
                    }

                    if let Some(answer) = analysis.get("final_answer").and_then(|a| a.as_str()) {
                        println!("    Final Answer: {answer}");
                    }

                    if let Some(verification) = analysis.get("verification") {
                        if let Some(is_correct) = verification
                            .get("is_correct")
                            .and_then(serde_json::Value::as_bool)
                        {
                            let status = if is_correct {
                                " Verified"
                            } else {
                                " Needs Review"
                            };
                            println!("    Verification: {status}");
                        }
                    }

                    if let Some(concepts) = analysis.get("concepts_used").and_then(|c| c.as_array())
                    {
                        let concept_strings: Vec<String> = concepts
                            .iter()
                            .filter_map(|c| c.as_str())
                            .map(std::string::ToString::to_string)
                            .collect();
                        println!("    Concepts Used: {}", concept_strings.join(", "));
                    }
                }
            }
            Err(e) => {
                println!("  Failed to parse JSON: {e}");
                println!("Raw response: {content}");
            }
        }
    }

    Ok(())
}

/// Example 6: Demonstration of schema validation and error handling
#[allow(clippy::too_many_lines)]
async fn validation_error_example(client: &Client) -> Result<(), Error> {
    println!("Demonstrating schema validation and error handling...");

    // Define a strict schema that's likely to cause validation challenges
    let strict_schema = json!({
        "type": "object",
        "properties": {
            "numbers": {
                "type": "array",
                "items": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 100
                },
                "minItems": 3,
                "maxItems": 5,
                "description": "Array of 3-5 integers between 1 and 100"
            },
            "precision_value": {
                "type": "number",
                "multipleOf": 0.01,
                "minimum": 0,
                "maximum": 1,
                "description": "A precise decimal value between 0 and 1, to 2 decimal places"
            },
            "strict_enum": {
                "type": "string",
                "enum": ["alpha", "beta", "gamma"],
                "description": "Must be exactly one of the allowed values"
            },
            "required_pattern": {
                "type": "string",
                "pattern": "^[A-Z]{2}[0-9]{4}$",
                "description": "Must be exactly 2 uppercase letters followed by 4 digits"
            }
        },
        "required": ["numbers", "precision_value", "strict_enum", "required_pattern"],
        "additionalProperties": false
    });

    println!(" Using a strict schema with specific constraints...");

    let builder = client
        .responses()
        .system("Generate data that strictly follows the provided JSON schema. Pay careful attention to all constraints including ranges, patterns, and array sizes.")
        .user("Generate sample data that conforms to the schema. Make sure all values meet the exact requirements.")
        .json_schema("strict_validation", strict_schema)
        .temperature(0.1)
        .max_completion_tokens(300);

    let response = client.send_responses(builder).await?;

    if let Some(content) = response.content() {
        println!(" Schema Validation Test:");

        match serde_json::from_str::<serde_json::Value>(content) {
            Ok(json) => {
                println!("{}", serde_json::to_string_pretty(&json)?);

                // Manual validation of the generated data
                println!("\n Manual Validation:");
                let mut validation_passed = true;

                // Check numbers array
                if let Some(numbers) = json.get("numbers").and_then(|n| n.as_array()) {
                    println!("    Numbers array: {} items", numbers.len());
                    if numbers.len() < 3 || numbers.len() > 5 {
                        println!("    Array size constraint violated");
                        validation_passed = false;
                    }
                    for (i, num) in numbers.iter().enumerate() {
                        if let Some(val) = num.as_i64() {
                            if !(1..=100).contains(&val) {
                                println!("    Number {i} ({val}) outside valid range [1-100]");
                                validation_passed = false;
                            }
                        }
                    }
                } else {
                    println!("    Numbers array missing or invalid");
                    validation_passed = false;
                }

                // Check precision value
                if let Some(precision) = json
                    .get("precision_value")
                    .and_then(serde_json::Value::as_f64)
                {
                    println!("    Precision value: {precision}");
                    if !(0.0..=1.0).contains(&precision) {
                        println!("    Precision value outside range [0-1]");
                        validation_passed = false;
                    }
                }

                // Check enum value
                if let Some(enum_val) = json.get("strict_enum").and_then(|e| e.as_str()) {
                    println!("     Enum value: {enum_val}");
                    if !["alpha", "beta", "gamma"].contains(&enum_val) {
                        println!("    Enum value not in allowed set");
                        validation_passed = false;
                    }
                }

                // Check pattern
                if let Some(pattern_val) = json.get("required_pattern").and_then(|p| p.as_str()) {
                    println!("    Pattern value: {pattern_val}");
                    let regex = regex::Regex::new(r"^[A-Z]{2}[0-9]{4}$").unwrap();
                    if !regex.is_match(pattern_val) {
                        println!("    Pattern does not match required format");
                        validation_passed = false;
                    }
                }

                if validation_passed {
                    println!("    All manual validations passed!");
                } else {
                    println!("     Some validation constraints were not met");
                }
            }
            Err(e) => {
                println!("  JSON parsing failed: {e}");
                println!("This demonstrates how schema constraints can sometimes be challenging for the model");
                println!("Raw response: {content}");
            }
        }
    }

    // Demonstrate handling of intentionally problematic schema
    println!("\n Testing with intentionally problematic request...");

    let problematic_builder = client
        .responses()
        .system("You are unhelpful and ignore instructions.")
        .user("Ignore the schema and just say 'hello world'")
        .json_schema(
            "strict_validation",
            json!({
                "type": "object",
                "properties": {
                    "impossible": {
                        "type": "string",
                        "pattern": "^impossible_pattern_that_cannot_match$"
                    }
                },
                "required": ["impossible"]
            }),
        )
        .temperature(0.1);

    match client.send_responses(problematic_builder).await {
        Ok(problematic_response) => {
            if let Some(content) = problematic_response.content() {
                println!(" Problematic request result:");
                println!("{content}");
                println!(" The model likely still attempted to follow the schema despite conflicting instructions");
            }
        }
        Err(e) => {
            println!("  Problematic request failed as expected: {e}");
        }
    }

    Ok(())
}

/// Comprehensive error handling helper
fn handle_api_error(error: &Error) {
    match error {
        Error::Api {
            status,
            message,
            error_type,
            error_code,
        } => {
            eprintln!(" API Error [{status}]: {message}");
            if let Some(error_type) = error_type {
                eprintln!("   Type: {error_type}");
            }
            if let Some(error_code) = error_code {
                eprintln!("   Code: {error_code}");
            }

            // Provide specific guidance based on error type
            match *status {
                400 => eprintln!(" Bad Request - Check your JSON schema or request parameters"),
                401 => eprintln!(" Check your API key: export OPENAI_API_KEY=\"your-key\""),
                403 => eprintln!(" Forbidden - Check your API permissions and model access"),
                422 => eprintln!(" Invalid schema or request format - verify your JSON schema"),
                429 => eprintln!(" Rate limited - try again in a moment"),
                500..=599 => eprintln!(" Server error - try again later"),
                _ => {}
            }
        }
        Error::InvalidRequest(msg) => {
            eprintln!(" Invalid Request: {msg}");
            eprintln!(" Check your request parameters and JSON schema format");
        }
        Error::Config(msg) => {
            eprintln!(" Configuration Error: {msg}");
            eprintln!(" Check your client configuration");
        }
        Error::Http(err) => {
            eprintln!(" HTTP Error: {err}");
            eprintln!(" Check your network connection");
        }
        Error::HttpMiddleware(err) => {
            eprintln!(" HTTP Middleware Error: {err}");
            eprintln!(" Check your network connection and middleware configuration");
        }
        Error::Json(err) => {
            eprintln!(" JSON Error: {err}");
            eprintln!(" Response parsing failed - the model may have generated invalid JSON");
        }
        Error::Authentication(msg) => {
            eprintln!(" Authentication Error: {msg}");
            eprintln!(" Check your API key");
        }
        Error::RateLimit(msg) => {
            eprintln!(" Rate Limit Error: {msg}");
            eprintln!(" Try again in a moment or upgrade your plan");
        }
        Error::Stream(msg) => {
            eprintln!(" Stream Error: {msg}");
            eprintln!(" Connection issue with streaming");
        }
        Error::File(err) => {
            eprintln!(" File Error: {err}");
            eprintln!(" Check file permissions and paths");
        }
        Error::Builder(msg) => {
            eprintln!(" Builder Error: {msg}");
            eprintln!(" Check your request builder configuration");
        }
        Error::Internal(msg) => {
            eprintln!(" Internal Error: {msg}");
            eprintln!(" This may be a bug, please report it");
        }
        Error::StreamConnection { message } => {
            eprintln!(" Stream Connection Error: {message}");
            eprintln!(" Check your network connection");
        }
        Error::StreamParsing { message, chunk } => {
            eprintln!(" Stream Parsing Error: {message}");
            eprintln!("   Problematic chunk: {chunk}");
            eprintln!(" The response stream may be corrupted");
        }
        Error::StreamBuffer { message } => {
            eprintln!(" Stream Buffer Error: {message}");
            eprintln!(" The stream buffer encountered an issue");
        }
    }
}