@@ -367,6 +367,9 @@ def __call__(self, prompt: Union[str, list[ContentBlock]], **kwargs: Any) -> Age
367
367
- message: The final message from the model
368
368
- metrics: Performance metrics from the event loop
369
369
- state: The final state of the event loop
370
+
371
+ Raises:
372
+ ValueError: If prompt is None.
370
373
"""
371
374
372
375
def execute () -> AgentResult :
@@ -393,6 +396,9 @@ async def invoke_async(self, prompt: Union[str, list[ContentBlock]], **kwargs: A
393
396
- message: The final message from the model
394
397
- metrics: Performance metrics from the event loop
395
398
- state: The final state of the event loop
399
+
400
+ Raises:
401
+ ValueError: If prompt is None.
396
402
"""
397
403
events = self .stream_async (prompt , ** kwargs )
398
404
async for event in events :
@@ -452,8 +458,8 @@ async def structured_output_async(
452
458
453
459
# add the prompt as the last message
454
460
if prompt :
455
- content : list [ ContentBlock ] = [{ "text" : prompt }] if isinstance (prompt , str ) else prompt
456
- self ._append_message ({ "role" : "user" , "content" : content } )
461
+ message = self . _standardize_prompt (prompt )
462
+ self ._append_message (message )
457
463
458
464
events = self .model .structured_output (output_model , self .messages , system_prompt = self .system_prompt )
459
465
async for event in events :
@@ -487,6 +493,7 @@ async def stream_async(self, prompt: Union[str, list[ContentBlock]], **kwargs: A
487
493
- And other event data provided by the callback handler
488
494
489
495
Raises:
496
+ ValueError: If prompt is None.
490
497
Exception: Any exceptions from the agent invocation will be propagated to the caller.
491
498
492
499
Example:
@@ -498,8 +505,7 @@ async def stream_async(self, prompt: Union[str, list[ContentBlock]], **kwargs: A
498
505
"""
499
506
callback_handler = kwargs .get ("callback_handler" , self .callback_handler )
500
507
501
- content : list [ContentBlock ] = [{"text" : prompt }] if isinstance (prompt , str ) else prompt
502
- message : Message = {"role" : "user" , "content" : content }
508
+ message = self ._standardize_prompt (prompt )
503
509
504
510
self .trace_span = self ._start_agent_trace_span (message )
505
511
with trace_api .use_span (self .trace_span ):
@@ -561,6 +567,15 @@ async def _run_loop(
561
567
self .conversation_manager .apply_management (self )
562
568
self .hooks .invoke_callbacks (AfterInvocationEvent (agent = self ))
563
569
570
+ def _standardize_prompt (self , prompt : Union [str , list [ContentBlock ]]) -> Message :
571
+ """Convert the prompt into a Message, validating it along the way."""
572
+ if prompt is None :
573
+ raise ValueError ("User prompt must not be None" )
574
+
575
+ content : list [ContentBlock ] = [{"text" : prompt }] if isinstance (prompt , str ) else prompt
576
+ message : Message = {"role" : "user" , "content" : content }
577
+ return message
578
+
564
579
async def _execute_event_loop_cycle (self , invocation_state : dict [str , Any ]) -> AsyncGenerator [dict [str , Any ], None ]:
565
580
"""Execute the event loop cycle with retry logic for context window limits.
566
581
0 commit comments