Skip to content

Commit b8b9be4

Browse files
committed
Support UploadedFile for google genai models
1 parent 0ca16b9 commit b8b9be4

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
ThinkingPart,
3232
ToolCallPart,
3333
ToolReturnPart,
34+
UploadedFile,
3435
UserPromptPart,
3536
VideoUrl,
3637
)
@@ -54,6 +55,7 @@
5455
ContentUnionDict,
5556
CountTokensConfigDict,
5657
ExecutableCodeDict,
58+
File,
5759
FunctionCallDict,
5860
FunctionCallingConfigDict,
5961
FunctionCallingConfigMode,
@@ -425,7 +427,7 @@ async def _map_messages(self, messages: list[ModelMessage]) -> tuple[ContentDict
425427
if isinstance(part, SystemPromptPart):
426428
system_parts.append({'text': part.content})
427429
elif isinstance(part, UserPromptPart):
428-
message_parts.extend(await self._map_user_prompt(part))
430+
message_parts.extend(await self._map_user_prompt(part, contents))
429431
elif isinstance(part, ToolReturnPart):
430432
message_parts.append(
431433
{
@@ -465,7 +467,7 @@ async def _map_messages(self, messages: list[ModelMessage]) -> tuple[ContentDict
465467
system_instruction = ContentDict(role='user', parts=system_parts) if system_parts else None
466468
return system_instruction, contents
467469

468-
async def _map_user_prompt(self, part: UserPromptPart) -> list[PartDict]:
470+
async def _map_user_prompt(self, part: UserPromptPart, contents: list[ContentUnionDict]) -> list[PartDict]:
469471
if isinstance(part.content, str):
470472
return [{'text': part.content}]
471473
else:
@@ -499,6 +501,12 @@ async def _map_user_prompt(self, part: UserPromptPart) -> list[PartDict]:
499501
content.append(
500502
{'file_data': {'file_uri': item.url, 'mime_type': item.media_type}}
501503
) # pragma: lax no cover
504+
elif isinstance(item, UploadedFile):
505+
if not isinstance(item.file, File):
506+
raise UserError('UploadedFile.file must be a genai.types.File object')
507+
# genai.types.File is its own ContentUnionDict and not a
508+
# PartDict, so append to the contents directly.
509+
contents.append(item.file)
502510
else:
503511
assert_never(item)
504512
return content

0 commit comments

Comments
 (0)