@@ -93,37 +93,235 @@ def sentry_patched_iter(
9393 span .set_attribute if isinstance (span , StreamedSpan ) else span .set_data
9494 )
9595
96+ role : "Optional[str]" = None
97+ parts : "dict[int, Union[TextPart, ReasoningPart, ToolCallPart, ToolCallResponsePart, UriPart, BlobPart]]" = {}
98+
9699 try :
97100 for event in original_iter (self ):
98- if "metadata" not in event :
99- yield event
100- continue
101+ if "metadata" in event :
102+ usage = event ["metadata" ]["usage" ]
101103
102- usage = event ["metadata" ]["usage" ]
104+ input_tokens = usage ["inputTokens" ]
105+ set_on_span (
106+ SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS , usage ["outputTokens" ]
107+ )
108+ set_on_span (
109+ SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS , usage ["totalTokens" ]
110+ )
103111
104- input_tokens = usage ["inputTokens" ]
105- set_on_span (SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS , usage ["outputTokens" ])
106- set_on_span (SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS , usage ["totalTokens" ])
112+ cache_read_tokens = usage .get ("cacheReadInputTokens" )
113+ if cache_read_tokens is not None :
114+ input_tokens += cache_read_tokens
115+ set_on_span (
116+ SPANDATA .GEN_AI_USAGE_INPUT_TOKENS_CACHED , cache_read_tokens
117+ )
118+
119+ cache_write_tokens = usage .get ("cacheWriteInputTokens" )
120+ if cache_write_tokens is not None :
121+ input_tokens += cache_write_tokens
122+ set_on_span (
123+ SPANDATA .GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE ,
124+ cache_write_tokens ,
125+ )
107126
108- cache_read_tokens = usage .get ("cacheReadInputTokens" )
109- if cache_read_tokens is not None :
110- input_tokens += cache_read_tokens
111127 set_on_span (
112- SPANDATA .GEN_AI_USAGE_INPUT_TOKENS_CACHED , cache_read_tokens
128+ SPANDATA .GEN_AI_USAGE_INPUT_TOKENS ,
129+ input_tokens ,
113130 )
114131
115- cache_write_tokens = usage .get ("cacheWriteInputTokens" )
116- if cache_write_tokens is not None :
117- input_tokens += cache_write_tokens
132+ yield event
133+ continue
134+
135+ client = sentry_sdk .get_client ()
136+ if (
137+ not has_data_collection_enabled (client .options )
138+ or not client .options ["data_collection" ]["gen_ai" ]["outputs" ]
139+ ) and (
140+ has_data_collection_enabled (client .options )
141+ or not should_send_default_pii ()
142+ ):
143+ yield event
144+ continue
145+
146+ if "messageStart" in event :
147+ role = event ["messageStart" ]["role" ]
148+
149+ yield event
150+ continue
151+
152+ if "messageStop" in event and role is not None :
118153 set_on_span (
119- SPANDATA .GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE ,
120- cache_write_tokens ,
154+ SPANDATA .GEN_AI_OUTPUT_MESSAGES ,
155+ json .dumps (
156+ [
157+ {
158+ "role" : role ,
159+ "parts" : [parts [index ] for index in sorted (parts )],
160+ }
161+ ]
162+ ),
121163 )
122164
123- set_on_span (
124- SPANDATA .GEN_AI_USAGE_INPUT_TOKENS ,
125- input_tokens ,
126- )
165+ yield event
166+ continue
167+
168+ if (
169+ "contentBlockStart" in event
170+ and "toolUse" in event ["contentBlockStart" ]["start" ]
171+ ):
172+ parts [event ["contentBlockStart" ]["contentBlockIndex" ]] = {
173+ "type" : "tool_call" ,
174+ "name" : event ["contentBlockStart" ]["start" ]["toolUse" ]["name" ],
175+ }
176+
177+ yield event
178+ continue
179+
180+ if (
181+ "contentBlockStart" in event
182+ and "toolResult" in event ["contentBlockStart" ]["start" ]
183+ ):
184+ parts [event ["contentBlockStart" ]["contentBlockIndex" ]] = {
185+ "type" : "tool_call_response" ,
186+ }
187+
188+ yield event
189+ continue
190+
191+ if "contentBlockDelta" not in event :
192+ yield event
193+ continue
194+
195+ index = event ["contentBlockDelta" ]["contentBlockIndex" ]
196+ delta = event ["contentBlockDelta" ]["delta" ]
197+
198+ if "text" in delta and index not in parts :
199+ parts [index ] = {"type" : "text" , "content" : delta ["text" ]}
200+
201+ yield event
202+ continue
203+
204+ if "text" in delta :
205+ cast ("TextPart" , parts [index ])["content" ] += delta ["text" ]
206+
207+ yield event
208+ continue
209+
210+ if "toolUse" in delta and index not in parts :
211+ parts [index ] = {
212+ "type" : "tool_call" ,
213+ "arguments" : delta ["toolUse" ]["input" ],
214+ }
215+
216+ yield event
217+ continue
218+
219+ if "toolUse" in delta and "arguments" in parts [index ]:
220+ cast ("ToolCallPart" , parts [index ])["arguments" ] += delta ["toolUse" ][
221+ "input"
222+ ]
223+
224+ yield event
225+ continue
226+
227+ if "toolUse" in delta :
228+ cast ("ToolCallPart" , parts [index ])["arguments" ] = delta ["toolUse" ][
229+ "input"
230+ ]
231+
232+ yield event
233+ continue
234+
235+ if "toolResult" in delta and index not in parts :
236+ parts [index ] = {
237+ "type" : "tool_call_response" ,
238+ "response" : [
239+ item
240+ for item in delta ["toolResult" ]
241+ if "json" in item or "text" in item
242+ ],
243+ }
244+
245+ yield event
246+ continue
247+
248+ if "toolResult" in delta and "response" in parts [index ]:
249+ cast ("ToolCallResponsePart" , parts [index ])["response" ] += [
250+ item
251+ for item in delta ["toolResult" ]
252+ if "json" in item or "text" in item
253+ ]
254+
255+ yield event
256+ continue
257+
258+ if "toolResult" in delta :
259+ cast ("ToolCallResponsePart" , parts [index ])["response" ] = [
260+ item
261+ for item in delta ["toolResult" ]
262+ if "json" in item or "text" in item
263+ ]
264+
265+ yield event
266+ continue
267+
268+ if "reasoningContent" in delta and index not in parts :
269+ parts [index ] = {"type" : "reasoning" }
270+
271+ if "text" in delta ["reasoningContent" ]:
272+ cast ("ReasoningPart" , parts [index ])["content" ] = delta [
273+ "reasoningContent"
274+ ]["text" ]
275+
276+ yield event
277+ continue
278+
279+ if (
280+ "reasoningContent" in delta
281+ and "text" in delta ["reasoningContent" ]
282+ and "content" in parts [index ]
283+ ):
284+ cast ("ReasoningPart" , parts [index ])["content" ] += delta [
285+ "reasoningContent"
286+ ]["text" ]
287+
288+ yield event
289+ continue
290+
291+ if "reasoningContent" in delta and "text" in delta ["reasoningContent" ]:
292+ cast ("ReasoningPart" , parts [index ])["content" ] = delta [
293+ "reasoningContent"
294+ ]["text" ]
295+
296+ yield event
297+ continue
298+
299+ if (
300+ "image" in delta
301+ and "source" in delta ["image" ]
302+ and "s3Location" in delta ["image" ]["source" ]
303+ ):
304+ parts [index ] = {
305+ "type" : "uri" ,
306+ "modality" : "image" ,
307+ "uri" : delta ["image" ]["source" ]["s3Location" ]["uri" ],
308+ }
309+
310+ yield event
311+ continue
312+
313+ if (
314+ "image" in delta
315+ and "source" in delta ["image" ]
316+ and "bytes" in delta ["image" ]["source" ]
317+ ):
318+ parts [index ] = {
319+ "type" : "blob" ,
320+ "modality" : "image" ,
321+ }
322+
323+ yield event
324+ continue
127325
128326 yield event
129327 except GeneratorExit :
0 commit comments