Helper function to run agent with different methods.
(runner_method: str, agent, input_data, **kwargs)
| 24 | |
| 25 | |
| 26 | async def run_agent_async(runner_method: str, agent, input_data, **kwargs): |
| 27 | """Helper function to run agent with different methods.""" |
| 28 | if runner_method == "run": |
| 29 | return await Runner.run(agent, input_data, **kwargs) |
| 30 | elif runner_method == "run_sync": |
| 31 | # For run_sync, we need to run it in a thread with its own event loop |
| 32 | return await asyncio.to_thread(_run_sync_wrapper, agent, input_data, **kwargs) |
| 33 | elif runner_method == "run_streamed": |
| 34 | result = Runner.run_streamed(agent, input_data, **kwargs) |
| 35 | # For streaming, we first try to get at least one event to trigger any early exceptions |
| 36 | # If there's an exception in setup (like memory validation), it will be raised here |
| 37 | try: |
| 38 | first_event = None |
| 39 | async for event in result.stream_events(): |
| 40 | if first_event is None: |
| 41 | first_event = event |
| 42 | # Continue consuming all events |
| 43 | pass |
| 44 | except Exception: |
| 45 | # If an exception occurs during streaming, we let it propagate up |
| 46 | raise |
| 47 | return result |
| 48 | else: |
| 49 | raise ValueError(f"Unknown runner method: {runner_method}") |
| 50 | |
| 51 | |
| 52 | # Parametrized tests for different runner methods |
no test coverage detected