1313)
1414
1515try :
16- from anthropic .resources import Messages , AsyncMessages
16+ from anthropic .resources import AsyncMessages , Messages
1717
1818 if TYPE_CHECKING :
1919 from anthropic .types import MessageStreamEvent
2020except ImportError :
2121 raise DidNotEnable ("Anthropic not installed" )
2222
2323if TYPE_CHECKING :
24- from typing import Any , Iterator , AsyncIterator
24+ from typing import Any , AsyncIterator , Iterator
2525 from sentry_sdk .tracing import Span
2626
2727
@@ -91,6 +91,7 @@ def _get_responses(content):
9191
9292
9393def _collect_ai_data (event , input_tokens , output_tokens , content_blocks ):
94+ # type: (MessageStreamEvent, int, int, list[str]) -> None
9495 """
9596 Count token usage and collect content blocks from the AI streaming response.
9697 """
@@ -112,8 +113,9 @@ def _collect_ai_data(event, input_tokens, output_tokens, content_blocks):
112113
113114
114115def _add_ai_data_to_span (
115- span , integration , content_blocks , input_tokens , output_tokens
116+ span , integration , input_tokens , output_tokens , content_blocks
116117):
118+ # type: (Span, AnthropicIntegration, int, int, list[str]) -> None
117119 """
118120 Add token usage and content blocks from the AI streaming response to the span.
119121 """
@@ -182,31 +184,31 @@ def new_iterator():
182184 # type: () -> Iterator[MessageStreamEvent]
183185 input_tokens = 0
184186 output_tokens = 0
185- content_blocks = []
187+ content_blocks = [] # type: list[str]
186188
187189 for event in old_iterator :
188190 _collect_ai_data (event , input_tokens , output_tokens , content_blocks )
189191 if event .type != "message_stop" :
190192 yield event
191193
192194 _add_ai_data_to_span (
193- span , integration , content_blocks , input_tokens , output_tokens
195+ span , integration , input_tokens , output_tokens , content_blocks
194196 )
195197 span .__exit__ (None , None , None )
196198
197199 async def new_iterator_async ():
198200 # type: () -> AsyncIterator[MessageStreamEvent]
199201 input_tokens = 0
200202 output_tokens = 0
201- content_blocks = []
203+ content_blocks = [] # type: list[str]
202204
203205 async for event in old_iterator :
204206 _collect_ai_data (event , input_tokens , output_tokens , content_blocks )
205207 if event .type != "message_stop" :
206208 yield event
207209
208210 _add_ai_data_to_span (
209- span , integration , content_blocks , input_tokens , output_tokens
211+ span , integration , input_tokens , output_tokens , content_blocks
210212 )
211213 span .__exit__ (None , None , None )
212214
0 commit comments