Skip to content

Conversation

@mshsheikh
Copy link
Contributor

Problem:
When running agents in parallel using asyncio.gather, any unhandled exception in one task (e.g., network error, agent crash) would immediately propagate and terminate the entire workflow, discarding all partial results.

Example of Failure:

res_1, res_2, res_3 = await asyncio.gather(
    Runner.run(spanish_agent, msg),  # Fails with Exception
    Runner.run(spanish_agent, msg),
    Runner.run(spanish_agent, msg),
)
# All results are lost due to a single failure.

Changes:

  • Added return_exceptions=True to asyncio.gather() to handle failures gracefully:
  res_1, res_2, res_3 = await asyncio.gather(
      Runner.run(spanish_agent, msg),
      Runner.run(spanish_agent, msg),
      Runner.run(spanish_agent, msg),
      return_exceptions=True,
  )
  • Exceptions are now captured as Exception objects in the results instead of being raised immediately.

Benefits:

  • Ensures robustness: Partial results are preserved even if one or more tasks fail.
  • Allows explicit error handling for failed tasks (e.g., logging, fallback logic).
  • Prevents unnecessary re-runs of successful tasks when one task fails.

Example Output Handling:

outputs = [
    ItemHelpers.text_message_outputs(res_1.new_items) if not isinstance(res_1, Exception) else None,
    # ...similar for res_2, res_3
]

**Problem**:
When running agents in parallel using `asyncio.gather`, any unhandled exception in one task (e.g., network error, agent crash) would immediately propagate and terminate the entire workflow, discarding all partial results.

**Example of Failure**:
```python
res_1, res_2, res_3 = await asyncio.gather(
    Runner.run(spanish_agent, msg),  # Fails with Exception
    Runner.run(spanish_agent, msg),
    Runner.run(spanish_agent, msg),
)
# All results are lost due to a single failure.
```

**Changes**:
- Added `return_exceptions=True` to `asyncio.gather()` to handle failures gracefully:
```python
  res_1, res_2, res_3 = await asyncio.gather(
      Runner.run(spanish_agent, msg),
      Runner.run(spanish_agent, msg),
      Runner.run(spanish_agent, msg),
      return_exceptions=True,
  )
```
- Exceptions are now captured as `Exception` objects in the results instead of being raised immediately.

**Benefits**:
- Ensures robustness: Partial results are preserved even if one or more tasks fail.
- Allows explicit error handling for failed tasks (e.g., logging, fallback logic).
- Prevents unnecessary re-runs of successful tasks when one task fails.

**Example Output Handling**:  
```python
outputs = [
    ItemHelpers.text_message_outputs(res_1.new_items) if not isinstance(res_1, Exception) else None,
    # ...similar for res_2, res_3
]
```
@mshsheikh mshsheikh closed this Jul 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant