Skip to content

Commit bbf0fb1

Browse files
feat(bedrock): Set gen_ai.input.messages attribute
1 parent 7dec8c9 commit bbf0fb1

3 files changed

Lines changed: 767 additions & 8 deletions

File tree

‎sentry_sdk/_types.py‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,20 +471,44 @@ class DataCollection(TypedDict):
471471

472472
class TextPart(TypedDict):
473473
type: Literal["text"]
474-
content: str
474+
content: NotRequired[str]
475475

476476
class ReasoningPart(TypedDict):
477477
type: Literal["reasoning"]
478-
content: str
478+
content: NotRequired[str]
479479

480480
class ToolCallPart(TypedDict):
481481
type: Literal["tool_call"]
482482
name: NotRequired[str]
483483
arguments: NotRequired[Any]
484484

485+
class ToolCallResponsePart(TypedDict):
486+
type: Literal["tool_call_response"]
487+
response: NotRequired[Any]
488+
489+
Modality = Literal["image", "video", "audio", "document"]
490+
491+
class UriPart(TypedDict):
492+
type: Literal["uri"]
493+
modality: NotRequired[Modality]
494+
uri: NotRequired[str]
495+
496+
class BlobPart(TypedDict):
497+
type: Literal["blob"]
498+
modality: NotRequired[Modality]
499+
485500
class InputMessage(TypedDict):
486501
role: Literal["user", "assistant", "tool"]
487-
parts: list[Union[TextPart, ReasoningPart, ToolCallPart]]
502+
parts: list[
503+
Union[
504+
TextPart,
505+
ReasoningPart,
506+
ToolCallPart,
507+
ToolCallResponsePart,
508+
UriPart,
509+
BlobPart,
510+
]
511+
]
488512

489513
class OutputMessage(TypedDict):
490514
role: Literal["assistant"]

‎sentry_sdk/integrations/boto3/_services/bedrock.py‎

Lines changed: 206 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@
3030
ConverseStreamResponseTypeDef,
3131
)
3232

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+
)
3444
from sentry_sdk.integrations.boto3._context import AwsCallContext
3545
from sentry_sdk.tracing import Span
3646

@@ -202,6 +212,193 @@ def _transform_system_instructions(
202212
return instruction_text_parts
203213

204214

215+
def _transform_messages(
216+
input_messages: "Union[list[dict[str, Any]], tuple[dict[str, Any], ...]]",
217+
) -> "list[InputMessage]":
218+
"""
219+
Unlike other providers, AWS declares that input and output messages use the same message type.
220+
For this reason, the same transform is used for user and assistant messages.
221+
"""
222+
messages: "list[InputMessage]" = []
223+
for message in input_messages:
224+
if not isinstance(message, dict):
225+
continue
226+
227+
content = message.get("content")
228+
if not isinstance(content, (list, tuple)) or "role" not in message:
229+
continue
230+
231+
parts: "list[Union[TextPart, ReasoningPart, ToolCallPart, ToolCallResponsePart, UriPart, BlobPart]]" = []
232+
for block in content:
233+
if not isinstance(block, dict):
234+
continue
235+
236+
if "text" in block:
237+
parts.append({"type": "text", "content": block["text"]})
238+
continue
239+
240+
tool_use = block.get("toolUse")
241+
if tool_use is not None:
242+
tool_call_part: "ToolCallPart" = {"type": "tool_call"}
243+
244+
if not isinstance(tool_use, dict):
245+
parts.append(tool_call_part)
246+
continue
247+
248+
if "name" in tool_use:
249+
tool_call_part["name"] = tool_use["name"]
250+
if "input" in tool_use:
251+
tool_call_part["arguments"] = tool_use["input"]
252+
parts.append(tool_call_part)
253+
continue
254+
255+
tool_result = block.get("toolResult")
256+
if tool_result is not None:
257+
tool_call_response_part: "ToolCallResponsePart" = {
258+
"type": "tool_call_response",
259+
}
260+
261+
if (
262+
not isinstance(tool_result, dict)
263+
or "content" not in tool_result
264+
or not isinstance(tool_result["content"], (list, tuple))
265+
):
266+
parts.append(tool_call_response_part)
267+
continue
268+
269+
tool_call_response_part["response"] = [
270+
item
271+
for item in tool_result["content"]
272+
if isinstance(item, dict) and ("json" in item or "text" in item)
273+
]
274+
parts.append(tool_call_response_part)
275+
continue
276+
277+
reasoning_content = block.get("reasoningContent")
278+
if reasoning_content is not None:
279+
reasoning_part: "ReasoningPart" = {"type": "reasoning"}
280+
281+
if (
282+
not isinstance(reasoning_content, dict)
283+
or "reasoningText" not in reasoning_content
284+
or not isinstance(reasoning_content["reasoningText"], dict)
285+
or "text" not in reasoning_content["reasoningText"]
286+
):
287+
parts.append(reasoning_part)
288+
continue
289+
290+
reasoning_part["content"] = reasoning_content["reasoningText"]["text"]
291+
parts.append(reasoning_part)
292+
continue
293+
294+
image = block.get("image")
295+
if (
296+
isinstance(image, dict)
297+
and isinstance(image.get("source"), dict)
298+
and "s3Location" in image["source"]
299+
):
300+
uri_part: "UriPart" = {
301+
"type": "uri",
302+
"modality": "image",
303+
}
304+
s3_location = image["source"]["s3Location"]
305+
if not isinstance(s3_location, dict) or "uri" not in s3_location:
306+
parts.append(uri_part)
307+
continue
308+
309+
uri_part["uri"] = s3_location["uri"]
310+
parts.append(uri_part)
311+
continue
312+
elif (
313+
isinstance(image, dict)
314+
and isinstance(image.get("source"), dict)
315+
and "bytes" in image["source"]
316+
):
317+
parts.append({"type": "blob", "modality": "image"})
318+
continue
319+
320+
video = block.get("video")
321+
if (
322+
isinstance(video, dict)
323+
and isinstance(video.get("source"), dict)
324+
and "s3Location" in video["source"]
325+
):
326+
uri_part = {
327+
"type": "uri",
328+
"modality": "video",
329+
}
330+
s3_location = video["source"]["s3Location"]
331+
if not isinstance(s3_location, dict) or "uri" not in s3_location:
332+
parts.append(uri_part)
333+
continue
334+
335+
uri_part["uri"] = s3_location["uri"]
336+
parts.append(uri_part)
337+
continue
338+
elif (
339+
isinstance(video, dict)
340+
and isinstance(video.get("source"), dict)
341+
and "bytes" in video["source"]
342+
):
343+
parts.append({"type": "blob", "modality": "video"})
344+
continue
345+
346+
audio = block.get("audio")
347+
if (
348+
isinstance(audio, dict)
349+
and isinstance(audio.get("source"), dict)
350+
and "s3Location" in audio["source"]
351+
):
352+
uri_part = {
353+
"type": "uri",
354+
"modality": "audio",
355+
}
356+
s3_location = audio["source"]["s3Location"]
357+
if not isinstance(s3_location, dict) or "uri" not in s3_location:
358+
parts.append(uri_part)
359+
continue
360+
361+
uri_part["uri"] = s3_location["uri"]
362+
parts.append(uri_part)
363+
continue
364+
elif (
365+
isinstance(audio, dict)
366+
and isinstance(audio.get("source"), dict)
367+
and "bytes" in audio["source"]
368+
):
369+
parts.append({"type": "blob", "modality": "audio"})
370+
continue
371+
372+
document = block.get("document")
373+
if (
374+
not isinstance(document, dict)
375+
or "source" not in document
376+
or not isinstance(document["source"], dict)
377+
):
378+
continue
379+
380+
if "s3Location" in document["source"]:
381+
uri_part = {
382+
"type": "uri",
383+
"modality": "document",
384+
}
385+
s3_location = document["source"]["s3Location"]
386+
if not isinstance(s3_location, dict) or "uri" not in s3_location:
387+
parts.append(uri_part)
388+
continue
389+
390+
uri_part["uri"] = s3_location["uri"]
391+
parts.append(uri_part)
392+
continue
393+
394+
if "bytes" in document["source"]:
395+
parts.append({"type": "blob", "modality": "document"})
396+
continue
397+
398+
messages.append({"role": message["role"], "parts": parts})
399+
return messages
400+
401+
205402
class _BedrockExtension(_ServiceExtension):
206403
__slots__ = ()
207404
skip_if_no_active_span = False
@@ -250,6 +447,14 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
250447
if not client_options["data_collection"]["gen_ai"]["inputs"]:
251448
return attributes
252449

450+
input_messages = ctx.params.get("messages")
451+
if (
452+
has_data_collection_enabled(client_options) or should_send_default_pii()
453+
) and isinstance(input_messages, (list, tuple)):
454+
attributes[SPANDATA.GEN_AI_INPUT_MESSAGES] = json.dumps(
455+
_transform_messages(input_messages)
456+
)
457+
253458
system_instructions = ctx.params.get("system")
254459
if (
255460
has_data_collection_enabled(client_options) or should_send_default_pii()

0 commit comments

Comments
 (0)