Skip to content

Allow empty messages when using GoogleModel #1922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pydantic_ai_slim/pydantic_ai/models/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,10 @@ async def _map_messages(self, messages: list[ModelMessage]) -> tuple[ContentDict
else:
assert_never(part)

if message_parts: # pragma: no branch
contents.append({'role': 'user', 'parts': message_parts})
# Google GenAI requires at least one part in the message.
if not message_parts:
message_parts = [{'text': ''}]
contents.append({'role': 'user', 'parts': message_parts})
elif isinstance(m, ModelResponse):
contents.append(_content_model_response(m))
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
interactions:
- request:
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
content-length:
- '173'
content-type:
- application/json
host:
- generativelanguage.googleapis.com
method: POST
parsed_body:
contents:
- parts:
- text: ''
role: user
generationConfig: {}
systemInstruction:
parts:
- text: You are a helpful assistant.
role: user
uri: https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent
response:
headers:
alt-svc:
- h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
content-length:
- '736'
content-type:
- application/json; charset=UTF-8
server-timing:
- gfet4t7; dur=361
transfer-encoding:
- chunked
vary:
- Origin
- X-Origin
- Referer
parsed_body:
candidates:
- avgLogprobs: -0.20726837430681502
content:
parts:
- text: |
Please provide me with a question or task. I need some information to be able to help you.
role: model
finishReason: STOP
modelVersion: gemini-1.5-flash
responseId: y5BBaPCQELGqmecP68OBiAc
usageMetadata:
candidatesTokenCount: 21
candidatesTokensDetails:
- modality: TEXT
tokenCount: 21
promptTokenCount: 7
promptTokensDetails:
- modality: TEXT
tokenCount: 7
totalTokenCount: 28
status:
code: 200
message: OK
version: 1
10 changes: 10 additions & 0 deletions tests/models/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,13 @@ async def test_google_model_safety_settings(allow_model_requests: None, google_p

with pytest.raises(UnexpectedModelBehavior, match='Safety settings triggered'):
await agent.run('Tell me a joke about a Brazilians.')


async def test_google_model_empty_user_prompt(allow_model_requests: None, google_provider: GoogleProvider):
m = GoogleModel('gemini-1.5-flash', provider=google_provider)
agent = Agent(m, instructions='You are a helpful assistant.')

result = await agent.run()
assert result.output == snapshot(
'Please provide me with a question or task. I need some information to be able to help you.\n'
)
Loading