Skip to content

Commit 7dfca69

Browse files
authored
Gemini empty text (#568)
1 parent e9bdb04 commit 7dfca69

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

pydantic_ai_slim/pydantic_ai/models/gemini.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ def _content_model_response(m: ModelResponse) -> _GeminiContent:
444444
if isinstance(item, ToolCallPart):
445445
parts.append(_function_call_part_from_call(item))
446446
elif isinstance(item, TextPart):
447-
parts.append(_GeminiTextPart(text=item.content))
447+
if item.content:
448+
parts.append(_GeminiTextPart(text=item.content))
448449
else:
449450
assert_never(item)
450451
return _GeminiContent(role='model', parts=parts)

tests/models/test_gemini.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ModelResponse,
2020
RetryPromptPart,
2121
SystemPromptPart,
22+
TextPart,
2223
ToolCallPart,
2324
ToolReturnPart,
2425
UserPromptPart,
@@ -694,3 +695,40 @@ async def test_stream_text_heterogeneous(get_gemini_client: GetGeminiClient):
694695
async with agent.run_stream('Hello') as result:
695696
with pytest.raises(UnexpectedModelBehavior, match=msg):
696697
await result.get_data()
698+
699+
700+
async def test_empty_text_ignored():
701+
content = _content_model_response(
702+
ModelResponse(
703+
parts=[
704+
ToolCallPart.from_raw_args('final_result', {'response': [1, 2, 123]}),
705+
TextPart(content='xxx'),
706+
]
707+
)
708+
)
709+
# text included
710+
assert content == snapshot(
711+
{
712+
'role': 'model',
713+
'parts': [
714+
{'function_call': {'name': 'final_result', 'args': {'response': [1, 2, 123]}}},
715+
{'text': 'xxx'},
716+
],
717+
}
718+
)
719+
720+
content = _content_model_response(
721+
ModelResponse(
722+
parts=[
723+
ToolCallPart.from_raw_args('final_result', {'response': [1, 2, 123]}),
724+
TextPart(content=''),
725+
]
726+
)
727+
)
728+
# text skipped
729+
assert content == snapshot(
730+
{
731+
'role': 'model',
732+
'parts': [{'function_call': {'name': 'final_result', 'args': {'response': [1, 2, 123]}}}],
733+
}
734+
)

0 commit comments

Comments
 (0)