Skip to content

Commit 6ec7171

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

3 files changed

Lines changed: 761 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: 200 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,187 @@ 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 not isinstance(tool_result, dict):
262+
parts.append(tool_call_response_part)
263+
continue
264+
265+
if "content" in tool_result:
266+
tool_call_response_part["response"] = tool_result["content"]
267+
268+
parts.append(tool_call_response_part)
269+
continue
270+
271+
reasoning_content = block.get("reasoningContent")
272+
if reasoning_content is not None:
273+
reasoning_part: "ReasoningPart" = {"type": "reasoning"}
274+
275+
if (
276+
not isinstance(reasoning_content, dict)
277+
or "reasoningText" not in reasoning_content
278+
or not isinstance(reasoning_content["reasoningText"], dict)
279+
or "text" not in reasoning_content["reasoningText"]
280+
):
281+
parts.append(reasoning_part)
282+
continue
283+
284+
reasoning_part["content"] = reasoning_content["reasoningText"]["text"]
285+
parts.append(reasoning_part)
286+
continue
287+
288+
image = block.get("image")
289+
if (
290+
isinstance(image, dict)
291+
and isinstance(image.get("source"), dict)
292+
and "s3Location" in image["source"]
293+
):
294+
uri_part: "UriPart" = {
295+
"type": "uri",
296+
"modality": "image",
297+
}
298+
s3_location = image["source"]["s3Location"]
299+
if not isinstance(s3_location, dict) or "uri" not in s3_location:
300+
parts.append(uri_part)
301+
continue
302+
303+
uri_part["uri"] = s3_location["uri"]
304+
parts.append(uri_part)
305+
continue
306+
elif (
307+
isinstance(image, dict)
308+
and isinstance(image.get("source"), dict)
309+
and "bytes" in image["source"]
310+
):
311+
parts.append({"type": "blob", "modality": "image"})
312+
continue
313+
314+
video = block.get("video")
315+
if (
316+
isinstance(video, dict)
317+
and isinstance(video.get("source"), dict)
318+
and "s3Location" in video["source"]
319+
):
320+
uri_part = {
321+
"type": "uri",
322+
"modality": "video",
323+
}
324+
s3_location = video["source"]["s3Location"]
325+
if not isinstance(s3_location, dict) or "uri" not in s3_location:
326+
parts.append(uri_part)
327+
continue
328+
329+
uri_part["uri"] = s3_location["uri"]
330+
parts.append(uri_part)
331+
continue
332+
elif (
333+
isinstance(video, dict)
334+
and isinstance(video.get("source"), dict)
335+
and "bytes" in video["source"]
336+
):
337+
parts.append({"type": "blob", "modality": "video"})
338+
continue
339+
340+
audio = block.get("audio")
341+
if (
342+
isinstance(audio, dict)
343+
and isinstance(audio.get("source"), dict)
344+
and "s3Location" in audio["source"]
345+
):
346+
uri_part = {
347+
"type": "uri",
348+
"modality": "audio",
349+
}
350+
s3_location = audio["source"]["s3Location"]
351+
if not isinstance(s3_location, dict) or "uri" not in s3_location:
352+
parts.append(uri_part)
353+
continue
354+
355+
uri_part["uri"] = s3_location["uri"]
356+
parts.append(uri_part)
357+
continue
358+
elif (
359+
isinstance(audio, dict)
360+
and isinstance(audio.get("source"), dict)
361+
and "bytes" in audio["source"]
362+
):
363+
parts.append({"type": "blob", "modality": "audio"})
364+
continue
365+
366+
document = block.get("document")
367+
if (
368+
not isinstance(document, dict)
369+
or "source" not in document
370+
or not isinstance(document["source"], dict)
371+
):
372+
continue
373+
374+
if "s3Location" in document["source"]:
375+
uri_part = {
376+
"type": "uri",
377+
"modality": "document",
378+
}
379+
s3_location = document["source"]["s3Location"]
380+
if not isinstance(s3_location, dict) or "uri" not in s3_location:
381+
parts.append(uri_part)
382+
continue
383+
384+
uri_part["uri"] = s3_location["uri"]
385+
parts.append(uri_part)
386+
continue
387+
388+
if "bytes" in document["source"]:
389+
parts.append({"type": "blob", "modality": "document"})
390+
continue
391+
392+
messages.append({"role": message["role"], "parts": parts})
393+
return messages
394+
395+
205396
class _BedrockExtension(_ServiceExtension):
206397
__slots__ = ()
207398
skip_if_no_active_span = False
@@ -250,6 +441,14 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
250441
if not client_options["data_collection"]["gen_ai"]["inputs"]:
251442
return attributes
252443

444+
input_messages = ctx.params.get("messages")
445+
if (
446+
has_data_collection_enabled(client_options) or should_send_default_pii()
447+
) and isinstance(input_messages, (list, tuple)):
448+
attributes[SPANDATA.GEN_AI_INPUT_MESSAGES] = json.dumps(
449+
_transform_messages(input_messages)
450+
)
451+
253452
system_instructions = ctx.params.get("system")
254453
if (
255454
has_data_collection_enabled(client_options) or should_send_default_pii()

0 commit comments

Comments
 (0)