Skip to content

feat: add AgentResult.text, AgentResult.__repr__, and Agent.clear()#1942

Open
Exploreunive wants to merge 1 commit intostrands-agents:mainfrom
Exploreunive:feature/agent-result-text-and-clear
Open

feat: add AgentResult.text, AgentResult.__repr__, and Agent.clear()#1942
Exploreunive wants to merge 1 commit intostrands-agents:mainfrom
Exploreunive:feature/agent-result-text-and-clear

Conversation

@Exploreunive
Copy link

Summary

Add three convenience features that improve the developer experience when working with the Strands SDK:

  1. AgentResult.text property — A discoverable, Pythonic way to extract text content from agent results. Equivalent to str(result) but more intuitive.

  2. AgentResult.__repr__ — Debug-friendly representation showing stop_reason and a truncated text preview, making agent output much easier to inspect in logs and REPLs.

  3. Agent.clear() — A single method to reset conversation history and event loop metrics, useful for multi-turn testing scenarios and conversation restarts.

Changes

  • src/strands/agent/agent_result.py: Added text property and __repr__ method
  • src/strands/agent/agent.py: Added clear() method
  • tests/strands/agent/test_agent_result.py: 6 new tests for text and __repr__
  • tests/strands/agent/test_agent.py: 2 new tests for clear()

Testing

All 27 tests pass (25 existing + 2 new for clear(), 6 new for text/repr on agent_result).

…clear()

- AgentResult.text: convenience property for extracting text content,
  equivalent to str(result) but more discoverable and Pythonic
- AgentResult.__repr__: debug-friendly representation showing stop_reason
  and a truncated text preview
- Agent.clear(): reset conversation history and event loop metrics in one
  call, useful for multi-turn testing and conversation restart scenarios
Exploreunive pushed a commit to Exploreunive/sdk-python that referenced this pull request Mar 21, 2026
Addresses review feedback from strands-agents#1943:

1. **Clean split**: Only isError-related changes, no unrelated commits from
   AgentResult.text (PR strands-agents#1942) or other work.

2. **isError propagation**: MCPToolResult now exposes isError field:
   - True when MCP server reports CallToolResult.isError (application error)
   - True when a Python exception occurs during tool execution (protocol error)
   - False/absent on success
   Updated docstring clarifies both error sources.

3. **Exception propagation** (maintainer feedback from strands-agents#1670):
   MCPAgentTool.stream() now passes the original exception via
   ToolResultEvent(result, exception=e), matching the pattern used by
   decorated tools. Added _invoke_tool() helper that calls the MCP client
   internals directly to capture exceptions.

Files changed:
- mcp_types.py: Added isError: NotRequired[bool], updated docstring
- mcp_client.py: Propagated isError in _handle_tool_result and
  _handle_tool_execution_error
- mcp_agent_tool.py: Added _invoke_tool() for exception capture,
  stream() yields ToolResultEvent(result, exception=...)
- Tests: Added isError assertions, exception propagation tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant