agentsdk 0.5.2

An open-source Rust library for building AI-powered applications, inspired by the Vercel AI SDK. It provides a robust, type-safe, and easy-to-use interface for interacting with various Large Language Models (LLMs).
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
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
/// Main macro to generate all language model provider integration tests.
///
/// This macro generates a comprehensive suite of tests for a language model provider,
/// including basic functionality, streaming, tools, structured output, error handling, and embeddings.
///
/// # Parameters
///
/// * `provider: $provider_type:ident` - The type of the provider (e.g., `OpenAI`)
/// * `api_key_var: $env_key:expr` - Environment variable name for the API key (e.g., `"OPENAI_API_KEY"`)
/// * `model_struct: $model_struct:ident` - The model struct type (e.g., `Gpt5`)
/// * `default_model: $default_model:expr` - Expression for the default model instance
/// * `tool_model: $tool_model:expr` - Expression for the model instance used in tool tests
/// * `structured_output_model: $structured_output_model:expr` - Expression for the model instance used in structured output tests
/// * `reasoning_model: $reasoning_model:expr` - Expression for the model instance used in reasoning tests
/// * `embedding_model: $embedding_model:expr` - Expression for the model instance used in embedding tests
/// * `skip_reasoning: $skip_reasoning:tt` - Bool literal to skip reasoning tests at compile time
/// * `skip_tool: $skip_tool:tt` - Bool literal to skip tool tests at compile time
/// * `skip_structured_output: $skip_structured_output:tt` - Bool literal to skip structured output tests at compile time
/// * `skip_streaming: $skip_streaming:tt` - Bool literal to skip streaming tests at compile time
/// * `skip_embedding: $skip_embedding:tt` - Bool literal to skip embedding tests at compile time
///
/// # Example
///
/// ```rust
/// generate_language_model_tests!(
///     provider: OpenAI,
///     api_key_var: "OPENAI_API_KEY",
///     model_struct: Gpt5,
///     default_model: OpenAI::gpt_5_nano(),
///     tool_model: OpenAI::gpt_5_nano(),
///     structured_output_model: OpenAI::gpt_5_nano(),
///     reasoning_model: OpenAI::gpt_5_nano(),
///     embedding_model: OpenAI::text_embedding_3_small(),
///     skip_reasoning: true,
///     skip_tool: false,
///     skip_structured_output: false,
///     skip_streaming: false,
///     skip_embedding: false
/// );
///
macro_rules! generate_language_model_tests {
    (
        provider: $provider_type:ident,
        api_key_var: $env_key:expr,
        model_struct: $model_struct:ident,
        default_model: $default_model:expr,
        tool_model: $tool_model:expr,
        structured_output_model: $structured_output_model:expr,
        reasoning_model: $reasoning_model:expr,
        embedding_model: $embedding_model:expr,
        skip_reasoning: $skip_reasoning:tt,
        skip_tool: $skip_tool:tt,
        skip_structured_output: $skip_structured_output:tt,
        skip_streaming: $skip_streaming:tt,
        skip_embedding: $skip_embedding:tt
    ) => {
        use agentsdk::core::tools::ToolExecute;
        use agentsdk::core::{
            DynamicModel, LanguageModelRequest, LanguageModelStreamChunkType, Message,
            language_model::{LanguageModel, LanguageModelResponseContentType, StopReason},
            tools::Tool,
        };
        use agentsdk::tool;
        use dotenv::dotenv;
        use std::sync::{Arc, Mutex};

        #[allow(unused_imports)]
        use {futures::StreamExt, schemars::JsonSchema, serde::Deserialize, serde_json::Value};

        // Helper macro for API key checking
        macro_rules! skip_if_no_api_key {
            () => {
                dotenv().ok();
                if std::env::var($env_key).is_err() {
                    println!("Skipping test: {} not set", $env_key);
                    return;
                }
            };
        }

        // Generate all standard test categories
        generate_provider_has_default_interface!($provider_type, $model_struct);
        generate_basic_tests!($default_model);
        generate_language_model_stop_reason_tests!($default_model);
        generate_language_model_hook_tests!($tool_model);
        generate_language_model_step_id_tests!($default_model);
        generate_language_model_streaming_tests!($reasoning_model, $skip_streaming);
        generate_language_model_tool_tests!($tool_model, $skip_tool);
        generate_language_model_schema_tests!($structured_output_model, $skip_structured_output);
        generate_language_model_reasoning_tests!($reasoning_model, $skip_reasoning);
        generate_embedding_tests!($embedding_model, $skip_embedding);
    };
}

// Test to ensure all providers have the default provider settings builder interface
macro_rules! generate_provider_has_default_interface {
    ($provider_type:ident, $model_struct:ident) => {
        #[tokio::test]
        async fn test_provider_has_default_interface() {
            let provider = $provider_type::<$model_struct>::builder()
                .provider_name("test-provider".to_string())
                .api_key("test-api-key")
                .base_url("http://localhost:8080".to_string())
                .path("/custom/path")
                .build();

            // check if provider didn't throw an error
            assert!(provider.is_ok());

            // check provider settings
            let provider = provider.unwrap();
            assert_eq!(provider.settings.provider_name, "test-provider");
            assert_eq!(provider.settings.api_key, "test-api-key");
            assert_eq!(provider.settings.base_url, "http://localhost:8080/");
            assert_eq!(provider.settings.path, Some("/custom/path".to_string()));

            let provider_with_body = $provider_type::<$model_struct>::builder()
                .provider_name("test-provider-body".to_string())
                .api_key("test-api-key")
                .base_url("http://localhost:8080")
                .body(serde_json::json!({
                    "store": false
                }))
                .build()
                .unwrap();

            assert_eq!(
                provider_with_body.settings.body,
                Some(
                    serde_json::json!({
                        "store": false
                    })
                    .as_object()
                    .expect("body should be an object")
                    .clone()
                )
            );

            let provider_with_headers = $provider_type::<$model_struct>::builder()
                .provider_name("test-provider-headers".to_string())
                .api_key("test-api-key")
                .base_url("http://localhost:8080")
                .headers(std::collections::HashMap::from([(
                    "x-trace-id".to_string(),
                    "trace-123".to_string(),
                )]))
                .build()
                .unwrap();

            assert_eq!(
                provider_with_headers.settings.headers,
                Some(std::collections::HashMap::from([(
                    "x-trace-id".to_string(),
                    "trace-123".to_string(),
                )]))
            );

            // should fail on invalid base url
            let provider2 = $provider_type::<$model_struct>::builder()
                .provider_name("test-provider2".to_string())
                .api_key("test-api-key2")
                .base_url("ocalhost:80802".to_string())
                .build();

            assert!(provider2.is_err());
            assert_eq!(
                provider2.unwrap_err().to_string(),
                "Invalid input: Base URL must start with http:// or https://"
            );

            // should fail on empty api key
            let provider3 = $provider_type::<$model_struct>::builder()
                .provider_name("test-provider3".to_string())
                .api_key("")
                .base_url("http://localhost:8080/".to_string())
                .build();

            assert!(provider3.is_err());
            assert_eq!(
                provider3.unwrap_err().to_string(),
                "A required field is missing: api_key"
            );

            let provider_no_path = $provider_type::<$model_struct>::builder()
                .provider_name("test-provider-no-path")
                .api_key("test-api-key")
                .base_url("http://localhost:8080")
                .build()
                .unwrap();
            assert_eq!(provider_no_path.settings.path, None);

            // should have model_name() method for dynamic model
            let _provider_dynamic = $provider_type::model_name("test-model".to_string());

            // should have model_name() on dynamic model builder
            let provider_dynamic_builder = $provider_type::<DynamicModel>::builder()
                .model_name("test-model".to_string())
                .api_key("test-api-key")
                .base_url("http://localhost:8080")
                .build()
                .unwrap();

            assert_eq!(provider_dynamic_builder.name(), "test-model");
        }
    };
}

// Generate basic text generation tests
macro_rules! generate_basic_tests {
    ($default_model:expr) => {
        #[tokio::test]
        async fn test_generate_text_basic() {
            skip_if_no_api_key!();

            let result = LanguageModelRequest::builder()
                .model($default_model)
                .prompt("Respond with exactly the word 'hello' in all lowercase.Do not include any punctuation, prefixes, or suffixes.")
                .build()
                .generate_text()
                .await;

            assert!(result.is_ok());

            let text = result
                .as_ref()
                .expect("")
                .text()
                .unwrap()
                .trim()
                .to_string();

            assert!(text.contains("hello"));
        }

        #[tokio::test]
        async fn test_generate_text_with_system_prompt() {
            skip_if_no_api_key!();

            let result = LanguageModelRequest::builder()
                .model($default_model)
                .system("Only say hello whatever the user says. all lowercase no punctuation, prefixes, or suffixes.")
                .prompt("Hello how are you doing?")
                .build()
                .generate_text()
                .await;

            assert!(result.is_ok());

            let text = result
                .as_ref()
                .expect("Failed to get result")
                .text()
                .unwrap()
                .trim()
                .to_string();
            assert!(text.contains("hello"));
        }

        #[tokio::test]
        async fn test_generate_text_with_messages() {
            skip_if_no_api_key!();

            let messages = Message::builder()
                .system("You are a helpful assistant.")
                .user("Whatsup?, Surafel is here")
                .assistant("How could I help you?")
                .user("Could you tell my name?")
                .build();

            let mut language_model = LanguageModelRequest::builder()
                .model($default_model)
                .messages(messages)
                .build();

            let result = language_model.generate_text().await;
            assert!(result.is_ok());

            let text = result
                .as_ref()
                .expect("Failed to get result")
                .text()
                .unwrap()
                .trim()
                .to_string();
            assert!(text.contains("Surafel"));
        }

        #[tokio::test]
        async fn test_generate_text_with_messages_and_system_prompt() {
            skip_if_no_api_key!();

            let messages = Message::builder()
                .system("Only say hello whatever the user says. \n all lowercase no punctuation, prefixes, or suffixes.")
                .user("Whatsup?, Surafel is here")
                .assistant("How could I help you?")
                .user("Could you tell my name?")
                .build();

            let result = LanguageModelRequest::builder()
                .model($default_model)
                .system("Only say hello whatever the user says. all lowercase no punctuation, prefixes, or suffixes.")
                .messages(messages)
                .build()
                .generate_text()
                .await;

            assert!(result.is_ok());

            let text = result
                .as_ref()
                .expect("Failed to get result")
                .text()
                .unwrap()
                .trim()
                .to_string();
            assert!(text.contains("hello"));
        }
    };
}

/// Generate stop reason tests
macro_rules! generate_language_model_stop_reason_tests {
    ($default_model:expr) => {
        #[tokio::test]
        async fn test_stop_reason_normal_finish() {
            skip_if_no_api_key!();

            let result = LanguageModelRequest::builder()
                .model($default_model)
                .prompt("Respond with exactly the word 'hello' in all lowercase. Do not include any punctuation.")
                .build()
                .generate_text()
                .await;

            assert!(result.is_ok());
            let response = result.unwrap();
            assert!(matches!(response.stop_reason(), Some(StopReason::Finish)));
        }

        #[tokio::test]
        async fn test_stop_reason_hook_stop() {
            skip_if_no_api_key!();

            let result = LanguageModelRequest::builder()
             .model($default_model)
             .prompt("just response with 'HI' nothing less nothing more")
             .stop_when(|_| true) // Always stop
             .build()
             .generate_text()
             .await;

            assert!(result.is_ok());
            let response = result.unwrap();
            assert!(matches!(response.stop_reason(), Some(StopReason::Hook)));
         }

        #[tokio::test]
        async fn test_stop_reason_api_error() {
            skip_if_no_api_key!();

            let result = LanguageModelRequest::builder()
                .model($default_model)
                .prompt("Hello")
                .build()
                .generate_text()
                .await;

            // Should fail, but if it succeeds, check stop_reason
            if let Ok(response) = result {
                // If somehow succeeds, but unlikely
                assert!(matches!(response.stop_reason(), Some(StopReason::Finish)));
            } else {
                // Error occurred, but stop_reason is set in the options before error
                // Since result is Err, we can't check response.stop_reason
                // Perhaps modify to check options, but for now, just assert error
                assert!(result.is_err());
            }
        }

        #[tokio::test]
        async fn test_stop_reason_stream_finish() {
            skip_if_no_api_key!();

            let mut response = LanguageModelRequest::builder()
                .model($default_model)
                .prompt("Respond with 'world'")
                .build()
                .stream_text()
                .await
                .unwrap();

            // consume the stream
            while let Some(_) = response.stream.next().await {}

            // The stream is already consumed internally, stop_reason is set
            assert!(matches!(response.stop_reason().await, Some(StopReason::Finish)));
        }
    };
}

/// Generate streaming tests
macro_rules! generate_language_model_streaming_tests {
    ($reasoning_model:expr, true) => {
        // Skipping streaming tests: provider doesn't support streaming
    };
    ($reasoning_model:expr, false) => {
        #[tokio::test]
        async fn test_generate_stream_basic() {
            skip_if_no_api_key!();

            let response = LanguageModelRequest::builder()
                .model($reasoning_model)
                .prompt("Respond with exactly the word 'hello' in all lowercase.Do not include any punctuation, prefixes, or suffixes.")
                .build()
                .stream_text()
                .await
                .unwrap();

            let mut stream = response.stream;

            let mut buf = String::new();
            while let Some(chunk) = stream.next().await {
                // println!("chunk: {:?}", chunk);
                if let LanguageModelStreamChunkType::TextDelta(text) = chunk {
                    buf.push_str(&text);
                }
            }

            assert!(buf.contains("hello"));
        }

        #[tokio::test]
        async fn test_streaming_with_reasoning_effort() {
            skip_if_no_api_key!();

            let response = LanguageModelRequest::builder()
                .model($reasoning_model)
                .prompt("Count from 1 to 5 step by step")
                .reasoning_effort(agentsdk::core::language_model::ReasoningEffort::Medium)
                .build()
                .stream_text()
                .await
                .unwrap();

            let mut stream = response.stream;
            let mut chunks_received = 0;
            while let Some(chunk) = stream.next().await {
                if let LanguageModelStreamChunkType::TextDelta(_) = chunk {
                    chunks_received += 1;
                }
            }

            assert!(chunks_received > 0);
        }

    };
}

/// Generate tool-related tests
macro_rules! generate_language_model_tool_tests {
    ($tool_model:expr, true) => {
        // Skipping tool tests: provider doesn't support tools
    };
    ($tool_model:expr, false) => {
        #[tokio::test]
        async fn test_generate_text_with_tools() {
            skip_if_no_api_key!();

            #[tool]
            /// Returns the username
            fn get_username() -> Tool {
                Ok("ishak".to_string())
            }

            let response = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call a tool to get the username.")
                .prompt("What is the username?")
                .with_tool(get_username())
                .build()
                .generate_text()
                .await
                .unwrap();

            assert!(response.text().unwrap().contains("ishak"));
        }

        #[tokio::test]
        async fn test_generate_text_with_tools_input() {
            skip_if_no_api_key!();

            #[tool]
            /// Returns the username
            fn get_username(user_id: String) -> Tool {
                match user_id.as_str() {
                    "123" => Ok("sura".to_string()),
                    _ => Ok("invalid".to_string()),
                }
            }

            let response = LanguageModelRequest::builder()
                .model($tool_model)
                .system("you are a helpful assistant.")
                .prompt("What is the username with user id 123?")
                .with_tool(get_username())
                .build()
                .generate_text()
                .await
                .unwrap();

            assert!(response.text().unwrap().contains("sura"));
        }

        #[tokio::test]
        async fn test_generate_stream_with_tools() {
            skip_if_no_api_key!();

            #[tool]
            /// Returns the username
            fn get_username() -> Tool {
                Ok("ishak".to_string())
            }

            let response = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call a tool to get the username.")
                .prompt("What is the username?")
                .with_tool(get_username())
                .build()
                .stream_text()
                .await
                .unwrap();

            let mut stream = response.stream;

            let mut buf = String::new();
            while let Some(chunk) = stream.next().await {
                if let LanguageModelStreamChunkType::TextDelta(text) = chunk {
                    buf.push_str(&text);
                }
            }

            assert!(buf.contains("ishak"));
        }

        #[tokio::test]
        async fn test_generate_stream_with_tools_input() {
            skip_if_no_api_key!();

            #[tool]
            /// Returns the username
            fn get_username(user_id: String) -> Tool {
                match user_id.as_str() {
                    "123" => Ok("sura".to_string()),
                    _ => Ok("invalid".to_string()),
                }
            }

            let response = LanguageModelRequest::builder()
                .model($tool_model)
                .system("You are a helpful assistant.")
                .prompt("What is the username for user id '123'?")
                .with_tool(get_username())
                .build()
                .stream_text()
                .await
                .unwrap();

            let mut stream = response.stream;

            let mut buf = String::new();
            while let Some(chunk) = stream.next().await {
                if let LanguageModelStreamChunkType::TextDelta(text) = chunk {
                    buf.push_str(&text);
                }
            }

            assert!(buf.contains("sura"));
        }

        #[tokio::test]
        async fn test_generate_stream_with_structs() {
            skip_if_no_api_key!();

            // define tool function body, should return Result<String, String>
            #[allow(unused_variables)]
            let func = ToolExecute::from_sync(|_ctx, inp: Value| {
                // Ai SDK will pass in a json object with the following structure
                // ```json
                // {
                //     "location": "New York"
                // }
                // ```
                let location = inp.get("location").unwrap();
                Ok(format!("Cloudy"))
            });

            // define tool input structure
            #[derive(schemars::JsonSchema, Debug)]
            #[allow(dead_code)]
            struct ToolInput {
                location: String,
            }

            // change tool arguments to json schema
            // Which will be similar to the following
            // ```json
            // "properties": {
            //     "location": {
            //         "type": "string"
            //     }
            // }
            let schema = schemars::schema_for!(ToolInput);

            // bring it all together
            let get_weather_tool = Tool::builder()
                .name("get-weather")
                .description("Get the weather information given a location")
                .input_schema(schema.clone())
                .execute(func)
                .build()
                .unwrap();

            // call the model with the tool
            let result = LanguageModelRequest::builder()
                .model($tool_model)
                .system("You are a helpful assistant with access to tools.")
                .prompt("What is the weather in New York?")
                .with_tool(get_weather_tool) // you don't need to call it with.
                .build()
                .generate_text()
                .await;

            assert!(result.is_ok());
        }
    };
}

/// Generate schema/structured output tests
macro_rules! generate_language_model_schema_tests {
    ($structured_output_model:expr, true) => {
        // Skipping structured output tests: provider doesn't support structured output
    };
    ($structured_output_model:expr, false) => {
        #[tokio::test]
        async fn test_generate_text_with_output_schema() {
            skip_if_no_api_key!();

            #[derive(Debug, JsonSchema, Deserialize)]
            #[allow(dead_code)]
            struct User {
                name: String,
                age: u32,
                email: String,
                phone: String,
            }

            let result = LanguageModelRequest::builder()
                .model($structured_output_model)
                .prompt("generate user with dummy data, and and name of 'John Doe'")
                .schema::<User>()
                .build()
                .generate_text()
                .await
                .unwrap();

            let user: User = result.into_schema().unwrap();

            assert_eq!(user.name, "John Doe");
        }

        #[tokio::test]
        async fn test_stream_text_with_output_schema() {
            skip_if_no_api_key!();

            #[derive(Debug, JsonSchema, Deserialize)]
            #[allow(dead_code)]
            struct User {
                name: String,
                age: u32,
                email: String,
                phone: String,
            }

            let response = LanguageModelRequest::builder()
                .model($structured_output_model)
                .prompt("generate user with dummy data, and add name of 'John Doe'")
                .schema::<User>()
                .build()
                .stream_text()
                .await
                .unwrap();

            let mut stream = response.stream;

            let mut buf = String::new();
            while let Some(chunk) = stream.next().await {
                if let LanguageModelStreamChunkType::TextDelta(text) = chunk {
                    buf.push_str(&text);
                }
            }

            let user: User = serde_json::from_str(&buf).unwrap();

            assert_eq!(user.name, "John Doe");
        }
    };
}

/// Generate hook-related tests
macro_rules! generate_language_model_hook_tests {
    ($tool_model:expr) => {
        #[tokio::test]
        async fn test_on_step_start_executes_before_each_step() {
            skip_if_no_api_key!();

            let counter = Arc::new(Mutex::new(0));
            let counter_clone = Arc::clone(&counter);

            #[tool]
            // Returns the neighborhood
            fn get_neighborhood() -> Tool {
                Ok("ankocha".to_string())
            }

            let _ = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call the tool. Return the neighborhood. Nothing more and nothing less")
                .prompt("What is the neighborhood?")
                .with_tool(get_neighborhood())
                .on_step_start(move |_| {
                    let mut c = counter_clone.lock().unwrap();
                    *c += 1;
                })
                .build()
                .generate_text()
                .await
                .unwrap();

            let count = *counter.lock().unwrap();
            assert!(count >= 2); // At least initial + tool step
        }

        #[tokio::test]
        async fn test_on_step_finish_executes_after_each_step() {
            skip_if_no_api_key!();

            let counter = Arc::new(Mutex::new(0));
            let counter_clone = Arc::clone(&counter);

            #[tool]
            // Returns the neighbourhood
            fn get_neighborhood() -> Tool {
                Ok("ankocha".to_string())
            }

            let _ = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call the tool. Return the neighborhood. Nothing more and nothing less")
                .prompt("What is the neighborhood?")
                .with_tool(get_neighborhood())
                .on_step_finish(move |_| {
                    let mut c = counter_clone.lock().unwrap();
                    *c += 1;
                })
                .build()
                .generate_text()
                .await
                .unwrap();

            let count = *counter.lock().unwrap();
            assert!(count >= 2);
        }

        #[tokio::test]
        async fn test_hooks_run_in_correct_order() {
            skip_if_no_api_key!();

            let log = Arc::new(Mutex::new(Vec::new()));
            let log_prepare = Arc::clone(&log);
            let log_finish = Arc::clone(&log);

            #[tool]
            fn get_neighbourhood() -> Tool {
                Ok("ankocha".to_string())
            }

            let result = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call the tool. Return the neighborhood. Nothing more and nothing less")
                .prompt("What is the neighborhood?")
                .with_tool(get_neighbourhood())
                .on_step_start(move |_| {
                    log_prepare.lock().unwrap().push("prepare");
                })
                .on_step_finish(move |_| {
                    log_finish.lock().unwrap().push("finish");
                })
                .build()
                .generate_text()
                .await
                .unwrap();

            assert!(result.text().is_some());

            let log = log.lock().unwrap();
            // Check pairs of prepare/finish
            let mut i = 0;
            while i + 1 < log.len() {
                assert_eq!(log[i], "prepare");
                assert_eq!(log[i + 1], "finish");
                i += 2;
            }
        }

        #[tokio::test]
        async fn test_stop_when_halts_during_tool_call() {
            skip_if_no_api_key!();

            #[tool]
            fn get_neighborhood() -> Tool {
                Ok("done".to_string())
            }

            let result = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call the tool. Return the neighborhood. Nothing more and nothing less")
                .prompt("What is the neighborhood?")
                .with_tool(get_neighborhood())
                .stop_when(|_| true) // Always stop
                .build()
                .generate_text()
                .await
                .unwrap();

            let step_ids = result.step_ids();
            // Should stop after tool call, no final text step
            assert!(step_ids.len() < 5); // Less than full flow
            assert!(matches!(result.stop_reason().unwrap(), StopReason::Hook));
        }

        #[tokio::test]
        async fn test_stop_when_halts_during_streaming() {
            skip_if_no_api_key!();

            #[tool]
            fn get_neighbourhood() -> Tool {
                Ok("ankocha".to_string())
            }

            let response = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call the tool. Return the neighborhood. Nothing more and nothing less")
                .prompt("What is the neighborhood?")
                .with_tool(get_neighbourhood())
                .stop_when(|_| true)
                .build()
                .stream_text()
                .await
                .unwrap();

            let mut stream = response.stream;
            let mut found_incomplete = false;
            while let Some(chunk) = stream.next().await {
                if let LanguageModelStreamChunkType::Incomplete(_) = chunk {
                    found_incomplete = true;
                    break;
                }
            }
            assert!(found_incomplete);
        }

        #[tokio::test]
        async fn test_stop_when_false_allows_continuation() {
            skip_if_no_api_key!();

            #[tool]
            fn get_neighbourhood() -> Tool {
                Ok("anckocha".to_string())
            }

            let result = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call the tool. Return the neighborhood. Nothing more and nothing less")
                .prompt("What is the neighborhood?")
                .with_tool(get_neighbourhood())
                .stop_when(|_| false) // Never stop
                .build()
                .generate_text()
                .await
                .unwrap();

            let step_ids = result.step_ids();
            assert!(step_ids.len() >= 5); // Full flow
            assert!(result.text().is_some());
        }

        #[tokio::test]
        async fn test_hooks_persist_across_multiple_steps() {
            skip_if_no_api_key!();

            let counter = Arc::new(Mutex::new(0));
            let counter_clone = Arc::clone(&counter);

            #[tool]
            fn get_neighbourhood() -> Tool {
                Ok("You can find the neighborhood if you call the get_neighbourhood_2 tool".to_string())
            }

            #[tool]
            fn get_neighbourhood_2() -> Tool {
                Ok("ankocha".to_string())
            }

            let _ = LanguageModelRequest::builder()
                .model($tool_model)
                .system(
                    "Call the tool get_neighbourhood. Return the neighborhood. 
                    Nothing more and nothing less. If you can't find the neighborhood,
                    call the tool get_neighbourhood_2. Return the neighborhood.
                    Nothing more and nothing less",
                )
                .prompt("What is the neighborhood?")
                .with_tool(get_neighbourhood())
                .with_tool(get_neighbourhood_2())
                .on_step_finish(move |_| {
                    let mut c = counter_clone.lock().unwrap();
                    *c += 1;
                })
                .build()
                .generate_text()
                .await
                .unwrap();

            let count = *counter.lock().unwrap();
            assert!(count >= 3); // Multiple steps
        }

        #[tokio::test]
        async fn test_hooks_cloned_via_arc() {
            skip_if_no_api_key!();

            let called = Arc::new(Mutex::new(false));
            let called_clone = Arc::clone(&called);

            let _ = LanguageModelRequest::builder()
                .model($tool_model)
                .prompt("Say hello")
                .on_step_finish(move |_| {
                    *called_clone.lock().unwrap() = true;
                })
                .build()
                .generate_text()
                .await
                .unwrap();

            assert!(*called.lock().unwrap());
        }

        #[tokio::test]
        async fn test_no_panic_when_hooks_none() {
            skip_if_no_api_key!();

            let result = LanguageModelRequest::builder()
                .model($tool_model)
                .prompt("Say hello")
                .build()
                .generate_text()
                .await;

            assert!(result.is_ok());
        }

        #[tokio::test]
        async fn test_on_step_start_mutates_options() {
            skip_if_no_api_key!();

            let result = LanguageModelRequest::builder()
                .model($tool_model)
                .prompt("Say hello")
                .on_step_start(|opts| {
                    opts.system = Some("Updated system message!!!".to_string());
                })
                .build()
                .generate_text()
                .await
                .unwrap();

            // Hard to verify mutation directly, but ensure no panic and response ok
            assert!(result.text().is_some());
            assert_eq!(result.options.system.unwrap(), "Updated system message!!!");
        }

        #[tokio::test]
        async fn test_hook_isolation() {
            skip_if_no_api_key!();

            // Without hooks
            let result_no_hooks = LanguageModelRequest::builder()
                .model($tool_model)
                .prompt("Say hello")
                .build()
                .generate_text()
                .await
                .unwrap();

            // With hooks (should not affect output)
            let result_with_hooks = LanguageModelRequest::builder()
                .model($tool_model)
                .prompt("Say hello")
                .on_step_finish(|_| {})
                .build()
                .generate_text()
                .await
                .unwrap();

            // Outputs should be similar (hooks don't change logic)
            assert!(result_no_hooks.text().is_some());
            assert!(result_with_hooks.text().is_some());
        }

        #[tokio::test]
        async fn test_on_step_finish_for_text_reasoning_and_tool_call() {
            skip_if_no_api_key!();

            let called_for_text = Arc::new(Mutex::new(false));
            let called_for_tool = Arc::new(Mutex::new(false));
            let text_clone = Arc::clone(&called_for_text);
            let tool_clone = Arc::clone(&called_for_tool);

            #[tool]
            // Returns the username
            fn get_username() -> Tool {
                Ok("ishak".to_string())
            }

            let result = LanguageModelRequest::builder()
                .model($tool_model)
                .system("Call the tool. to find the username. and return only the username nothing more and nothing less")
                .prompt("What is the username")
                .with_tool(get_username())
                .on_step_finish(move |opts| {
                    if let Some(Message::Assistant(assistant_msg)) = opts.messages().last() {
                        match &assistant_msg.content {
                            LanguageModelResponseContentType::ToolCall(_) => {
                                *tool_clone.lock().unwrap() = true;
                            }
                            LanguageModelResponseContentType::Text(_) => {
                                *text_clone.lock().unwrap() = true;
                            }
                            LanguageModelResponseContentType::Reasoning { .. } => {
                                *text_clone.lock().unwrap() = true;
                            }
                            _ => {}
                        }
                    }
                })
                .build()
                .generate_text()
                .await
                .unwrap();

            assert!(!*called_for_tool.lock().unwrap());
            assert!(*called_for_text.lock().unwrap());
            assert_eq!(result.text().unwrap(), "ishak");
        }

        #[tokio::test]
        async fn test_streaming_on_step_start_before_start() {
            skip_if_no_api_key!();

            let called = Arc::new(Mutex::new(false));
            let called_clone = Arc::clone(&called);

            let mut response = LanguageModelRequest::builder()
                .model($tool_model)
                .prompt("Say hello")
                .on_step_start(move |_| {
                    *called_clone.lock().unwrap() = true;
                })
                .build()
                .stream_text()
                .await
                .unwrap();

            // consume the stream
            while let Some(_) = response.stream.next().await {}

            assert!(*called.lock().unwrap()); // Called before streaming starts
        }

        #[tokio::test]
        async fn test_streaming_on_step_finish_at_end() {
            skip_if_no_api_key!();

            let called = Arc::new(Mutex::new(false));
            let called_clone = Arc::clone(&called);

            let response = LanguageModelRequest::builder()
                .model($tool_model)
                .prompt("Say hello")
                .on_step_finish(move |_| {
                    *called_clone.lock().unwrap() = true;
                })
                .build()
                .stream_text()
                .await
                .unwrap();

            let mut stream = response.stream;
            while stream.next().await.is_some() {} // Consume stream

            assert!(*called.lock().unwrap()); // Called after End
        }
    };
}

/// Generate step ID tests
macro_rules! generate_language_model_step_id_tests {
    ($default_model:expr) => {
        #[tokio::test]
        async fn test_step_id_basic_assignment() {
            skip_if_no_api_key!();

            let result = LanguageModelRequest::builder()
                .model($default_model)
                .system("Do the exact instructions you are told")
                .prompt("Respond with exactly 'test' in lowercase.")
                .build()
                .generate_text()
                .await
                .unwrap();

            // Check step_ids: system (0), user (0), assistant (1)
            let step_ids = result.step_ids();
            assert_eq!(step_ids.len(), 3);
            assert_eq!(step_ids[0], 0); // system
            assert_eq!(step_ids[1], 0); // user
            assert_eq!(step_ids[2], 1); // assistant
        }

        #[tokio::test]
        async fn test_step_id_tool_call_flow() {
            skip_if_no_api_key!();

            #[tool]
            fn get_test_value() -> Tool {
                Ok("test_value".to_string())
            }

            let result = LanguageModelRequest::builder()
                .model($default_model)
                .system("Call the tool to get the test value.")
                .prompt("What is the test value?")
                .with_tool(get_test_value())
                .build()
                .generate_text()
                .await
                .unwrap();

            let step_ids = dbg!(&result).step_ids();
            // system (0), user (0), assistant tool call (1), tool result (1), assistant text (3)
            assert!(dbg!(&step_ids).len() >= 5);
            assert_eq!(step_ids[0], 0);
            assert_eq!(step_ids[1], 0);
            assert_eq!(step_ids[2], 1); // assistant tool call
            assert_eq!(step_ids[3], 1); // tool result
            assert_eq!(step_ids[4], 2); // assistant text
            assert!(result.text().unwrap().contains("test_value"));
        }

        #[tokio::test]
        async fn test_step_id_streaming() {
            skip_if_no_api_key!();

            let response = LanguageModelRequest::builder()
                .model($default_model)
                .system("Do the exact instructions you are told")
                .prompt("Respond with 'stream test'")
                .build()
                .stream_text()
                .await
                .unwrap();

            let step_ids = response.step_ids().await;
            // user (0), assistant (0)
            assert_eq!(step_ids.len(), 2);
            assert_eq!(step_ids[0], 0);
            assert_eq!(step_ids[1], 0);
        }
    };
}

/// Generate reasoning tests
macro_rules! generate_language_model_reasoning_tests {
    ($reasoning_model:expr, true) => {
        // Skipping reasoning tests: provider doesn't support reasoning
    };
    ($reasoning_model:expr, false) => {
        // TODO: sura please fix this
        #[tokio::test]
        async fn test_reasoning_effort_with_non_reasoning_model() {
            skip_if_no_api_key!();

            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| async {
                LanguageModelRequest::builder()
                    .model($reasoning_model)
                    .prompt("What is 2 + 2? Answer with just the number.")
                    .reasoning_effort(agentsdk::core::language_model::ReasoningEffort::Low)
                    .build()
                    .generate_text()
                    .await
            }))
            .map(|future| tokio::runtime::Handle::current().block_on(future));

            assert!(result.is_err());
        }
    };
}

/// Generate embedding tests
macro_rules! generate_embedding_tests {
    ($embedding_model:expr, true) => {
        // Skipping embedding tests: provider doesn't support embeddings
    };
    ($embedding_model:expr, false) => {
        use agentsdk::core::embedding_model::EmbeddingModelRequest;

        #[tokio::test]
        async fn test_single_embedding_request() {
            skip_if_no_api_key!();

            let result = EmbeddingModelRequest::builder()
                .model($embedding_model)
                .input(vec!["Hello, world!".to_string()])
                .dimensions(100)
                .build()
                .embed()
                .await
                .expect("Embedding request failed");

            // Check that we got back at least one embedding
            assert!(!result.is_empty(), "Expected at least one embedding");

            // Check that the first embedding is a vector of floats
            assert!(
                !result[0].is_empty(),
                "Expected embedding vector to be non-empty"
            );
            assert_eq!(result[0].len(), 100);

            // Check that all values are valid floats (not NaN or infinity)
            for value in &result[0] {
                assert!(
                    value.is_finite(),
                    "Embedding values should be finite floats"
                );
            }
        }

        #[tokio::test]
        async fn test_embedding_with_multiple_inputs() {
            skip_if_no_api_key!();

            let inputs = vec![
                "Hello, world!".to_string(),
                "This is a test".to_string(),
                "Multiple embeddings".to_string(),
            ];

            let result = EmbeddingModelRequest::builder()
                .model($embedding_model)
                .input(inputs.clone())
                .build()
                .embed()
                .await
                .expect("Embedding request failed");

            // Check that we got back embeddings for all inputs
            assert_eq!(
                result.len(),
                inputs.len(),
                "Expected {} embeddings, got {}",
                inputs.len(),
                result.len()
            );

            // Check that each embedding is a valid vector of floats
            for (i, embedding) in result.iter().enumerate() {
                assert!(!embedding.is_empty(), "Embedding {} should not be empty", i);

                for (j, value) in embedding.iter().enumerate() {
                    assert!(
                        value.is_finite(),
                        "Embedding {} value {} should be a finite float",
                        i,
                        j
                    );
                }
            }
        }
    };
}