Skip to content

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

Merged
Evan Mattson (moonbox3) merged 5 commits into
microsoft:mainfrom
droideronline:fix/claude-agent-run-stream-error-handling
Feb 5, 2026
Merged

Python: fix(claude): handle API errors in run_stream() method#3653
Evan Mattson (moonbox3) merged 5 commits into
microsoft:mainfrom
droideronline:fix/claude-agent-run-stream-error-handling

Conversation

@droideronline

Copy link
Copy Markdown
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
@markwallace-microsoft Mark Wallace (markwallace-microsoft) added the python Usage: [Issues, PRs], Target: Python label Feb 3, 2026
@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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread python/packages/claude/agent_framework_claude/_agent.py Outdated
@markwallace-microsoft

Mark Wallace (markwallace-microsoft) commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

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

Python Unit Test Overview

Tests Skipped Failures Errors Time
3991 221 💤 0 ❌ 0 🔥 1m 7s ⏱️

@moonbox3

Copy link
Copy Markdown
Contributor

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

@droideronline

Copy link
Copy Markdown
Contributor Author

Evan Mattson (@moonbox3) - I cherry picked your changes, ready to merge.

@droideronline

Copy link
Copy Markdown
Contributor Author

Evan Mattson (@moonbox3) - kindly have a look at this as well

@moonbox3

Evan Mattson (moonbox3) commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

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

@droideronline

Dineshsuriya D (droideronline) commented Feb 4, 2026

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor

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.

@moonbox3

Copy link
Copy Markdown
Contributor

Looks like there's one more uv.lock conflict, Dineshsuriya D (@droideronline). Once addressed, I'll get this PR in.

- 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
Dineshsuriya D (droideronline) force-pushed the fix/claude-agent-run-stream-error-handling branch from fbea4c0 to 0a71012 Compare February 5, 2026 05:28
@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Feb 5, 2026
Merged via the queue into microsoft:main with commit 9e51e2f Feb 5, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

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

5 participants