@@ -393,21 +393,33 @@ async def run_agent(context: RunContextWrapper, input: str) -> str:
393
393
return run_agent
394
394
395
395
async def get_system_prompt (self , run_context : RunContextWrapper [TContext ]) -> str | None :
396
- """Get the system prompt for the agent."""
397
396
if isinstance (self .instructions , str ):
398
397
return self .instructions
399
398
elif callable (self .instructions ):
399
+ # Inspect the signature of the instructions function
400
+ sig = inspect .signature (self .instructions )
401
+ params = list (sig .parameters .values ())
402
+
403
+ # Enforce exactly 2 parameters
404
+ if len (params ) != 2 :
405
+ raise TypeError (
406
+ f"'instructions' callable must accept exactly 2 arguments (context, agent), "
407
+ f"but got { len (params )} : { [p .name for p in params ]} "
408
+ )
409
+
410
+ # Call the instructions function properly
400
411
if inspect .iscoroutinefunction (self .instructions ):
401
412
return await cast (Awaitable [str ], self .instructions (run_context , self ))
402
413
else :
403
414
return cast (str , self .instructions (run_context , self ))
415
+
404
416
elif self .instructions is not None :
405
- logger .error (f"Instructions must be a string or a function, got { self .instructions } " )
417
+ logger .error (f"Instructions must be a string or a callable function, got { type ( self .instructions ). __name__ } " )
406
418
407
419
return None
408
420
409
421
async def get_prompt (
410
422
self , run_context : RunContextWrapper [TContext ]
411
423
) -> ResponsePromptParam | None :
412
424
"""Get the prompt for the agent."""
413
- return await PromptUtil .to_model_input (self .prompt , run_context , self )
425
+ return await PromptUtil .to_model_input (self .prompt , run_context , self )
0 commit comments