1919from textwrap import dedent
2020
2121from google .adk .cli .utils .agent_loader import AgentLoader
22+ from pydantic import ValidationError
2223import pytest
2324
2425
@@ -30,6 +31,8 @@ def cleanup_sys_path(self):
3031 """Ensure sys.path is restored after each test."""
3132 original_path = sys .path .copy ()
3233 original_env = os .environ .copy ()
34+ # Enable WIP features for YAML agent loading tests
35+ os .environ ["ADK_ALLOW_WIP_FEATURES" ] = "true"
3336 yield
3437 sys .path [:] = original_path
3538 # Restore environment variables
@@ -292,7 +295,8 @@ def test_agent_not_found_error(self):
292295 expected_msg_part_1 = "No root_agent found for 'nonexistent_agent'."
293296 expected_msg_part_2 = (
294297 "Searched in 'nonexistent_agent.agent.root_agent',"
295- " 'nonexistent_agent.root_agent'."
298+ " 'nonexistent_agent.root_agent' and"
299+ " 'nonexistent_agent/root_agent.yaml'."
296300 )
297301 expected_msg_part_3 = (
298302 f"Ensure '{ agents_dir } /nonexistent_agent' is structured correctly"
@@ -443,3 +447,129 @@ def test_sys_path_modification(self):
443447 # Now assert path was added
444448 assert str (temp_path ) in sys .path
445449 assert agent .name == "path_agent"
450+
451+ def create_yaml_agent_structure (
452+ self , temp_dir : Path , agent_name : str , yaml_content : str
453+ ):
454+ """Create an agent structure with YAML configuration.
455+
456+ Args:
457+ temp_dir: The temporary directory to create the agent in
458+ agent_name: Name of the agent
459+ yaml_content: YAML content for the root_agent.yaml file
460+ """
461+ agent_dir = temp_dir / agent_name
462+ agent_dir .mkdir ()
463+
464+ # Create root_agent.yaml file
465+ yaml_file = agent_dir / "root_agent.yaml"
466+ yaml_file .write_text (yaml_content )
467+
468+ def test_load_agent_from_yaml_config (self ):
469+ """Test loading an agent from YAML configuration."""
470+ with tempfile .TemporaryDirectory () as temp_dir :
471+ temp_path = Path (temp_dir )
472+ agent_name = "yaml_agent"
473+
474+ # Create YAML configuration
475+ yaml_content = dedent ("""
476+ agent_class: LlmAgent
477+ name: yaml_test_agent
478+ model: gemini-2.0-flash
479+ instruction: You are a test agent loaded from YAML configuration.
480+ description: A test agent created from YAML config
481+ """ )
482+
483+ self .create_yaml_agent_structure (temp_path , agent_name , yaml_content )
484+
485+ # Load the agent
486+ loader = AgentLoader (str (temp_path ))
487+ agent = loader .load_agent (agent_name )
488+
489+ # Assert agent was loaded correctly
490+ assert agent .name == "yaml_test_agent"
491+ # Check if it's an LlmAgent before accessing model and instruction
492+ from google .adk .agents .llm_agent import LlmAgent
493+
494+ if isinstance (agent , LlmAgent ):
495+ assert agent .model == "gemini-2.0-flash"
496+ # Handle instruction which can be string or InstructionProvider
497+ instruction_text = str (agent .instruction )
498+ assert "test agent loaded from YAML" in instruction_text
499+
500+ def test_yaml_agent_caching_returns_same_instance (self ):
501+ """Test that loading the same YAML agent twice returns the same instance."""
502+ with tempfile .TemporaryDirectory () as temp_dir :
503+ temp_path = Path (temp_dir )
504+ agent_name = "cached_yaml_agent"
505+
506+ # Create YAML configuration
507+ yaml_content = dedent ("""
508+ agent_class: LlmAgent
509+ name: cached_yaml_test_agent
510+ model: gemini-2.0-flash
511+ instruction: You are a cached test agent.
512+ """ )
513+
514+ self .create_yaml_agent_structure (temp_path , agent_name , yaml_content )
515+
516+ # Load the agent twice
517+ loader = AgentLoader (str (temp_path ))
518+ agent1 = loader .load_agent (agent_name )
519+ agent2 = loader .load_agent (agent_name )
520+
521+ # Assert same instance is returned
522+ assert agent1 is agent2
523+ assert agent1 .name == agent2 .name
524+
525+ def test_yaml_agent_not_found_error (self ):
526+ """Test that appropriate error is raised when YAML agent is not found."""
527+ with tempfile .TemporaryDirectory () as temp_dir :
528+ loader = AgentLoader (temp_dir )
529+ agents_dir = temp_dir # For use in the expected message string
530+
531+ # Try to load non-existent YAML agent
532+ with pytest .raises (ValueError ) as exc_info :
533+ loader .load_agent ("nonexistent_yaml_agent" )
534+
535+ expected_msg_part_1 = "No root_agent found for 'nonexistent_yaml_agent'."
536+ expected_msg_part_2 = (
537+ "Searched in 'nonexistent_yaml_agent.agent.root_agent',"
538+ " 'nonexistent_yaml_agent.root_agent' and"
539+ " 'nonexistent_yaml_agent/root_agent.yaml'."
540+ )
541+ expected_msg_part_3 = (
542+ f"Ensure '{ agents_dir } /nonexistent_yaml_agent' is structured"
543+ " correctly"
544+ )
545+
546+ assert expected_msg_part_1 in str (exc_info .value )
547+ assert expected_msg_part_2 in str (exc_info .value )
548+ assert expected_msg_part_3 in str (exc_info .value )
549+
550+ def test_yaml_agent_invalid_yaml_error (self ):
551+ """Test that appropriate error is raised when YAML is invalid."""
552+ with tempfile .TemporaryDirectory () as temp_dir :
553+ temp_path = Path (temp_dir )
554+ agent_name = "invalid_yaml_agent"
555+
556+ # Create invalid YAML content with wrong field name
557+ invalid_yaml_content = dedent ("""
558+ agent_type: LlmAgent
559+ name: invalid_yaml_test_agent
560+ model: gemini-2.0-flash
561+ instruction: You are a test agent with invalid YAML
562+ """ )
563+
564+ self .create_yaml_agent_structure (
565+ temp_path , agent_name , invalid_yaml_content
566+ )
567+
568+ loader = AgentLoader (str (temp_path ))
569+
570+ # Try to load agent with invalid YAML
571+ with pytest .raises (ValidationError ) as exc_info :
572+ loader .load_agent (agent_name )
573+
574+ # Should raise some form of YAML parsing error
575+ assert "Extra inputs are not permitted" in str (exc_info .value )
0 commit comments