feat: Raise API errors as exceptions instead of text messages#526
Draft
wingding12 wants to merge 1 commit intoanthropics:mainfrom
Draft
feat: Raise API errors as exceptions instead of text messages#526wingding12 wants to merge 1 commit intoanthropics:mainfrom
wingding12 wants to merge 1 commit intoanthropics:mainfrom
Conversation
…sages
API errors from the Anthropic API (400, 401, 429, 529, etc.) were being
returned as text messages in AssistantMessage.content instead of being
raised as exceptions. This made it impossible to programmatically handle
API errors with try/except.
Changes:
1. Added new exception hierarchy in _errors.py:
- APIError (base class with error_type and model attributes)
- AuthenticationError (authentication_failed)
- BillingError (billing_error)
- RateLimitError (rate_limit)
- InvalidRequestError (invalid_request)
- ServerError (server_error)
2. Modified message_parser.py to:
- Read error field from correct location (top level, not nested)
Fixes anthropics#505
- Raise appropriate exception when error field is present
Fixes anthropics#472
3. Added comprehensive tests (27 new tests) covering:
- Exception class attributes and inheritance
- Error type to exception class mapping
- Message parser behavior for each error type
- Integration patterns (retry logic, catch-all)
Fixes anthropics#472
Fixes anthropics#505
a453834 to
38c63a0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses issue #472 by converting API error responses into proper Python exceptions that can be caught and handled programmatically.
Problem:
Previously, API errors (rate limits, authentication failures, invalid requests, server errors) were returned as text content within
AssistantMessageobjects. This made it difficult to distinguish them from normal responses and handle them appropriately in application code.Solution:
_errors.py:APIError(base class for all API errors)AuthenticationError(401 responses)BillingError(billing issues)RateLimitError(429 responses)InvalidRequestError(400 responses)ServerError(500/529 responses)_internal/client.pyto detectAssistantMessageobjects with theerrorfield set and raise the corresponding exception__init__.pyfor public useChanges:
src/claude_agent_sdk/_errors.py- Added 6 new exception types with proper inheritance and attributessrc/claude_agent_sdk/_internal/client.py- Added error detection and exception raising logicsrc/claude_agent_sdk/__init__.py- Exported new exception typestests/test_errors.py- Added tests for new exception typestests/test_client.py- Added tests for error handling in the clientExample Usage
Users can now handle API errors idiomatically:
Test Plan
Fixes #472