@@ -1089,3 +1089,121 @@ async def provider(ctx):
10891089
10901090 assert text == 'async provider'
10911091 assert bypass is True
1092+
1093+
1094+ def test_instruction_defaults_to_empty_and_is_omitted ():
1095+ client = _RecordingClient ([[]])
1096+ agent = ManagedAgent (name = 'mgr' , agent_id = 'agents/a' , api_client = client )
1097+
1098+ asyncio .run (_drain (agent ._run_async_impl (_user_ctx ('hi' ))))
1099+
1100+ assert agent .instruction == ''
1101+ assert 'system_instruction' not in client .aio .interactions .calls [0 ]
1102+
1103+
1104+ def test_string_instruction_forwarded_as_system_instruction ():
1105+ client = _RecordingClient ([[]])
1106+ agent = ManagedAgent (
1107+ name = 'mgr' ,
1108+ agent_id = 'agents/a' ,
1109+ instruction = 'You are a terse assistant.' ,
1110+ api_client = client ,
1111+ )
1112+
1113+ asyncio .run (_drain (agent ._run_async_impl (_user_ctx ('hi' ))))
1114+
1115+ assert (
1116+ client .aio .interactions .calls [0 ]['system_instruction' ]
1117+ == 'You are a terse assistant.'
1118+ )
1119+
1120+
1121+ def test_sync_instruction_provider_forwarded_and_bypasses_injection ():
1122+ client = _RecordingClient ([[]])
1123+
1124+ # The '{name}' must be left literal: providers bypass state injection.
1125+ agent = ManagedAgent (
1126+ name = 'mgr' ,
1127+ agent_id = 'agents/a' ,
1128+ instruction = lambda ctx : 'Persona for {name}' ,
1129+ api_client = client ,
1130+ )
1131+
1132+ asyncio .run (_drain (agent ._run_async_impl (_user_ctx ('hi' ))))
1133+
1134+ assert (
1135+ client .aio .interactions .calls [0 ]['system_instruction' ]
1136+ == 'Persona for {name}'
1137+ )
1138+
1139+
1140+ def test_async_instruction_provider_forwarded ():
1141+ client = _RecordingClient ([[]])
1142+
1143+ async def provider (ctx ):
1144+ return 'Async persona.'
1145+
1146+ agent = ManagedAgent (
1147+ name = 'mgr' ,
1148+ agent_id = 'agents/a' ,
1149+ instruction = provider ,
1150+ api_client = client ,
1151+ )
1152+
1153+ asyncio .run (_drain (agent ._run_async_impl (_user_ctx ('hi' ))))
1154+
1155+ assert (
1156+ client .aio .interactions .calls [0 ]['system_instruction' ] == 'Async persona.'
1157+ )
1158+
1159+
1160+ def test_instruction_sent_on_chained_turn ():
1161+ prior = Event (
1162+ author = 'mgr' , interaction_id = 'int_prev' , environment_id = 'env_prev'
1163+ )
1164+ client = _RecordingClient ([[]])
1165+ agent = ManagedAgent (
1166+ name = 'mgr' ,
1167+ agent_id = 'agents/a' ,
1168+ instruction = 'Stay in character.' ,
1169+ api_client = client ,
1170+ )
1171+ ctx = _user_ctx ('again' , session_events = [prior ])
1172+
1173+ asyncio .run (_drain (agent ._run_async_impl (ctx )))
1174+
1175+ create_kwargs = client .aio .interactions .calls [0 ]
1176+ assert create_kwargs ['previous_interaction_id' ] == 'int_prev'
1177+ assert create_kwargs ['system_instruction' ] == 'Stay in character.'
1178+
1179+
1180+ def test_string_instruction_injects_session_state ():
1181+ from google .adk .agents .invocation_context import InvocationContext
1182+ from google .adk .sessions .in_memory_session_service import InMemorySessionService
1183+ from google .adk .sessions .session import Session
1184+
1185+ client = _RecordingClient ([[]])
1186+ agent = ManagedAgent (
1187+ name = 'mgr' ,
1188+ agent_id = 'agents/a' ,
1189+ instruction = 'Discuss {topic}.' ,
1190+ api_client = client ,
1191+ )
1192+ session = Session (
1193+ app_name = 'test' , user_id = 'user' , id = 's1' , state = {'topic' : 'volcanoes' }
1194+ )
1195+ ctx = InvocationContext (
1196+ invocation_id = 'inv1' ,
1197+ session = session ,
1198+ session_service = InMemorySessionService (),
1199+ user_content = genai_types .Content (
1200+ role = 'user' , parts = [genai_types .Part (text = 'hi' )]
1201+ ),
1202+ )
1203+
1204+ asyncio .run (_drain (agent ._run_async_impl (ctx )))
1205+
1206+ assert (
1207+ client .aio .interactions .calls [0 ]['system_instruction' ]
1208+ == 'Discuss volcanoes.'
1209+ )
0 commit comments