Skip to content

Conversation

@droideronline
Copy link
Contributor

Summary

This PR fixes a bug where ClaudeAgent.run_stream() silently ignored API errors and AssistantMessage responses, returning empty results instead of raising exceptions.

Changes

_agent.py

  • Import AssistantMessage and TextBlock from claude_agent_sdk
  • Handle AssistantMessage.error by raising ServiceException with descriptive error messages mapped from error types (authentication_failed, billing_error, rate_limit, invalid_request, server_error, unknown)
  • Check ResultMessage.is_error and raise ServiceException with the error details from ResultMessage.result

test_claude_agent.py

  • Added test_run_stream_raises_on_assistant_message_error - verifies ServiceException is raised when AssistantMessage contains an error
  • Added test_run_stream_raises_on_result_message_error - verifies ServiceException is raised when ResultMessage.is_error is True

Testing

All 46 tests pass:

pytest tests/test_claude_agent.py -v
============================== 46 passed in 0.28s ==============================

Fixes #3652

Copilot AI review requested due to automatic review settings February 3, 2026 20:24
@github-actions github-actions bot changed the title fix(claude): handle API errors in run_stream() method Python: fix(claude): handle API errors in run_stream() method Feb 3, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical bug where ClaudeAgent.run_stream() silently ignored API errors and returned empty results instead of raising exceptions. The fix adds proper error handling for both AssistantMessage and ResultMessage error conditions.

Changes:

  • Added error handling for AssistantMessage.error field with descriptive error messages mapped from error types
  • Added error checking for ResultMessage.is_error flag to surface API errors as exceptions
  • Added comprehensive test coverage for both error scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/claude/agent_framework_claude/_agent.py Added imports for AssistantMessage and TextBlock; implemented error handling logic in run_stream() to raise ServiceException for API errors
python/packages/claude/tests/test_claude_agent.py Added two new test cases verifying exception raising for AssistantMessage errors and ResultMessage errors

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Feb 4, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
TOTAL16255191888% 
report-only-changed-files is enabled. No files were changed during this commit :)

Python Unit Test Overview

Tests Skipped Failures Errors Time
3931 221 💤 0 ❌ 0 🔥 1m 5s ⏱️

@moonbox3
Copy link
Contributor

moonbox3 commented Feb 4, 2026

I've got a fix in #3661 that allows for the failing unit test to pass.

- 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 microsoft#3652
Address PR review feedback - add null check for message.content to prevent
potential AttributeError if content is None.
@droideronline droideronline force-pushed the fix/claude-agent-run-stream-error-handling branch from a22339a to a148b32 Compare February 4, 2026 05:39
@droideronline
Copy link
Contributor Author

@moonbox3 - I cherry picked your changes, ready to merge.

@droideronline
Copy link
Contributor Author

@moonbox3 - kindly have a look at this as well

@moonbox3
Copy link
Contributor

moonbox3 commented Feb 4, 2026

This one needs a refresh on the uv.lock file: uv sync --dev then uv run poe install and commit and push please.

@droideronline
Copy link
Contributor Author

droideronline commented Feb 4, 2026

This one needs a refresh on the uv.lock file: uv sync --dev then uv run poe install and commit and push please.

pre-commit hooks were also failing. its fixed now.

@moonbox3
Copy link
Contributor

moonbox3 commented Feb 4, 2026

We'll get this in, but before that, we're trying to get #3379 in, which may cause some merge conflicts here. Please be aware.

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.

Python: [Bug]: ClaudeAgent.run_stream() silently ignores API errors and AssistantMessage responses

4 participants