@@ -367,6 +367,9 @@ def __call__(self, prompt: Union[str, list[ContentBlock]], **kwargs: Any) -> Age
367367 - message: The final message from the model
368368 - metrics: Performance metrics from the event loop
369369 - state: The final state of the event loop
370+
371+ Raises:
372+ ValueError: If prompt is None.
370373 """
371374
372375 def execute () -> AgentResult :
@@ -393,6 +396,9 @@ async def invoke_async(self, prompt: Union[str, list[ContentBlock]], **kwargs: A
393396 - message: The final message from the model
394397 - metrics: Performance metrics from the event loop
395398 - state: The final state of the event loop
399+
400+ Raises:
401+ ValueError: If prompt is None.
396402 """
397403 events = self .stream_async (prompt , ** kwargs )
398404 async for event in events :
@@ -452,8 +458,7 @@ async def structured_output_async(
452458
453459 # Create temporary messages array if prompt is provided
454460 if prompt :
455- content : list [ContentBlock ] = [{"text" : prompt }] if isinstance (prompt , str ) else prompt
456- temp_messages = self .messages + [{"role" : "user" , "content" : content }]
461+ temp_messages = self .messages + self ._standardize_prompt (prompt )
457462 else :
458463 temp_messages = self .messages
459464
@@ -489,6 +494,7 @@ async def stream_async(self, prompt: Union[str, list[ContentBlock]], **kwargs: A
489494 - And other event data provided by the callback handler
490495
491496 Raises:
497+ ValueError: If prompt is None.
492498 Exception: Any exceptions from the agent invocation will be propagated to the caller.
493499
494500 Example:
@@ -500,8 +506,7 @@ async def stream_async(self, prompt: Union[str, list[ContentBlock]], **kwargs: A
500506 """
501507 callback_handler = kwargs .get ("callback_handler" , self .callback_handler )
502508
503- content : list [ContentBlock ] = [{"text" : prompt }] if isinstance (prompt , str ) else prompt
504- message : Message = {"role" : "user" , "content" : content }
509+ message = self ._standardize_prompt (prompt )
505510
506511 self .trace_span = self ._start_agent_trace_span (message )
507512 with trace_api .use_span (self .trace_span ):
@@ -563,6 +568,15 @@ async def _run_loop(
563568 self .conversation_manager .apply_management (self )
564569 self .hooks .invoke_callbacks (AfterInvocationEvent (agent = self ))
565570
571+ def _standardize_prompt (self , prompt : Union [str , list [ContentBlock ]]) -> Message :
572+ """Convert the prompt into a Message, validating it along the way."""
573+ if prompt is None :
574+ raise ValueError ("User prompt must not be None" )
575+
576+ content : list [ContentBlock ] = [{"text" : prompt }] if isinstance (prompt , str ) else prompt
577+ message : Message = {"role" : "user" , "content" : content }
578+ return message
579+
566580 async def _execute_event_loop_cycle (self , invocation_state : dict [str , Any ]) -> AsyncGenerator [dict [str , Any ], None ]:
567581 """Execute the event loop cycle with retry logic for context window limits.
568582
0 commit comments