1515from __future__ import annotations
1616
1717import logging
18+ from enum import Enum
1819
1920from google .genai import types as genai_types
2021from opentelemetry .util .genai .types import (
22+ FinishReason ,
2123 InputMessage ,
22- OutputMessage ,
2324 MessagePart ,
24- FinishReason ,
25+ OutputMessage ,
2526 Text ,
2627 ToolCall ,
2728 ToolCallResponse ,
2829)
2930
30- from .message_models import (
31- # BlobPart,
32- # FileDataPart,
33- Role ,
34- )
31+
32+ class Role (str , Enum ):
33+ SYSTEM = "system"
34+ USER = "user"
35+ ASSISTANT = "assistant"
36+ TOOL = "tool"
37+
3538
3639_logger = logging .getLogger (__name__ )
3740
41+
3842def to_input_messages (
3943 * ,
4044 contents : list [genai_types .Content ],
4145) -> list [InputMessage ]:
4246 return [_to_input_message (content ) for content in contents ]
4347
48+
4449def to_output_messages (
4550 * ,
4651 candidates : list [genai_types .Candidate ],
@@ -58,32 +63,29 @@ def content_to_output_message(
5863 parts = message .parts ,
5964 )
6065
61- messages = (
62- content_to_output_message (candidate ) for candidate in candidates
63- )
66+ messages = (content_to_output_message (candidate ) for candidate in candidates )
6467 return [message for message in messages if message is not None ]
6568
69+
6670def to_system_instructions (
6771 * ,
6872 content : genai_types .Content ,
6973) -> list [MessagePart ]:
70- parts = (
71- _to_part (part , idx ) for idx , part in enumerate (content .parts or [])
72- )
74+ parts = (_to_part (part , idx ) for idx , part in enumerate (content .parts or []))
7375 return [part for part in parts if part is not None ]
7476
77+
7578def _to_input_message (
7679 content : genai_types .Content ,
7780) -> InputMessage :
78- parts = (
79- _to_part (part , idx ) for idx , part in enumerate (content .parts or [])
80- )
81+ parts = (_to_part (part , idx ) for idx , part in enumerate (content .parts or []))
8182 return InputMessage (
8283 role = _to_role (content .role ),
8384 # filter Nones
8485 parts = [part for part in parts if part is not None ],
8586 )
8687
88+
8789def _to_part (part : genai_types .Part , idx : int ) -> MessagePart | None :
8890 def tool_call_id (name : str | None ) -> str :
8991 if name :
@@ -93,30 +95,23 @@ def tool_call_id(name: str | None) -> str:
9395 if (text := part .text ) is not None :
9496 return Text (content = text )
9597
96- # if data := part.inline_data: # TODO ???
97- # return BlobPart(mime_type=data.mime_type or "", data=data.data or b"")
98-
99- # if data := part.file_data: # TODO ???
100- # return FileDataPart(
101- # mime_type=data.mime_type or "", file_uri=data.file_uri or ""
102- # )
103-
10498 if call := part .function_call :
10599 return ToolCall (
106- id = call .id or tool_call_id (call .name ), # TODO ???
100+ id = call .id or tool_call_id (call .name ),
107101 name = call .name or "" ,
108102 arguments = call .args ,
109103 )
110104
111105 if response := part .function_response :
112106 return ToolCallResponse (
113- id = response .id or tool_call_id (response .name ), # TODO ???
107+ id = response .id or tool_call_id (response .name ),
114108 response = response .response ,
115109 )
116110
117111 _logger .info ("Unknown part dropped from telemetry %s" , part )
118112 return None
119113
114+
120115def _to_role (role : str | None ) -> str :
121116 if role == "user" :
122117 return Role .USER .value
0 commit comments