Skip to content

Fix ChainAgent raising raw string instead of Exception object#450

Open
gavksingh wants to merge 2 commits intoawslabs:mainfrom
gavksingh:fix/chain-agent-exception-handling
Open

Fix ChainAgent raising raw string instead of Exception object#450
gavksingh wants to merge 2 commits intoawslabs:mainfrom
gavksingh:fix/chain-agent-exception-handling

Conversation

@gavksingh
Copy link
Copy Markdown

@gavksingh gavksingh commented Mar 17, 2026

Summary

  • Fixed a bug in ChainAgent.process_request() where a raw f-string was raised instead of an Exception object on error
  • This caused a TypeError: exceptions must derive from BaseException instead of the intended error message being propagated
  • Added comprehensive unit tests for ChainAgent

Details

In python/src/agent_squad/agents/chain_agent.py line 65, the error handling code was:

raise f"Error processing request with agent {agent.name}:{str(error)}" from error

This raises a TypeError because strings are not valid exception types in Python. The fix wraps the f-string in Exception():

raise Exception(f"Error processing request with agent {agent.name}:{str(error)}") from error

Tests added

New file: python/src/tests/agents/test_chain_agent.py (9 tests)

  • test_chain_agent_requires_at_least_one_agent — validates empty agents list raises ValueError
  • test_chain_agent_single_agent_success — single agent returns expected ConversationMessage
  • test_chain_agent_multi_agent_chaining — output of agent N feeds into agent N+1
  • test_chain_agent_raises_exception_not_type_error — the bug fix test: verifies Exception is raised, not TypeError
  • test_chain_agent_error_propagation — error in 2nd agent references 2nd agent's name
  • test_chain_agent_default_response_on_empty_content — empty content triggers default response
  • test_chain_agent_streaming_last_agent — last agent async iterable passes through
  • test_chain_agent_streaming_intermediate_blocked — intermediate streaming returns default response
  • test_chain_agent_custom_default_output — custom default_output text propagates

Test plan

  • Verified the fix correctly wraps the error message in an Exception object
  • All 9 new tests pass (pytest -v)
  • Bug fix test confirms Exception is raised, not TypeError

Fixes #248

The process_request() method was raising a raw f-string on error,
which raises a TypeError instead of the intended exception message.
Wrap the f-string in Exception() so it properly raises with the
correct error message and traceback chain.

Fixes awslabs#248
Tests verify that ChainAgent properly raises Exception (not TypeError)
when an agent in the chain fails, along with chaining behavior,
streaming, default responses, and error propagation.
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.

Bug: Chain Agent Exception Handling

1 participant