Python: fix(claude): handle API errors in run_stream() method (#3653)

* fix(claude): handle API errors in run_stream() method

- Import AssistantMessage and TextBlock from claude_agent_sdk
- Check AssistantMessage.error and raise ServiceException with descriptive message
- Check ResultMessage.is_error and raise ServiceException with error details
- Add tests for error handling in run_stream()

Fixes #3652

* fix: add defensive check for message.content before iterating

Address PR review feedback - add null check for message.content to prevent
potential AttributeError if content is None.

* chore: refresh uv.lock

* chore: fix import sorting

* chore: refresh uv.lock
This commit is contained in:
Dineshsuriya D
2026-02-05 11:10:32 +05:30
committed by GitHub
Unverified
parent 0daa7700c6
commit 9e51e2f0bc
3 changed files with 100 additions and 18 deletions
@@ -379,6 +379,61 @@ class TestClaudeAgentRunStream:
assert updates[0].text == "Streaming "
assert updates[1].text == "response"
async def test_run_stream_raises_on_assistant_message_error(self) -> None:
"""Test run_stream raises ServiceException when AssistantMessage has an error."""
from agent_framework.exceptions import ServiceException
from claude_agent_sdk import AssistantMessage, ResultMessage, TextBlock
messages = [
AssistantMessage(
content=[TextBlock(text="Error details from API")],
model="claude-sonnet",
error="invalid_request",
),
ResultMessage(
subtype="success",
duration_ms=100,
duration_api_ms=50,
is_error=False,
num_turns=1,
session_id="error-session",
),
]
mock_client = self._create_mock_client(messages)
with patch("agent_framework_claude._agent.ClaudeSDKClient", return_value=mock_client):
agent = ClaudeAgent()
with pytest.raises(ServiceException) as exc_info:
async for _ in agent.run_stream("Hello"):
pass
assert "Invalid request to Claude API" in str(exc_info.value)
assert "Error details from API" in str(exc_info.value)
async def test_run_stream_raises_on_result_message_error(self) -> None:
"""Test run_stream raises ServiceException when ResultMessage.is_error is True."""
from agent_framework.exceptions import ServiceException
from claude_agent_sdk import ResultMessage
messages = [
ResultMessage(
subtype="error",
duration_ms=100,
duration_api_ms=50,
is_error=True,
num_turns=0,
session_id="error-session",
result="Model 'claude-sonnet-4.5' not found",
),
]
mock_client = self._create_mock_client(messages)
with patch("agent_framework_claude._agent.ClaudeSDKClient", return_value=mock_client):
agent = ClaudeAgent()
with pytest.raises(ServiceException) as exc_info:
async for _ in agent.run_stream("Hello"):
pass
assert "Model 'claude-sonnet-4.5' not found" in str(exc_info.value)
# region Test ClaudeAgent Session Management