11try :
2+ from google .genai import types as genai_types
23 from vertexai import types as vertexai_types
34except ImportError as e :
45 raise ImportError (
2526
2627
2728_TO_SDK_TASK_STATE = {
28- vertexai_types .State .STATE_UNSPECIFIED : TaskState .unknown ,
29- vertexai_types .State .SUBMITTED : TaskState .submitted ,
30- vertexai_types .State .WORKING : TaskState .working ,
31- vertexai_types .State .COMPLETED : TaskState .completed ,
32- vertexai_types .State .CANCELLED : TaskState .canceled ,
33- vertexai_types .State .FAILED : TaskState .failed ,
34- vertexai_types .State .REJECTED : TaskState .rejected ,
35- vertexai_types .State .INPUT_REQUIRED : TaskState .input_required ,
36- vertexai_types .State .AUTH_REQUIRED : TaskState .auth_required ,
29+ vertexai_types .A2aTaskState .STATE_UNSPECIFIED : TaskState .unknown ,
30+ vertexai_types .A2aTaskState .SUBMITTED : TaskState .submitted ,
31+ vertexai_types .A2aTaskState .WORKING : TaskState .working ,
32+ vertexai_types .A2aTaskState .COMPLETED : TaskState .completed ,
33+ vertexai_types .A2aTaskState .CANCELLED : TaskState .canceled ,
34+ vertexai_types .A2aTaskState .FAILED : TaskState .failed ,
35+ vertexai_types .A2aTaskState .REJECTED : TaskState .rejected ,
36+ vertexai_types .A2aTaskState .INPUT_REQUIRED : TaskState .input_required ,
37+ vertexai_types .A2aTaskState .AUTH_REQUIRED : TaskState .auth_required ,
3738}
3839
3940_SDK_TO_STORED_TASK_STATE = {v : k for k , v in _TO_SDK_TASK_STATE .items ()}
4041
4142
42- def to_sdk_task_state (stored_state : vertexai_types .State ) -> TaskState :
43+ def to_sdk_task_state (stored_state : vertexai_types .A2aTaskState ) -> TaskState :
4344 """Converts a proto A2aTask.State to a TaskState enum."""
4445 return _TO_SDK_TASK_STATE .get (stored_state , TaskState .unknown )
4546
4647
47- def to_stored_task_state (task_state : TaskState ) -> vertexai_types .State :
48+ def to_stored_task_state (task_state : TaskState ) -> vertexai_types .A2aTaskState :
4849 """Converts a TaskState enum to a proto A2aTask.State enum value."""
4950 return _SDK_TO_STORED_TASK_STATE .get (
50- task_state , vertexai_types .State .STATE_UNSPECIFIED
51+ task_state , vertexai_types .A2aTaskState .STATE_UNSPECIFIED
5152 )
5253
5354
54- def to_stored_part (part : Part ) -> vertexai_types .Part :
55+ def to_stored_part (part : Part ) -> genai_types .Part :
5556 """Converts a SDK Part to a proto Part."""
5657 if isinstance (part .root , TextPart ):
57- return vertexai_types .Part (text = part .root .text )
58+ return genai_types .Part (text = part .root .text )
5859 if isinstance (part .root , DataPart ):
5960 data_bytes = json .dumps (part .root .data ).encode ('utf-8' )
60- return vertexai_types .Part (
61- inline_data = vertexai_types .Blob (
61+ return genai_types .Part (
62+ inline_data = genai_types .Blob (
6263 mime_type = 'application/json' , data = data_bytes
6364 )
6465 )
6566 if isinstance (part .root , FilePart ):
6667 file_content = part .root .file
6768 if isinstance (file_content , FileWithBytes ):
6869 decoded_bytes = base64 .b64decode (file_content .bytes )
69- return vertexai_types .Part (
70- inline_data = vertexai_types .Blob (
70+ return genai_types .Part (
71+ inline_data = genai_types .Blob (
7172 mime_type = file_content .mime_type or '' , data = decoded_bytes
7273 )
7374 )
7475 if isinstance (file_content , FileWithUri ):
75- return vertexai_types .Part (
76- file_data = vertexai_types .FileData (
76+ return genai_types .Part (
77+ file_data = genai_types .FileData (
7778 mime_type = file_content .mime_type or '' ,
7879 file_uri = file_content .uri ,
7980 )
8081 )
8182 raise ValueError (f'Unsupported part type: { type (part .root )} ' )
8283
8384
84- def to_sdk_part (stored_part : vertexai_types .Part ) -> Part :
85+ def to_sdk_part (stored_part : genai_types .Part ) -> Part :
8586 """Converts a proto Part to a SDK Part."""
8687 if stored_part .text :
8788 return Part (root = TextPart (text = stored_part .text ))
8889 if stored_part .inline_data :
89- encoded_bytes = base64 .b64encode (stored_part . inline_data . data ). decode (
90- 'utf-8 '
91- )
90+ encoded_bytes = base64 .b64encode (
91+ stored_part . inline_data . data or b' '
92+ ). decode ( 'utf-8' )
9293 return Part (
9394 root = FilePart (
9495 file = FileWithBytes (
@@ -97,7 +98,7 @@ def to_sdk_part(stored_part: vertexai_types.Part) -> Part:
9798 )
9899 )
99100 )
100- if stored_part .file_data :
101+ if stored_part .file_data and stored_part . file_data . file_uri :
101102 return Part (
102103 root = FilePart (
103104 file = FileWithUri (
0 commit comments