@@ -351,7 +351,7 @@ def __init__(self):
351351 assert f"Fail to load '{ agent_name } ' module." in str (exc_info .value )
352352 assert "No module named 'non_existent_module'" in str (exc_info .value )
353353
354- def test_agent_internal_import_error (self ):
354+ def test_agent_internal_syntax_error (self ):
355355 """Test other import errors within an agent's code (e.g., SyntaxError)."""
356356 with tempfile .TemporaryDirectory () as temp_dir :
357357 temp_path = Path (temp_dir )
@@ -380,10 +380,47 @@ def __init__(self):
380380 ) as exc_info : # Or potentially ImportError depending on Python version specifics with importlib
381381 loader .load_agent (agent_name )
382382
383- assert f"Fail to load '{ agent_name } ' module." in str (exc_info .value )
383+ assert str (exc_info .value ).startswith (
384+ f"Fail to load '{ agent_name } ' module."
385+ )
384386 # Check for part of the original SyntaxError message
385387 assert "invalid syntax" in str (exc_info .value ).lower ()
386388
389+ def test_agent_internal_name_error (self ):
390+ """Test other import errors within an agent's code (e.g., SyntaxError)."""
391+ with tempfile .TemporaryDirectory () as temp_dir :
392+ temp_path = Path (temp_dir )
393+ agent_name = "name_error_agent"
394+
395+ # Create agent with a syntax error (which leads to ImportError)
396+ agent_file = temp_path / f"{ agent_name } .py"
397+ agent_file .write_text (dedent (f"""
398+ from google.adk.agents.base_agent import BaseAgent
399+
400+ # name is not defined
401+ print(non_existing_name)
402+
403+ class { agent_name .title ()} Agent(BaseAgent):
404+ def __init__(self):
405+ super().__init__(name="{ agent_name } ")
406+
407+ root_agent = { agent_name .title ()} Agent()
408+ """ ))
409+
410+ loader = AgentLoader (str (temp_path ))
411+ # SyntaxError is a subclass of Exception, and importlib might wrap it
412+ # The loader is expected to prepend its message and re-raise.
413+ with pytest .raises (
414+ NameError
415+ ) as exc_info : # Or potentially ImportError depending on Python version specifics with importlib
416+ loader .load_agent (agent_name )
417+
418+ assert str (exc_info .value ).startswith (
419+ f"Fail to load '{ agent_name } ' module."
420+ )
421+ # Check for part of the original SyntaxError message
422+ assert "is not defined" in str (exc_info .value ).lower ()
423+
387424 def test_sys_path_modification (self ):
388425 """Test that agents_dir is added to sys.path correctly."""
389426 with tempfile .TemporaryDirectory () as temp_dir :
0 commit comments