Skip to content

Commit e252f8c

Browse files
committed
Modified google.py and started using the method _inline_text_file_part in DocumentUrl and BinaryContent
1 parent c319843 commit e252f8c

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,13 @@ async def _map_user_prompt(self, part: UserPromptPart) -> list[PartDict]:
569569
content.append({'text': item})
570570
elif isinstance(item, BinaryContent):
571571
if self._is_text_like_media_type(item.media_type):
572-
content.append({'text': item.data.decode('utf-8')})
572+
content.append(
573+
self._inline_text_file_part(
574+
item.data.decode('utf-8'),
575+
media_type=item.media_type,
576+
identifier=item.identifier,
577+
)
578+
)
573579
else:
574580
inline_data_dict: BlobDict = {'data': item.data, 'mime_type': item.media_type}
575581
part_dict: PartDict = {'inline_data': inline_data_dict}
@@ -580,7 +586,13 @@ async def _map_user_prompt(self, part: UserPromptPart) -> list[PartDict]:
580586
elif isinstance(item, DocumentUrl):
581587
if self._is_text_like_media_type(item.media_type):
582588
downloaded_text = await download_item(item, data_format='text')
583-
content.append({'text': downloaded_text['data']})
589+
content.append(
590+
self._inline_text_file_part(
591+
downloaded_text['data'],
592+
media_type=item.media_type,
593+
identifier=item.identifier,
594+
)
595+
)
584596
else:
585597
downloaded_item = await download_item(item, data_format='bytes')
586598
inline_data_dict: BlobDict = {
@@ -628,6 +640,17 @@ def _is_text_like_media_type(media_type: str) -> bool:
628640
or media_type in ('application/x-yaml', 'application/yaml')
629641
)
630642

643+
@staticmethod
644+
def _inline_text_file_part(text: str, *, media_type: str, identifier: str) -> ChatCompletionContentPartTextParam:
645+
text = '\n'.join(
646+
[
647+
f'-----BEGIN FILE id="{identifier}" type="{media_type}"-----',
648+
text,
649+
f'-----END FILE id="{identifier}"-----',
650+
]
651+
)
652+
return {"text": text}
653+
631654
def _map_response_schema(self, o: OutputObjectDefinition) -> dict[str, Any]:
632655
response_schema = o.json_schema.copy()
633656
if o.name:

0 commit comments

Comments
 (0)