Skip to content

Commit f35474a

Browse files
feat(bedrock): Set gen_ai.input.messages attribute (#8242)
Set input messages from the `messages` parameter passed to the Bedrock API.
1 parent 72a02b1 commit f35474a

3 files changed

Lines changed: 525 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

@@ -160,6 +170,193 @@ def _transform_system_instructions(
160170
return instruction_text_parts
161171

162172

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+
163360
def _transform_tool_definitions(
164361
tools: "list[dict[str, Any]]",
165362
) -> "list[ToolDefinition]":
@@ -258,6 +455,14 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
258455
_transform_system_instructions(system_instructions)
259456
)
260457

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+
261466
tool_config = ctx.params.get("toolConfig")
262467
if not isinstance(tool_config, dict):
263468
return attributes

0 commit comments

Comments
 (0)