fix: preserve finish_reason in streaming final chunk#26
Conversation
- Track _last_finish_reason in AnthropicTextGenerationStream - Use _last_finish_reason instead of None in final empty chunk - Export TextGenerationChunk and TextGenerationFinishReason from package
PR Review: Fix preserve finish_reason in streaming final chunkOverviewThis PR addresses a bug where the ✅ Code Quality & Best PracticesStrengths:
Minor observations:
🐛 Potential IssuesNo bugs identified. The implementation is sound:
⚡ Performance ConsiderationsNo performance concerns:
🔒 Security ConcernsNo security issues identified:
🧪 Test CoverageGap identified: While the fix is correct, there are no tests specifically validating this behavior:
Recommendation: async def test_finish_reason_preserved_in_final_chunk():
"""Verify finish_reason appears in final message_stop chunk."""
events = [
{"type": "message_start", "message": {"content": []}},
{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello"}},
{"type": "message_delta", "delta": {"stop_reason": "end_turn"}, "usage": {...}},
{"type": "message_stop"}, # Final chunk should have finish_reason
]
# Assert final chunk has finish_reason with reason="end_turn"This would prevent regression and document expected behavior. 📦 Public API ChangesPositive impact:
SummaryVerdict: ✅ Approve with minor suggestion The code fix is correct and well-implemented. The only improvement would be adding tests to prevent future regressions. The public API exports are a welcome addition for library consumers. Suggested next steps:
Great work on identifying and fixing this edge case! 🎯 |
Preserves finish_reason in the final empty chunk of streaming responses.
Changes:
_last_finish_reasoninAnthropicTextGenerationStream_last_finish_reasoninstead ofNonein final empty chunkTextGenerationChunkandTextGenerationFinishReasonfrom packageWhy:
Previously, the final chunk always had
finish_reason=Noneeven when the stream completed with a stop reason. This fix ensures the finish_reason is preserved and available in the final chunk.