forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_all_examples.py
More file actions
executable file
·1613 lines (1421 loc) · 70.9 KB
/
Copy pathvalidate_all_examples.py
File metadata and controls
executable file
·1613 lines (1421 loc) · 70.9 KB
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
#!/usr/bin/env python
"""
validate_all_examples.py - Comprehensive validation for all python-a2a examples
This script validates all examples across the entire project to ensure
they're working correctly before each release. It tests each example category
including streaming, building_blocks, applications, ai_powered_agents, etc.
Usage:
python validate_all_examples.py [--category CATEGORY] [--verbose] [--skip-slow] [--concurrent WORKERS]
Options:
--category Specific category to test (streaming, building_blocks, etc.)
--verbose Show detailed output during validation
--skip-slow Skip time-consuming examples
--concurrent Maximum number of concurrent example runs (default: 4)
Higher values speed up validation but may cause resource contention.
Set to 1 for sequential execution.
"""
import os
import sys
import argparse
import subprocess
import time
import json
import importlib.util
import concurrent.futures
import threading
import signal
import random
from typing import List, Dict, Any, Optional, Tuple
# ANSI colors for prettier output
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
RED = "\033[31m"
CYAN = "\033[36m"
MAGENTA = "\033[35m"
BOLD = "\033[1m"
RESET = "\033[0m"
# Get the root directory of the project
EXAMPLES_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT = os.path.dirname(EXAMPLES_DIR)
# Categories and examples to validate
# Format: category_name -> list of examples with metadata
EXAMPLE_CATEGORIES = {
"getting_started": {
"description": "Basic usage examples",
"examples": [
{
"name": "hello_a2a",
"file": "hello_a2a.py",
"description": "Simple hello world example",
"args": [],
"success_markers": ["Welcome to Python A2A", "Basic A2A Message", "Response Message", "Message Properties", "You've created your first A2A messages"],
"timeout": 10,
"requires_api_key": False,
"expected_non_zero_exit": False
},
{
"name": "simple_client",
"file": "simple_client.py",
"description": "Simple A2A client example",
"args": [],
"success_markers": ["Client", "request", "response"],
"timeout": 30, # Increased timeout
"requires_api_key": False,
"server_example": True, # Flag that this example starts a server
"supports_port_arg": True # Flag that this example accepts a port argument
},
{
"name": "simple_server",
"file": "simple_server.py",
"description": "Simple A2A server example",
"args": [], # No special args needed
"success_markers": ["Server started", "listening"],
"timeout": 10,
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True,
"interactive": True, # Flag that this example is interactive
"test_args": ["--auto-test"] # Args for non-interactive testing
},
{
"name": "function_calling",
"file": "function_calling.py",
"description": "Function calling example",
"args": [],
"success_markers": ["function", "call", "result"],
"timeout": 120, # Increased timeout significantly as this example can be slow
"requires_api_key": False,
"expected_non_zero_exit": False,
"slow_example": True, # Mark as slow so it's skipped with --skip-slow
"skip_interactive": True # Skip direct execution to avoid timeout
},
]
},
"building_blocks": {
"description": "Core building blocks examples",
"examples": [
{
"name": "messages_and_conversations",
"file": "messages_and_conversations.py",
"description": "Working with messages and conversations",
"args": [],
"success_markers": ["Message created", "Conversation"],
"timeout": 10,
"requires_api_key": False
},
{
"name": "agent_skills",
"file": "agent_skills.py",
"description": "Defining agent skills",
"args": [],
"success_markers": ["A2A Agent Skills", "Utility Assistant", "Testing Agent Skills", "Input:", "Response:", "You've learned how to use A2A agent and skill decorators"],
"timeout": 10,
"requires_api_key": False
},
{
"name": "tasks",
"file": "tasks.py",
"description": "Working with tasks",
"args": [],
"success_markers": ["task", "status", "completed"],
"timeout": 10,
"requires_api_key": False
},
{
"name": "agent_discovery",
"file": "agent_discovery.py",
"description": "Agent discovery mechanisms",
"args": [],
"success_markers": ["discovery", "agent", "found"],
"timeout": 10,
"requires_api_key": False
},
]
},
"streaming": {
"description": "Streaming examples",
"examples": [
{
"name": "basic_streaming",
"file": "basic_streaming.py",
"description": "Minimal streaming implementation",
"args": ["--port", "8001"],
"success_markers": ["Streaming Example", "Starting streaming server", "Streaming with sse", "chunk", "stream_events", "Running a streaming test"],
"timeout": 20,
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True, # Flag that this example supports the port argument
"interactive": True, # Flag that this example is interactive
"test_args": ["--port", "8001", "--auto-test", "--query", "test query"] # Args for non-interactive testing
},
{
"name": "01_basic_streaming",
"file": "01_basic_streaming.py",
"description": "Basic streaming with comparison",
"args": ["--port", "8002"],
"success_markers": ["streaming", "response", "received"],
"timeout": 20,
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True,
"interactive": True
},
{
"name": "02_advanced_streaming",
"file": "02_advanced_streaming.py",
"description": "Advanced streaming techniques",
"args": ["--port", "8003", "--style", "sentence", "--delay", "fast"],
"success_markers": ["streaming", "complete", "chunks"],
"timeout": 20,
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True,
"interactive": True
},
{
"name": "03_streaming_llm_integration",
"file": "03_streaming_llm_integration.py",
"description": "LLM integration with streaming",
"args": ["--port", "8004", "--provider", "mock_openai"],
"success_markers": ["LLM", "streaming", "response"],
"timeout": 20,
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True,
"interactive": True
},
{
"name": "04_task_based_streaming",
"file": "04_task_based_streaming.py",
"description": "Task-based streaming",
"args": ["--port", "8005", "--steps", "2"],
"success_markers": ["task", "streaming", "artifacts"],
"timeout": 20,
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True,
"interactive": True
},
{
"name": "05_streaming_ui_integration",
"file": "05_streaming_ui_integration.py",
"description": "UI integration for streaming",
"args": ["--port", "8006"], # Removed headless flag which doesn't exist
"success_markers": ["UI", "streaming", "response"],
"timeout": 30, # Increased timeout
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True,
"interactive": True,
"skip_interactive": True # Mark to skip this since it requires direct user input
},
{
"name": "06_distributed_streaming",
"file": "06_distributed_streaming.py",
"description": "Distributed streaming architecture",
"args": ["--port", "8007"], # Removed servers flag which doesn't exist
"success_markers": ["distributed", "streaming", "servers"],
"timeout": 40, # Increased timeout further
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True,
"interactive": True
},
]
},
"ai_powered_agents": {
"description": "AI-powered agent examples",
"examples": [
{
"name": "openai_agent",
"file": "openai_agent.py",
"description": "OpenAI powered agent",
"args": ["--test-only", "--test-mode"], # Use test-only and test-mode flags
"success_markers": ["OpenAI", "Agent", "Model:", "Temperature:", "dependencies", "Test mode"],
"timeout": 15,
"requires_api_key": False, # No longer requires API key with test-mode
"api_env_var": "OPENAI_API_KEY",
"server_example": True,
"supports_port_arg": True
},
{
"name": "anthropic_agent",
"file": "anthropic_agent.py",
"description": "Anthropic Claude powered agent",
"args": ["--test-only"], # Check if test-only flag is available
"success_markers": ["Anthropic", "Claude", "response"],
"timeout": 15,
"requires_api_key": True,
"api_env_var": "ANTHROPIC_API_KEY",
"server_example": True,
"supports_port_arg": True
},
{
"name": "bedrock_agent",
"file": "bedrock_agent.py",
"description": "AWS Bedrock powered agent",
"args": ["--test-only"], # Check if test-only flag is available
"success_markers": ["Bedrock", "AWS", "response"],
"timeout": 15,
"requires_api_key": True,
"api_env_var": "AWS_ACCESS_KEY_ID",
"server_example": True,
"supports_port_arg": True
},
{
"name": "llm_client",
"file": "llm_client.py",
"description": "Generic LLM client",
"args": ["--test-mode"], # Add test-mode flag
"success_markers": ["LLM Client Example", "All dependencies are installed", "Test mode", "Mock", "provider"],
"timeout": 15, # Increased timeout to allow proper completion
"requires_api_key": False, # No longer requires API key with test-mode
"api_env_var": "OPENAI_API_KEY",
"interactive": False
},
]
},
"applications": {
"description": "Application examples",
"examples": [
{
"name": "weather_assistant",
"file": "weather_assistant.py",
"description": "Weather assistant application",
"args": ["--no-test"], # Use no-test flag to run without tests
"success_markers": ["Weather Assistant Example", "Available Skills", "Cities with Weather Data", "Starting Weather Assistant", "Current Weather", "Weather Forecast", "Activity Recommendations"],
"timeout": 15,
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True,
"interactive": True
},
{
"name": "openai_travel_planner",
"file": "openai_travel_planner.py",
"description": "OpenAI-based travel planner",
"args": ["--no-test"], # Use no-test flag instead of demo-mode
"success_markers": ["travel", "plan", "response"],
"timeout": 20,
"requires_api_key": True,
"api_env_var": "OPENAI_API_KEY",
"server_example": True,
"supports_port_arg": True,
"interactive": True
},
]
},
"agent_network": {
"description": "Agent network examples",
"examples": [
{
"name": "agent_discovery",
"file": "agent_discovery.py",
"description": "Agent discovery mechanism",
"args": [],
"success_markers": ["discovery", "agent", "found"],
"timeout": 30, # Increased timeout
"requires_api_key": False,
"server_example": True,
"supports_port_arg": False # Explicitly mark as not supporting port argument
},
{
"name": "agent_network",
"file": "agent_network.py",
"description": "Building agent networks",
"args": [],
"success_markers": ["network", "agent", "communication"],
"timeout": 20,
"requires_api_key": False,
"interactive": True
},
{
"name": "smart_routing",
"file": "smart_routing.py",
"description": "Smart routing between agents",
"args": [],
"success_markers": ["routing", "agent", "selection"],
"timeout": 30, # Increased timeout
"requires_api_key": False,
"server_example": True,
"supports_port_arg": False # Explicitly mark as not supporting port argument
},
]
},
"workflows": {
"description": "Workflow examples",
"examples": [
{
"name": "basic_workflow",
"file": "basic_workflow.py",
"description": "Basic workflow example",
"args": [],
"success_markers": ["Workflow Example", "Creating a simple workflow", "Workflow steps", "Running the workflow", "Step completed", "Workflow execution completed successfully"],
"timeout": 30, # Increased timeout
"requires_api_key": False,
"server_example": True,
"supports_port_arg": False # This script doesn't support port arguments
},
{
"name": "agents_workflow",
"file": "agents_workflow.py",
"description": "Multi-agent workflow",
"args": ["--query", "test query"],
"success_markers": ["workflow", "agents", "completed"],
"timeout": 30, # Increased timeout
"requires_api_key": True,
"api_env_var": "OPENAI_API_KEY,ANTHROPIC_API_KEY", # Needs either OpenAI or Anthropic API key
"server_example": True,
"supports_port_arg": False # This script doesn't support port arguments
},
{
"name": "parallel_workflow",
"file": "parallel_workflow.py",
"description": "Parallel execution workflow",
"args": [],
"success_markers": ["parallel", "workflow", "completed"],
"timeout": 30, # Increased timeout
"requires_api_key": False,
"server_example": True,
"supports_port_arg": False # This script doesn't support port arguments
},
]
},
"mcp": {
"description": "Model Context Protocol examples",
"examples": [
{
"name": "mcp_agent",
"file": "mcp_agent.py",
"description": "MCP agent implementation",
"args": ["--auto-mcp", "--port", "5050"], # Use a specific port to avoid conflicts
"success_markers": ["MCP Agent Example", "dependencies", "MCP-Enabled Agent", "agent port"],
"timeout": 15,
"requires_api_key": False,
"server_example": True,
"supports_port_arg": True
},
{
"name": "mcp_tools",
"file": "mcp_tools.py",
"description": "MCP tools example",
"args": [],
"success_markers": ["MCP", "tool", "Available Tools", "Testing MCP Tools", "Tool testing completed"],
"timeout": 15,
"requires_api_key": False,
"interactive": False
},
{
"name": "openai_mcp_agent",
"file": "openai_mcp_agent.py",
"description": "OpenAI with MCP integration",
"args": ["--no-test"], # Use no-test instead of demo-mode
"success_markers": ["OpenAI", "MCP", "tool"],
"timeout": 20,
"requires_api_key": True,
"api_env_var": "OPENAI_API_KEY",
"server_example": True
},
]
},
"langchain": {
"description": "LangChain integration examples",
"examples": [
{
"name": "a2a_to_langchain",
"file": "a2a_to_langchain.py",
"description": "Converting A2A to LangChain",
"args": ["--test-mode", "--port", "5555"], # Use a specific port to avoid conflicts
"success_markers": ["A2A", "LangChain", "integration", "dependencies", "Test mode"],
"timeout": 60, # Increased timeout for test mode server startup
"requires_api_key": True, # Mark as requiring API key
"api_env_var": "OPENAI_API_KEY",
"server_example": True # These start servers
},
{
"name": "langchain_to_a2a",
"file": "langchain_to_a2a.py",
"description": "Converting LangChain to A2A",
"args": ["--test-mode"], # Now supports test-mode flag
"success_markers": ["LangChain", "A2A", "integration", "dependencies"],
"timeout": 15, # Reduced timeout
"requires_api_key": True, # Mark as requiring API key
"api_env_var": "OPENAI_API_KEY",
"server_example": True
},
{
"name": "langchain_tools_to_mcp",
"file": "langchain_tools_to_mcp.py",
"description": "LangChain tools to MCP conversion",
"args": ["--test-mode"], # Now supports test-mode flag
"success_markers": ["LangChain", "MCP", "tool", "dependencies"],
"timeout": 15, # Reduced timeout
"requires_api_key": True, # Mark as requiring API key
"api_env_var": "OPENAI_API_KEY",
"server_example": True
},
{
"name": "mcp_to_langchain",
"file": "mcp_to_langchain.py",
"description": "MCP to LangChain tools conversion",
"args": ["--test-mode"], # Now supports test-mode flag
"success_markers": ["MCP", "LangChain", "tool", "dependencies"],
"timeout": 15, # Reduced timeout
"requires_api_key": True, # Mark as requiring API key
"api_env_var": "OPENAI_API_KEY",
"server_example": True
},
{
"name": "langchain_agent_with_tools",
"file": "langchain_agent_with_tools.py",
"description": "LangChain Agent with tools",
"args": ["--test-mode"], # Now supports test-mode flag
"success_markers": ["Agent", "tools", "server", "dependencies", "Test mode"],
"timeout": 90, # Longer timeout for agent examples with server startup
"requires_api_key": True,
"api_env_var": "OPENAI_API_KEY",
"server_example": True,
"supports_port_arg": True,
"slow_example": True # Mark as slow to skip in fast runs
},
{
"name": "langchain_advanced_agent",
"file": "langchain_advanced_agent.py",
"description": "Advanced LangChain Agent with specialized tools",
"args": ["--test-mode"], # Now supports test-mode flag
"success_markers": ["Advanced", "Agent", "Specialized", "tools", "dependencies"],
"timeout": 60, # Longer timeout for complex examples
"requires_api_key": True,
"api_env_var": "OPENAI_API_KEY",
"server_example": True,
"supports_port_arg": True,
"slow_example": True # Mark as slow to skip in fast runs
},
{
"name": "langchain_streaming",
"file": "langchain_streaming.py",
"description": "LangChain with streaming support",
"args": ["--test-mode"], # Now supports test-mode flag
"success_markers": ["streaming", "LangChain", "component", "dependencies"],
"timeout": 45, # Increased timeout for streaming
"requires_api_key": True,
"api_env_var": "OPENAI_API_KEY",
"server_example": True,
"supports_port_arg": True,
"slow_example": True # Mark as slow to skip in fast runs
},
]
},
"developer_tools": {
"description": "Developer tools examples",
"examples": [
{
"name": "cli_tools",
"file": "cli_tools.py",
"description": "Command-line tools",
"args": ["--help"], # Keep --help as it's valid
"success_markers": ["CLI", "tools", "commands", "help"],
"timeout": 10,
"requires_api_key": False,
"expected_non_zero_exit": False
},
{
"name": "testing_agents",
"file": "testing_agents.py",
"description": "Testing A2A agents",
"args": [],
"success_markers": ["test", "agent", "assertion"],
"timeout": 20, # Increased timeout
"requires_api_key": False,
"server_example": True
},
{
"name": "interactive_docs",
"file": "interactive_docs.py",
"description": "Interactive documentation",
"args": ["--no-open-browser"], # Use no-open-browser instead of demo-mode
"success_markers": ["docs", "interactive", "example"],
"timeout": 20, # Increased timeout
"requires_api_key": False,
"interactive": True
},
]
},
}
# Keep track of processes to kill at cleanup
active_processes = []
def get_example_path(category: str, example_file: str) -> str:
"""Get the full path to an example file."""
return os.path.join(EXAMPLES_DIR, category, example_file)
def check_import_dependencies() -> List[str]:
"""Check for required dependencies."""
missing = []
# Core dependencies
core_deps = ["python_a2a"]
for dep in core_deps:
try:
importlib.import_module(dep)
except ImportError:
missing.append(dep)
# Optional dependencies based on categories
optional_deps = {
"ai_powered_agents": ["openai", "anthropic", "boto3"],
"langchain": ["langchain", "langchain_openai"],
"streaming": ["aiohttp", "flask"],
}
for category, deps in optional_deps.items():
for dep in deps:
try:
importlib.import_module(dep)
except ImportError:
missing.append(f"{dep} (for {category})")
return missing
def check_api_key(env_var: str) -> bool:
"""
Check if an API key environment variable is set.
Handles both single keys and comma-separated lists of possible keys.
"""
# Check if this is a comma-separated list of possible keys
if "," in env_var:
# If any of the keys are available, that's sufficient
possible_keys = [key.strip() for key in env_var.split(",")]
return any(key in os.environ and os.environ[key] != "" for key in possible_keys)
# Regular single key check
return env_var in os.environ and os.environ[env_var] != ""
def run_example(
category: str,
example: Dict[str, Any],
verbose: bool = False,
worker_id: int = 0
) -> Tuple[bool, str]:
"""
Run a single example and validate its output.
Args:
category: The category of the example
example: The example metadata
verbose: Whether to show detailed output
worker_id: Worker ID for port allocation (0-based)
Returns:
A tuple of (success, details)
"""
# Print visible status to show progress with force_print
force_print(f"Running test: {category}/{example['name']}...")
example_path = get_example_path(category, example["file"])
if not os.path.exists(example_path):
return False, f"Example file not found: {example_path}"
# Handle examples that support test mode differently
# These examples include integrated mock components that don't require API keys
if (category == "langchain" or category == "ai_powered_agents") and "--test-mode" in example.get("args", []):
# For examples with test mode, we can run them even without API keys
# The examples have built-in mocks that will be used in test mode
print(f"🧪 Running {category} example in test mode: {example['name']}")
# Check if we already have a real API key in the environment
api_env_var = example.get("api_env_var", "")
has_real_api_key = False
if api_env_var and api_env_var in os.environ:
# Check that it's a real key (starts with sk-) and not a temporary one
api_key = os.environ.get(api_env_var)
if api_key and api_key.startswith("sk-") and not api_key.startswith("sk-test-key-for-"):
# We have a real API key, so use it instead of a mock/temp key
print(f"✅ Using real {api_env_var} API key from environment for better testing")
has_real_api_key = True
# Bypass API key requirement for test mode examples
bypass_api_check = True
# Only set a temporary key if we don't have a real one
if not has_real_api_key and api_env_var:
# Set temporary dummy API key for test mode examples
os.environ["TEMP_TEST_KEY"] = f"sk-test-key-for-validation-{api_env_var}"
os.environ[api_env_var] = os.environ["TEMP_TEST_KEY"]
print(f"🔑 Setting temporary {api_env_var} API key for validation")
else:
bypass_api_check = False
# Check for API key requirement (unless we're bypassing the check)
if not bypass_api_check and example.get("requires_api_key", False):
# For other examples that require API keys, check if they are available
api_env_var = example.get("api_env_var", "")
if not check_api_key(api_env_var):
return False, f"Missing API key environment variable: {api_env_var}"
# Prepare command with arguments
args = example.get("args", []).copy() # Make a copy to avoid modifying original
# Skip examples that require user interaction and can't be automated
if example.get("skip_interactive", False):
return True, "Skipped interactive example (requires user input)"
# Handle interactive examples - use test_args if available
# but don't add default flags that might not be supported
if example.get("interactive", False):
# Use test_args if they're explicitly provided
test_args = example.get("test_args", None)
if test_args:
# If test_args is provided, use those instead of the regular args
args = test_args.copy()
# For server examples, ensure a unique port only if the example supports it
if example.get("server_example", False):
# Get a unique port for this example
port = get_example_port(category, example, 9000, worker_id)
# Only add port arguments if the example explicitly specifies it should use them
# or if the example already has a port argument
has_port_arg = False
port_idx = -1
for i, arg in enumerate(args):
if arg == "--port" and i + 1 < len(args):
has_port_arg = True
port_idx = i + 1
break
# Only modify port if the example already uses it or explicitly allows it
if has_port_arg or example.get("supports_port_arg", False):
if port_idx >= 0:
# Replace existing port with our unique port
args[port_idx] = str(port)
else:
# Add port argument if needed
args.extend(["--port", str(port)])
# Prepare command
cmd = [sys.executable, example_path] + args
# Setup process
stdout = subprocess.PIPE if not verbose else None
stderr = subprocess.PIPE if not verbose else None
try:
# Set timeout - give interactive examples more time if needed
timeout = example.get("timeout", 30)
# If this is an interactive example being run in non-interactive mode,
# potentially increase the timeout to give it more time
if example.get("interactive", False) and timeout < 60:
timeout = max(timeout, 45) # Give interactive examples at least 45 seconds
# For server examples, use a shorter timeout or other testing strategy
if example.get("server_example", False):
# We may want to just check if the server starts
if verbose:
print(f" Running server example with timeout: {timeout}s")
# Run the example
if verbose:
print(f" Running command: {' '.join(cmd)}")
process = subprocess.Popen(
cmd,
stdout=stdout,
stderr=stderr,
text=True,
bufsize=1
)
# Add to global tracking
active_processes.append(process)
try:
# Wait for completion with timeout
stdout_data, stderr_data = process.communicate(timeout=timeout)
# Remove from tracking
if process in active_processes:
active_processes.remove(process)
# Check exit code - allow non-zero if expected
expected_non_zero = example.get("expected_non_zero_exit", False)
if process.returncode != 0 and not expected_non_zero:
# Include more detailed error information
error_output = ""
if stderr_data:
# Clean up the stderr data for display
lines = stderr_data.splitlines()
if len(lines) > 10:
# Show first 5 and last 5 lines if there's a lot of output
error_output = "\n".join(lines[:5]) + "\n...\n" + "\n".join(lines[-5:])
else:
error_output = stderr_data
return False, f"Example exited with non-zero code: {process.returncode}\n{error_output}"
# Check for success markers in output
if stdout_data:
# Get the success markers
success_markers = example.get("success_markers", [])
markers_found = []
markers_missing = []
for marker in success_markers:
if marker.lower() in stdout_data.lower():
markers_found.append(marker)
else:
markers_missing.append(marker)
# Check for minimum success criteria
min_required = 1 # Default: at least one marker should be found
# Calculate minimum required markers based on example category and nature
# Higher quality verification requires more than just 1 marker
# Calculate min_required as a percentage of total markers
# to ensure we're thoroughly testing all examples
if len(success_markers) >= 5:
# For examples with many markers (≥5), require at least 60%
min_required = max(1, int(len(success_markers) * 0.6))
elif len(success_markers) >= 3:
# For examples with medium markers (3-4), require at least 2
min_required = 2
# For specific examples that need stricter validation
if example.get("name") == "mcp_tools":
# For mcp_tools, require at least 3 markers including structure ones
min_required = max(min_required, 3)
elif example.get("name") == "mcp_agent":
# For mcp_agent, require at least 3 markers for thorough testing
min_required = max(min_required, 3)
# Workflow examples must prove they executed successfully
if "workflow" in example.get("name", "").lower():
workflow_markers = ["step completed", "workflow execution", "completed successfully"]
if any(marker.lower() in stdout_data.lower() for marker in workflow_markers):
min_required = max(min_required, 3)
# Streaming examples need to verify actual streaming functionality
if "streaming" in example.get("name", "").lower():
streaming_markers = ["chunk", "stream", "sse"]
if any(marker.lower() in stdout_data.lower() for marker in streaming_markers):
min_required = max(min_required, 3)
# Check if we found enough markers
if len(markers_found) >= min_required:
return True, f"Success markers found ({len(markers_found)}/{len(success_markers)}): {', '.join(markers_found)}"
else:
# For server examples or interactive examples, this might be okay if they don't output much
if example.get("interactive", False):
# Make it clear in the message that this was an interactive example that was tested
return True, "Interactive example run in auto mode"
elif should_expect_timeout(example):
# This is a server example that's expected to time out, so partial marker detection is okay
if len(markers_found) > 0:
return True, f"Server example with partial success markers: {', '.join(markers_found)}"
return True, "Server example started without errors"
# Full error reporting for failures
return False, f"Insufficient success markers found. Found {len(markers_found)}/{len(success_markers)}: {', '.join(markers_found)}. Missing: {', '.join(markers_missing)}"
else:
# If no output but process exited successfully
return True, "Example ran successfully (no output to verify markers)"
except subprocess.TimeoutExpired:
# Use the helper function to determine if timeout is expected
if should_expect_timeout(example):
# Kill the process but consider it successful
process.kill()
# Remove from tracking
if process in active_processes:
active_processes.remove(process)
return True, "Example timed out as expected (server likely runs indefinitely)"
# For examples that should not time out, this is an error
process.kill()
# Remove from tracking
if process in active_processes:
active_processes.remove(process)
return False, f"Example timed out after {timeout} seconds"
except Exception as e:
return False, f"Error running example: {str(e)}"
def get_example_port(category: str, example: Dict[str, Any], base_port: int = 9000, worker_id: int = 0) -> Optional[int]:
"""
Generate a unique port number for an example if it needs one.
Args:
category: The category name
example: The example metadata
base_port: Base port number to start from
worker_id: Worker ID to further ensure uniqueness (0-based)
Returns:
A port number or None if not needed
"""
# Check if this example needs a port (server examples)
if example.get("server_example", False):
# Create a deterministic port number based on name and worker
port_hash = hash(f"{category}_{example['name']}") % 1000
# Add worker offset to avoid collisions when running in parallel
worker_offset = worker_id * 2000 # Large enough offset to avoid collisions
return base_port + port_hash + worker_offset
return None
def validate_category(
category: str,
examples: List[Dict[str, Any]],
verbose: bool = False,
skip_slow: bool = False,
max_workers: int = 1
) -> Dict[str, Any]:
"""
Validate all examples in a category, with support for parallel execution.
Args:
category: The category name
examples: List of example metadata
verbose: Whether to show detailed output
skip_slow: Whether to skip slow examples
max_workers: Maximum number of concurrent workers
Returns:
Results dictionary with success stats and details
"""
# Diagnostic print for categories that support test mode
if category in ["langchain", "ai_powered_agents"]:
print(f"\n🔍 Special diagnostics for {category} category with {len(examples)} examples")
for i, example in enumerate(examples):
test_mode = "--test-mode" in example.get("args", [])
print(f" Example {i+1}: {example['name']} (test mode: {test_mode})")
print(f" Args: {example.get('args', [])}")
# Start timing
category_start_time = time.time()
results = {
"category": category,
"total": len(examples),
"passed": 0,
"failed": 0,
"skipped": 0,
"details": []
}
# Filter examples if skipping slow ones
examples_to_run = []
for example in examples:
# For examples with test mode, run them even without API keys
is_test_mode_example = False
for cat in ["langchain", "ai_powered_agents"]:
if category == cat and "--test-mode" in example.get("args", []):
is_test_mode_example = True
break
# Add more diagnostics for categories that support test mode
if category in ["langchain", "ai_powered_agents"]:
print(f"Filtering example: {example['name']}")
# Check for test mode
if "--test-mode" in example.get("args", []):
print(f" - Is {category} test mode: Yes")
else:
print(f" - Is {category} test mode: No")
if example.get("requires_api_key", False):
print(f" - Requires API key: Yes ({example.get('api_env_var', '')})")
else:
print(f" - Requires API key: No")
if skip_slow and example.get("slow_example", False):
print(f" - Slow example being skipped: Yes")
else:
print(f" - Slow example being skipped: No")
# Skip slow examples if requested
if skip_slow and (example.get("timeout", 10) > 15 or example.get("slow_example", False)):
results["skipped"] += 1
if example.get("slow_example", False):
skip_reason = "marked as slow example"
else:
skip_reason = "timeout > 15s"
results["details"].append({
"name": example["name"],
"success": None, # None indicates skipped
"message": f"Skipped (--skip-slow flag used, {skip_reason})"
})
# Special handling for examples with test mode to bypass API check
elif is_test_mode_example:
print(f"✅ Running {category} example in test mode: {example['name']}")
examples_to_run.append(example)
# Regular API key check for other examples
elif example.get("requires_api_key", False):
api_env_var = example.get("api_env_var", "")
if not check_api_key(api_env_var):
results["skipped"] += 1
# Format message appropriately for single or multiple keys
if "," in api_env_var:
key_message = f"missing any of these API keys: {api_env_var}"
else:
key_message = f"missing API key: {api_env_var}"
results["details"].append({
"name": example["name"],
"success": None,
"message": f"Skipped ({key_message})"
})
else:
examples_to_run.append(example)
else:
examples_to_run.append(example)
# Set up progress tracking
print(f"{BLUE}Testing {len(examples_to_run)}/{len(examples)} examples in category '{category}'...{RESET}")
# Special case handling for specific examples
# This section handles examples that need special treatment
# Process examples that need special treatment
for example in examples_to_run:
# Handle simple_server example
if example["name"] == "simple_server" and example.get("interactive", False):
# simple_server only supports the port argument, nothing else