Skip to content

Commit

Permalink
Gemini empty text (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Dec 30, 2024
1 parent e9bdb04 commit 7dfca69
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pydantic_ai_slim/pydantic_ai/models/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ def _content_model_response(m: ModelResponse) -> _GeminiContent:
if isinstance(item, ToolCallPart):
parts.append(_function_call_part_from_call(item))
elif isinstance(item, TextPart):
parts.append(_GeminiTextPart(text=item.content))
if item.content:
parts.append(_GeminiTextPart(text=item.content))
else:
assert_never(item)
return _GeminiContent(role='model', parts=parts)
Expand Down
38 changes: 38 additions & 0 deletions tests/models/test_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ModelResponse,
RetryPromptPart,
SystemPromptPart,
TextPart,
ToolCallPart,
ToolReturnPart,
UserPromptPart,
Expand Down Expand Up @@ -694,3 +695,40 @@ async def test_stream_text_heterogeneous(get_gemini_client: GetGeminiClient):
async with agent.run_stream('Hello') as result:
with pytest.raises(UnexpectedModelBehavior, match=msg):
await result.get_data()


async def test_empty_text_ignored():
content = _content_model_response(
ModelResponse(
parts=[
ToolCallPart.from_raw_args('final_result', {'response': [1, 2, 123]}),
TextPart(content='xxx'),
]
)
)
# text included
assert content == snapshot(
{
'role': 'model',
'parts': [
{'function_call': {'name': 'final_result', 'args': {'response': [1, 2, 123]}}},
{'text': 'xxx'},
],
}
)

content = _content_model_response(
ModelResponse(
parts=[
ToolCallPart.from_raw_args('final_result', {'response': [1, 2, 123]}),
TextPart(content=''),
]
)
)
# text skipped
assert content == snapshot(
{
'role': 'model',
'parts': [{'function_call': {'name': 'final_result', 'args': {'response': [1, 2, 123]}}}],
}
)

0 comments on commit 7dfca69

Please sign in to comment.