|
30 | 30 | ConverseStreamResponseTypeDef, |
31 | 31 | ) |
32 | 32 |
|
33 | | - from sentry_sdk._types import Attributes, TextPart, ToolDefinition |
| 33 | + from sentry_sdk._types import ( |
| 34 | + Attributes, |
| 35 | + BlobPart, |
| 36 | + InputMessage, |
| 37 | + ReasoningPart, |
| 38 | + TextPart, |
| 39 | + ToolCallPart, |
| 40 | + ToolCallResponsePart, |
| 41 | + ToolDefinition, |
| 42 | + UriPart, |
| 43 | + ) |
34 | 44 | from sentry_sdk.integrations.boto3._context import AwsCallContext |
35 | 45 | from sentry_sdk.tracing import Span |
36 | 46 |
|
@@ -160,6 +170,193 @@ def _transform_system_instructions( |
160 | 170 | return instruction_text_parts |
161 | 171 |
|
162 | 172 |
|
| 173 | +def _transform_messages( |
| 174 | + input_messages: "Union[list[dict[str, Any]], tuple[dict[str, Any], ...]]", |
| 175 | +) -> "list[InputMessage]": |
| 176 | + """ |
| 177 | + Unlike other providers, AWS declares that input and output messages use the same message type. |
| 178 | + For this reason, the same transform is used for user and assistant messages. |
| 179 | + """ |
| 180 | + messages: "list[InputMessage]" = [] |
| 181 | + for message in input_messages: |
| 182 | + if not isinstance(message, dict): |
| 183 | + continue |
| 184 | + |
| 185 | + content = message.get("content") |
| 186 | + if not isinstance(content, (list, tuple)) or "role" not in message: |
| 187 | + continue |
| 188 | + |
| 189 | + parts: "list[Union[TextPart, ReasoningPart, ToolCallPart, ToolCallResponsePart, UriPart, BlobPart]]" = [] |
| 190 | + for block in content: |
| 191 | + if not isinstance(block, dict): |
| 192 | + continue |
| 193 | + |
| 194 | + if "text" in block: |
| 195 | + parts.append({"type": "text", "content": block["text"]}) |
| 196 | + continue |
| 197 | + |
| 198 | + tool_use = block.get("toolUse") |
| 199 | + if tool_use is not None: |
| 200 | + tool_call_part: "ToolCallPart" = {"type": "tool_call"} |
| 201 | + |
| 202 | + if not isinstance(tool_use, dict): |
| 203 | + parts.append(tool_call_part) |
| 204 | + continue |
| 205 | + |
| 206 | + if "name" in tool_use: |
| 207 | + tool_call_part["name"] = tool_use["name"] |
| 208 | + if "input" in tool_use: |
| 209 | + tool_call_part["arguments"] = tool_use["input"] |
| 210 | + parts.append(tool_call_part) |
| 211 | + continue |
| 212 | + |
| 213 | + tool_result = block.get("toolResult") |
| 214 | + if tool_result is not None: |
| 215 | + tool_call_response_part: "ToolCallResponsePart" = { |
| 216 | + "type": "tool_call_response", |
| 217 | + } |
| 218 | + |
| 219 | + if ( |
| 220 | + not isinstance(tool_result, dict) |
| 221 | + or "content" not in tool_result |
| 222 | + or not isinstance(tool_result["content"], (list, tuple)) |
| 223 | + ): |
| 224 | + parts.append(tool_call_response_part) |
| 225 | + continue |
| 226 | + |
| 227 | + tool_call_response_part["response"] = [ |
| 228 | + item |
| 229 | + for item in tool_result["content"] |
| 230 | + if isinstance(item, dict) and ("json" in item or "text" in item) |
| 231 | + ] |
| 232 | + parts.append(tool_call_response_part) |
| 233 | + continue |
| 234 | + |
| 235 | + reasoning_content = block.get("reasoningContent") |
| 236 | + if reasoning_content is not None: |
| 237 | + reasoning_part: "ReasoningPart" = {"type": "reasoning"} |
| 238 | + |
| 239 | + if ( |
| 240 | + not isinstance(reasoning_content, dict) |
| 241 | + or "reasoningText" not in reasoning_content |
| 242 | + or not isinstance(reasoning_content["reasoningText"], dict) |
| 243 | + or "text" not in reasoning_content["reasoningText"] |
| 244 | + ): |
| 245 | + parts.append(reasoning_part) |
| 246 | + continue |
| 247 | + |
| 248 | + reasoning_part["content"] = reasoning_content["reasoningText"]["text"] |
| 249 | + parts.append(reasoning_part) |
| 250 | + continue |
| 251 | + |
| 252 | + image = block.get("image") |
| 253 | + if ( |
| 254 | + isinstance(image, dict) |
| 255 | + and isinstance(image.get("source"), dict) |
| 256 | + and "s3Location" in image["source"] |
| 257 | + ): |
| 258 | + uri_part: "UriPart" = { |
| 259 | + "type": "uri", |
| 260 | + "modality": "image", |
| 261 | + } |
| 262 | + s3_location = image["source"]["s3Location"] |
| 263 | + if not isinstance(s3_location, dict) or "uri" not in s3_location: |
| 264 | + parts.append(uri_part) |
| 265 | + continue |
| 266 | + |
| 267 | + uri_part["uri"] = s3_location["uri"] |
| 268 | + parts.append(uri_part) |
| 269 | + continue |
| 270 | + elif ( |
| 271 | + isinstance(image, dict) |
| 272 | + and isinstance(image.get("source"), dict) |
| 273 | + and "bytes" in image["source"] |
| 274 | + ): |
| 275 | + parts.append({"type": "blob", "modality": "image"}) |
| 276 | + continue |
| 277 | + |
| 278 | + video = block.get("video") |
| 279 | + if ( |
| 280 | + isinstance(video, dict) |
| 281 | + and isinstance(video.get("source"), dict) |
| 282 | + and "s3Location" in video["source"] |
| 283 | + ): |
| 284 | + uri_part = { |
| 285 | + "type": "uri", |
| 286 | + "modality": "video", |
| 287 | + } |
| 288 | + s3_location = video["source"]["s3Location"] |
| 289 | + if not isinstance(s3_location, dict) or "uri" not in s3_location: |
| 290 | + parts.append(uri_part) |
| 291 | + continue |
| 292 | + |
| 293 | + uri_part["uri"] = s3_location["uri"] |
| 294 | + parts.append(uri_part) |
| 295 | + continue |
| 296 | + elif ( |
| 297 | + isinstance(video, dict) |
| 298 | + and isinstance(video.get("source"), dict) |
| 299 | + and "bytes" in video["source"] |
| 300 | + ): |
| 301 | + parts.append({"type": "blob", "modality": "video"}) |
| 302 | + continue |
| 303 | + |
| 304 | + audio = block.get("audio") |
| 305 | + if ( |
| 306 | + isinstance(audio, dict) |
| 307 | + and isinstance(audio.get("source"), dict) |
| 308 | + and "s3Location" in audio["source"] |
| 309 | + ): |
| 310 | + uri_part = { |
| 311 | + "type": "uri", |
| 312 | + "modality": "audio", |
| 313 | + } |
| 314 | + s3_location = audio["source"]["s3Location"] |
| 315 | + if not isinstance(s3_location, dict) or "uri" not in s3_location: |
| 316 | + parts.append(uri_part) |
| 317 | + continue |
| 318 | + |
| 319 | + uri_part["uri"] = s3_location["uri"] |
| 320 | + parts.append(uri_part) |
| 321 | + continue |
| 322 | + elif ( |
| 323 | + isinstance(audio, dict) |
| 324 | + and isinstance(audio.get("source"), dict) |
| 325 | + and "bytes" in audio["source"] |
| 326 | + ): |
| 327 | + parts.append({"type": "blob", "modality": "audio"}) |
| 328 | + continue |
| 329 | + |
| 330 | + document = block.get("document") |
| 331 | + if ( |
| 332 | + not isinstance(document, dict) |
| 333 | + or "source" not in document |
| 334 | + or not isinstance(document["source"], dict) |
| 335 | + ): |
| 336 | + continue |
| 337 | + |
| 338 | + if "s3Location" in document["source"]: |
| 339 | + uri_part = { |
| 340 | + "type": "uri", |
| 341 | + "modality": "document", |
| 342 | + } |
| 343 | + s3_location = document["source"]["s3Location"] |
| 344 | + if not isinstance(s3_location, dict) or "uri" not in s3_location: |
| 345 | + parts.append(uri_part) |
| 346 | + continue |
| 347 | + |
| 348 | + uri_part["uri"] = s3_location["uri"] |
| 349 | + parts.append(uri_part) |
| 350 | + continue |
| 351 | + |
| 352 | + if "bytes" in document["source"]: |
| 353 | + parts.append({"type": "blob", "modality": "document"}) |
| 354 | + continue |
| 355 | + |
| 356 | + messages.append({"role": message["role"], "parts": parts}) |
| 357 | + return messages |
| 358 | + |
| 359 | + |
163 | 360 | def _transform_tool_definitions( |
164 | 361 | tools: "list[dict[str, Any]]", |
165 | 362 | ) -> "list[ToolDefinition]": |
@@ -258,6 +455,14 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": |
258 | 455 | _transform_system_instructions(system_instructions) |
259 | 456 | ) |
260 | 457 |
|
| 458 | + input_messages = ctx.params.get("messages") |
| 459 | + if ( |
| 460 | + has_data_collection_enabled(client_options) or should_send_default_pii() |
| 461 | + ) and isinstance(input_messages, (list, tuple)): |
| 462 | + attributes[SPANDATA.GEN_AI_INPUT_MESSAGES] = json.dumps( |
| 463 | + _transform_messages(input_messages) |
| 464 | + ) |
| 465 | + |
261 | 466 | tool_config = ctx.params.get("toolConfig") |
262 | 467 | if not isinstance(tool_config, dict): |
263 | 468 | return attributes |
|
0 commit comments