ai-sdk-core 0.3.0

High-level APIs for AI SDK - text generation, embeddings, and tool execution
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
//! Output strategy pattern for different object generation modes.
//!
//! This module defines the `OutputStrategy` trait and provides implementations for:
//! - Object output (single structured object)
//! - Array output (array of elements with validation)
//! - Enum output (single enum value)
//! - No-schema output (unvalidated JSON)

use async_trait::async_trait;
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::marker::PhantomData;
use thiserror::Error;

use ai_sdk_provider::language_model::{ResponseMetadata, Usage};

/// The type of output being generated.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputType {
    /// Single structured object
    Object,
    /// Array of elements
    Array,
    /// Single enum value
    Enum,
    /// Unvalidated JSON
    NoSchema,
}

/// Result of partial validation during streaming.
#[derive(Debug, Clone)]
pub struct PartialValidation<P> {
    /// The partial value that was validated
    pub partial: P,
    /// Text delta that was processed
    pub text_delta: String,
}

/// Context for final validation.
#[derive(Debug, Clone)]
pub struct ValidationContext {
    /// Complete generated text
    pub text: String,
    /// Response metadata from the model
    pub response: Option<ResponseMetadata>,
    /// Token usage statistics
    pub usage: Usage,
}

/// Result of validation (either partial or final).
#[derive(Debug)]
pub enum ValidationResult<T> {
    /// Validation succeeded
    Success {
        /// The validated value
        value: T,
        /// Raw JSON value before validation
        raw_value: Value,
    },
    /// Validation failed
    Failure {
        /// The validation error
        error: ValidationError,
        /// Raw JSON value that failed validation
        raw_value: Value,
    },
}

/// Errors that can occur during validation.
#[derive(Debug, Error)]
pub enum ValidationError {
    /// JSON parsing error
    #[error("JSON parsing error: {0}")]
    JsonError(#[from] serde_json::Error),

    /// Schema validation error
    #[error("Schema validation error: {0}")]
    SchemaError(String),

    /// Type mismatch error
    #[error("Type mismatch: expected {expected}, got {got}")]
    TypeMismatch {
        /// Expected type
        expected: String,
        /// Actual type
        got: String,
    },

    /// Custom validation error
    #[error("Validation error: {0}")]
    Custom(String),
}

/// Strategy trait for handling different output types.
///
/// This trait defines how to:
/// - Generate JSON schema for the output
/// - Validate partial results during streaming
/// - Validate final results
/// - Transform streaming output
#[async_trait]
pub trait OutputStrategy: Send + Sync {
    /// The type of partial values during streaming
    type Partial: Clone + Send;

    /// The type of final results
    type Result: Send;

    /// Returns the output type for this strategy
    fn output_type(&self) -> OutputType;

    /// Returns the JSON schema for this output type, if any
    async fn json_schema(&self) -> Option<Value>;

    /// Validates a partial result during streaming
    async fn validate_partial_result(
        &self,
        value: Value,
        text_delta: String,
        is_first_delta: bool,
        is_final_delta: bool,
        latest_object: Option<&Self::Partial>,
    ) -> ValidationResult<PartialValidation<Self::Partial>>;

    /// Validates the final result after generation completes
    async fn validate_final_result(
        &self,
        value: Option<Value>,
        context: ValidationContext,
    ) -> ValidationResult<Self::Result>;
}

/// Strategy for generating single structured objects.
///
/// This strategy validates objects against a JSON schema and returns
/// the complete object as the final result.
pub struct ObjectOutputStrategy<T> {
    schema: Value,
    _phantom: PhantomData<T>,
}

impl<T> ObjectOutputStrategy<T> {
    /// Creates a new object output strategy with the given JSON schema.
    pub fn new(schema: Value) -> Self {
        Self {
            schema,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<T> OutputStrategy for ObjectOutputStrategy<T>
where
    T: DeserializeOwned + Clone + Send + Sync + 'static,
{
    type Partial = T;
    type Result = T;

    fn output_type(&self) -> OutputType {
        OutputType::Object
    }

    async fn json_schema(&self) -> Option<Value> {
        Some(self.schema.clone())
    }

    async fn validate_partial_result(
        &self,
        value: Value,
        text_delta: String,
        _is_first_delta: bool,
        _is_final_delta: bool,
        _latest_object: Option<&Self::Partial>,
    ) -> ValidationResult<PartialValidation<Self::Partial>> {
        // For partial results, we don't validate against schema
        // Just try to deserialize as T (allowing partial data)
        match serde_json::from_value::<T>(value.clone()) {
            Ok(partial) => ValidationResult::Success {
                value: PartialValidation {
                    partial,
                    text_delta,
                },
                raw_value: value,
            },
            Err(e) => ValidationResult::Failure {
                error: ValidationError::JsonError(e),
                raw_value: value,
            },
        }
    }

    async fn validate_final_result(
        &self,
        value: Option<Value>,
        _context: ValidationContext,
    ) -> ValidationResult<Self::Result> {
        let Some(value) = value else {
            return ValidationResult::Failure {
                error: ValidationError::Custom("No value provided".to_string()),
                raw_value: Value::Null,
            };
        };

        // Validate against schema (for now, just try to deserialize)
        // TODO: Implement full JSON schema validation
        match serde_json::from_value::<T>(value.clone()) {
            Ok(result) => ValidationResult::Success {
                value: result,
                raw_value: value,
            },
            Err(e) => ValidationResult::Failure {
                error: ValidationError::JsonError(e),
                raw_value: value,
            },
        }
    }
}

/// Strategy for generating arrays of elements.
///
/// This strategy wraps the array in `{"elements": [...]}` for better LLM reliability
/// and validates each element (except the last incomplete one during streaming).
pub struct ArrayOutputStrategy<T> {
    element_schema: Value,
    _phantom: PhantomData<T>,
}

impl<T> ArrayOutputStrategy<T> {
    /// Creates a new array output strategy with the given element schema.
    pub fn new(element_schema: Value) -> Self {
        Self {
            element_schema,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<T> OutputStrategy for ArrayOutputStrategy<T>
where
    T: DeserializeOwned + Clone + Send + Sync + 'static,
{
    type Partial = Vec<T>;
    type Result = Vec<T>;

    fn output_type(&self) -> OutputType {
        OutputType::Array
    }

    async fn json_schema(&self) -> Option<Value> {
        // Wrap element schema in array wrapper
        Some(serde_json::json!({
            "type": "object",
            "properties": {
                "elements": {
                    "type": "array",
                    "items": self.element_schema
                }
            },
            "required": ["elements"],
            "additionalProperties": false
        }))
    }

    async fn validate_partial_result(
        &self,
        value: Value,
        text_delta: String,
        _is_first_delta: bool,
        is_final_delta: bool,
        _latest_object: Option<&Self::Partial>,
    ) -> ValidationResult<PartialValidation<Self::Partial>> {
        // Extract elements array from wrapper
        let elements = match value.get("elements") {
            Some(Value::Array(arr)) => arr,
            _ => {
                return ValidationResult::Failure {
                    error: ValidationError::TypeMismatch {
                        expected: "object with elements array".to_string(),
                        got: format!("{:?}", value),
                    },
                    raw_value: value,
                };
            }
        };

        // For streaming, validate all elements except possibly the last
        let validate_count = if is_final_delta {
            elements.len()
        } else {
            elements.len().saturating_sub(1)
        };

        let mut validated_elements = Vec::new();
        for elem_value in elements.iter().take(validate_count) {
            match serde_json::from_value::<T>(elem_value.clone()) {
                Ok(elem) => validated_elements.push(elem),
                Err(e) => {
                    return ValidationResult::Failure {
                        error: ValidationError::JsonError(e),
                        raw_value: value,
                    };
                }
            }
        }

        // Add last element without validation if not final
        if !is_final_delta && elements.len() > validate_count {
            if let Ok(elem) = serde_json::from_value::<T>(elements[validate_count].clone()) {
                validated_elements.push(elem);
            }
        }

        ValidationResult::Success {
            value: PartialValidation {
                partial: validated_elements,
                text_delta,
            },
            raw_value: value,
        }
    }

    async fn validate_final_result(
        &self,
        value: Option<Value>,
        _context: ValidationContext,
    ) -> ValidationResult<Self::Result> {
        let Some(value) = value else {
            return ValidationResult::Failure {
                error: ValidationError::Custom("No value provided".to_string()),
                raw_value: Value::Null,
            };
        };

        // Extract and validate all elements
        let elements = match value.get("elements") {
            Some(Value::Array(arr)) => arr,
            _ => {
                return ValidationResult::Failure {
                    error: ValidationError::TypeMismatch {
                        expected: "object with elements array".to_string(),
                        got: format!("{:?}", value),
                    },
                    raw_value: value,
                };
            }
        };

        let mut validated_elements = Vec::new();
        for elem_value in elements {
            match serde_json::from_value::<T>(elem_value.clone()) {
                Ok(elem) => validated_elements.push(elem),
                Err(e) => {
                    return ValidationResult::Failure {
                        error: ValidationError::JsonError(e),
                        raw_value: value,
                    };
                }
            }
        }

        ValidationResult::Success {
            value: validated_elements,
            raw_value: value,
        }
    }
}

/// Strategy for generating enum values.
///
/// This strategy wraps the enum value in `{"result": "value"}` and validates
/// against a list of allowed values.
pub struct EnumOutputStrategy {
    enum_values: Vec<String>,
}

impl EnumOutputStrategy {
    /// Creates a new enum output strategy with the given allowed values.
    pub fn new(enum_values: Vec<String>) -> Self {
        Self { enum_values }
    }
}

#[async_trait]
impl OutputStrategy for EnumOutputStrategy {
    type Partial = String;
    type Result = String;

    fn output_type(&self) -> OutputType {
        OutputType::Enum
    }

    async fn json_schema(&self) -> Option<Value> {
        Some(serde_json::json!({
            "type": "object",
            "properties": {
                "result": {
                    "type": "string",
                    "enum": self.enum_values
                }
            },
            "required": ["result"],
            "additionalProperties": false
        }))
    }

    async fn validate_partial_result(
        &self,
        value: Value,
        text_delta: String,
        _is_first_delta: bool,
        is_final_delta: bool,
        _latest_object: Option<&Self::Partial>,
    ) -> ValidationResult<PartialValidation<Self::Partial>> {
        let result_value = match value.get("result") {
            Some(Value::String(s)) => s.clone(),
            _ => {
                return ValidationResult::Failure {
                    error: ValidationError::TypeMismatch {
                        expected: "object with result string".to_string(),
                        got: format!("{:?}", value),
                    },
                    raw_value: value,
                };
            }
        };

        // For partial results, check if it's a prefix of any enum value
        if is_final_delta {
            // Final: must match exactly
            if !self.enum_values.contains(&result_value) {
                return ValidationResult::Failure {
                    error: ValidationError::SchemaError(format!(
                        "Value '{}' is not one of: {:?}",
                        result_value, self.enum_values
                    )),
                    raw_value: value,
                };
            }
        } else {
            // Partial: check if it's a prefix
            if !self
                .enum_values
                .iter()
                .any(|ev| ev.starts_with(&result_value))
            {
                return ValidationResult::Failure {
                    error: ValidationError::SchemaError(format!(
                        "Value '{}' is not a prefix of any enum value: {:?}",
                        result_value, self.enum_values
                    )),
                    raw_value: value,
                };
            }
        }

        ValidationResult::Success {
            value: PartialValidation {
                partial: result_value,
                text_delta,
            },
            raw_value: value,
        }
    }

    async fn validate_final_result(
        &self,
        value: Option<Value>,
        _context: ValidationContext,
    ) -> ValidationResult<Self::Result> {
        let Some(value) = value else {
            return ValidationResult::Failure {
                error: ValidationError::Custom("No value provided".to_string()),
                raw_value: Value::Null,
            };
        };

        let result_value = match value.get("result") {
            Some(Value::String(s)) => s.clone(),
            _ => {
                return ValidationResult::Failure {
                    error: ValidationError::TypeMismatch {
                        expected: "object with result string".to_string(),
                        got: format!("{:?}", value),
                    },
                    raw_value: value,
                };
            }
        };

        if !self.enum_values.contains(&result_value) {
            return ValidationResult::Failure {
                error: ValidationError::SchemaError(format!(
                    "Value '{}' is not one of: {:?}",
                    result_value, self.enum_values
                )),
                raw_value: value,
            };
        }

        ValidationResult::Success {
            value: result_value,
            raw_value: value,
        }
    }
}

/// Strategy for generating unvalidated JSON.
///
/// This strategy performs no validation and returns raw JSON values.
pub struct NoSchemaOutputStrategy;

#[async_trait]
impl OutputStrategy for NoSchemaOutputStrategy {
    type Partial = Value;
    type Result = Value;

    fn output_type(&self) -> OutputType {
        OutputType::NoSchema
    }

    async fn json_schema(&self) -> Option<Value> {
        None
    }

    async fn validate_partial_result(
        &self,
        value: Value,
        text_delta: String,
        _is_first_delta: bool,
        _is_final_delta: bool,
        _latest_object: Option<&Self::Partial>,
    ) -> ValidationResult<PartialValidation<Self::Partial>> {
        ValidationResult::Success {
            value: PartialValidation {
                partial: value.clone(),
                text_delta,
            },
            raw_value: value,
        }
    }

    async fn validate_final_result(
        &self,
        value: Option<Value>,
        _context: ValidationContext,
    ) -> ValidationResult<Self::Result> {
        let Some(value) = value else {
            return ValidationResult::Failure {
                error: ValidationError::Custom("No value provided".to_string()),
                raw_value: Value::Null,
            };
        };

        ValidationResult::Success {
            value: value.clone(),
            raw_value: value,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct TestObject {
        name: String,
        age: u32,
    }

    #[tokio::test]
    async fn test_object_strategy() {
        let schema = serde_json::json!({
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "age": {"type": "number"}
            }
        });

        let strategy = ObjectOutputStrategy::<TestObject>::new(schema);
        assert_eq!(strategy.output_type(), OutputType::Object);

        let value = serde_json::json!({"name": "Alice", "age": 30});
        let result = strategy
            .validate_final_result(
                Some(value),
                ValidationContext {
                    text: String::new(),
                    response: None,
                    usage: Usage::default(),
                },
            )
            .await;

        match result {
            ValidationResult::Success { value, .. } => {
                assert_eq!(value.name, "Alice");
                assert_eq!(value.age, 30);
            }
            ValidationResult::Failure { error, .. } => {
                panic!("Validation failed: {:?}", error);
            }
        }
    }

    #[tokio::test]
    async fn test_array_strategy() {
        let element_schema = serde_json::json!({
            "type": "object",
            "properties": {
                "name": {"type": "string"}
            }
        });

        let strategy = ArrayOutputStrategy::<TestObject>::new(element_schema);
        assert_eq!(strategy.output_type(), OutputType::Array);

        let value = serde_json::json!({
            "elements": [
                {"name": "Alice", "age": 30},
                {"name": "Bob", "age": 25}
            ]
        });

        let result = strategy
            .validate_final_result(
                Some(value),
                ValidationContext {
                    text: String::new(),
                    response: None,
                    usage: Usage::default(),
                },
            )
            .await;

        match result {
            ValidationResult::Success { value, .. } => {
                assert_eq!(value.len(), 2);
                assert_eq!(value[0].name, "Alice");
                assert_eq!(value[1].name, "Bob");
            }
            ValidationResult::Failure { error, .. } => {
                panic!("Validation failed: {:?}", error);
            }
        }
    }

    #[tokio::test]
    async fn test_enum_strategy() {
        let strategy = EnumOutputStrategy::new(vec![
            "option1".to_string(),
            "option2".to_string(),
            "option3".to_string(),
        ]);

        assert_eq!(strategy.output_type(), OutputType::Enum);

        let value = serde_json::json!({"result": "option2"});
        let result = strategy
            .validate_final_result(
                Some(value),
                ValidationContext {
                    text: String::new(),
                    response: None,
                    usage: Usage::default(),
                },
            )
            .await;

        match result {
            ValidationResult::Success { value, .. } => {
                assert_eq!(value, "option2");
            }
            ValidationResult::Failure { error, .. } => {
                panic!("Validation failed: {:?}", error);
            }
        }
    }

    #[tokio::test]
    async fn test_no_schema_strategy() {
        let strategy = NoSchemaOutputStrategy;
        assert_eq!(strategy.output_type(), OutputType::NoSchema);

        let value = serde_json::json!({"any": "value", "works": true});
        let result = strategy
            .validate_final_result(
                Some(value.clone()),
                ValidationContext {
                    text: String::new(),
                    response: None,
                    usage: Usage::default(),
                },
            )
            .await;

        match result {
            ValidationResult::Success {
                value: result_value,
                ..
            } => {
                assert_eq!(result_value, value);
            }
            ValidationResult::Failure { error, .. } => {
                panic!("Validation failed: {:?}", error);
            }
        }
    }
}