1313import json
1414import logging
1515import random
16+ import warnings
1617from concurrent .futures import ThreadPoolExecutor
1718from typing import (
1819 Any ,
@@ -374,7 +375,9 @@ def tool_names(self) -> list[str]:
374375 all_tools = self .tool_registry .get_all_tools_config ()
375376 return list (all_tools .keys ())
376377
377- def __call__ (self , prompt : AgentInput = None , ** kwargs : Any ) -> AgentResult :
378+ def __call__ (
379+ self , prompt : AgentInput = None , * , invocation_state : dict [str , Any ] | None = None , ** kwargs : Any
380+ ) -> AgentResult :
378381 """Process a natural language prompt through the agent's event loop.
379382
380383 This method implements the conversational interface with multiple input patterns:
@@ -389,7 +392,8 @@ def __call__(self, prompt: AgentInput = None, **kwargs: Any) -> AgentResult:
389392 - list[ContentBlock]: Multi-modal content blocks
390393 - list[Message]: Complete messages with roles
391394 - None: Use existing conversation history
392- **kwargs: Additional parameters to pass through the event loop.
395+ invocation_state: Additional parameters to pass through the event loop.
396+ **kwargs: Additional parameters to pass through the event loop.[Deprecating]
393397
394398 Returns:
395399 Result object containing:
@@ -401,13 +405,15 @@ def __call__(self, prompt: AgentInput = None, **kwargs: Any) -> AgentResult:
401405 """
402406
403407 def execute () -> AgentResult :
404- return asyncio .run (self .invoke_async (prompt , ** kwargs ))
408+ return asyncio .run (self .invoke_async (prompt , invocation_state = invocation_state , ** kwargs ))
405409
406410 with ThreadPoolExecutor () as executor :
407411 future = executor .submit (execute )
408412 return future .result ()
409413
410- async def invoke_async (self , prompt : AgentInput = None , ** kwargs : Any ) -> AgentResult :
414+ async def invoke_async (
415+ self , prompt : AgentInput = None , * , invocation_state : dict [str , Any ] | None = None , ** kwargs : Any
416+ ) -> AgentResult :
411417 """Process a natural language prompt through the agent's event loop.
412418
413419 This method implements the conversational interface with multiple input patterns:
@@ -422,7 +428,8 @@ async def invoke_async(self, prompt: AgentInput = None, **kwargs: Any) -> AgentR
422428 - list[ContentBlock]: Multi-modal content blocks
423429 - list[Message]: Complete messages with roles
424430 - None: Use existing conversation history
425- **kwargs: Additional parameters to pass through the event loop.
431+ invocation_state: Additional parameters to pass through the event loop.
432+ **kwargs: Additional parameters to pass through the event loop.[Deprecating]
426433
427434 Returns:
428435 Result: object containing:
@@ -432,7 +439,7 @@ async def invoke_async(self, prompt: AgentInput = None, **kwargs: Any) -> AgentR
432439 - metrics: Performance metrics from the event loop
433440 - state: The final state of the event loop
434441 """
435- events = self .stream_async (prompt , ** kwargs )
442+ events = self .stream_async (prompt , invocation_state = invocation_state , ** kwargs )
436443 async for event in events :
437444 _ = event
438445
@@ -528,9 +535,7 @@ async def structured_output_async(self, output_model: Type[T], prompt: AgentInpu
528535 self .hooks .invoke_callbacks (AfterInvocationEvent (agent = self ))
529536
530537 async def stream_async (
531- self ,
532- prompt : AgentInput = None ,
533- ** kwargs : Any ,
538+ self , prompt : AgentInput = None , * , invocation_state : dict [str , Any ] | None = None , ** kwargs : Any
534539 ) -> AsyncIterator [Any ]:
535540 """Process a natural language prompt and yield events as an async iterator.
536541
@@ -546,7 +551,8 @@ async def stream_async(
546551 - list[ContentBlock]: Multi-modal content blocks
547552 - list[Message]: Complete messages with roles
548553 - None: Use existing conversation history
549- **kwargs: Additional parameters to pass to the event loop.
554+ invocation_state: Additional parameters to pass through the event loop.
555+ **kwargs: Additional parameters to pass to the event loop.[Deprecating]
550556
551557 Yields:
552558 An async iterator that yields events. Each event is a dictionary containing
@@ -567,7 +573,19 @@ async def stream_async(
567573 yield event["data"]
568574 ```
569575 """
570- callback_handler = kwargs .get ("callback_handler" , self .callback_handler )
576+ merged_state = {}
577+ if kwargs :
578+ warnings .warn ("`**kwargs` parameter is deprecating, use `invocation_state` instead." , stacklevel = 2 )
579+ merged_state .update (kwargs )
580+ if invocation_state is not None :
581+ merged_state ["invocation_state" ] = invocation_state
582+ else :
583+ if invocation_state is not None :
584+ merged_state = invocation_state
585+
586+ callback_handler = self .callback_handler
587+ if kwargs :
588+ callback_handler = kwargs .get ("callback_handler" , self .callback_handler )
571589
572590 # Process input and get message to add (if any)
573591 messages = self ._convert_prompt_to_messages (prompt )
@@ -576,10 +594,10 @@ async def stream_async(
576594
577595 with trace_api .use_span (self .trace_span ):
578596 try :
579- events = self ._run_loop (messages , invocation_state = kwargs )
597+ events = self ._run_loop (messages , invocation_state = merged_state )
580598
581599 async for event in events :
582- event .prepare (invocation_state = kwargs )
600+ event .prepare (invocation_state = merged_state )
583601
584602 if event .is_callback_event :
585603 as_dict = event .as_dict ()
0 commit comments