ref(anthropic): Factor out streamed result handling#5563
ref(anthropic): Factor out streamed result handling#5563alexander-alderman-webb wants to merge 3 commits intomasterfrom
Conversation
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛Openai
Other
Documentation 📚
Internal Changes 🔧Agents
Anthropic
Openai Agents
Other
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ 13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 7.46s All tests are passing successfully. ❌ Patch coverage is 0.00%. Project has 13735 uncovered lines. Files with missing lines (180)
Generated by Codecov Action |
| is_streaming_response = kwargs.get("stream", False) | ||
| if is_streaming_response: | ||
| _patch_streaming_response_iterator(result, span, integration) | ||
| return result |
There was a problem hiding this comment.
Streaming response handling no longer protected by capture_internal_exceptions
The streaming path now checks kwargs.get("stream", False) and calls _patch_streaming_response_iterator before entering the capture_internal_exceptions() block. Previously, the streaming detection (hasattr(result, "_iterator")) was inside the exception handling block. If stream=True is passed but the result unexpectedly lacks _iterator (e.g., API behavior change, error responses), an AttributeError will propagate instead of being gracefully handled, potentially breaking the instrumented code path.
Verification
Read anthropic.py lines 510-520 to verify the new streaming check is outside capture_internal_exceptions(). The old code had elif hasattr(result, "_iterator"): inside the with capture_internal_exceptions(): block per the diff context. The new code at lines 513-516 executes before entering the block at line 518.
Suggested fix: Wrap the streaming response handling in capture_internal_exceptions to maintain defensive error handling
| is_streaming_response = kwargs.get("stream", False) | |
| if is_streaming_response: | |
| _patch_streaming_response_iterator(result, span, integration) | |
| return result | |
| with capture_internal_exceptions(): | |
| is_streaming_response = kwargs.get("stream", False) | |
| if is_streaming_response: | |
| _patch_streaming_response_iterator(result, span, integration) | |
| return result |
Identified by Warden code-review · TGC-FD8
Description
Issues
Reminders
tox -e linters.feat:,fix:,ref:,meta:)