Skip to content

Conversation

@naveen-corpusant
Copy link
Contributor

In a previous commit (#3360), we ignored text parts. On further testing, this actually causes some issues because of the structure of the if/elif/else statements (sorry for not catching this earlier! I'd tested my original revisions, but your code suggestions looked solid and worked in the happy path, but did break when testing again on 1.13 now).

I'd like to add a snapshot test to this PR- the tricky part is that it's hard to get this type of error to repro (happens on one of our tests about 1 in 10 or 15 times). Let me know if you can think of a better way to test this, I'll continue down the line of snapshot testing but wanted to put something up to also notify you that there's an error in 1.13

@DouweM
Copy link
Collaborator

DouweM commented Nov 10, 2025

@naveen-corpusant Ay, thanks for the heads-up, I was already planning to do another release today so at least the latest version won't be broken for too long. I suppose the issue is that we raise Unsupported response from Gemini in else, so we'd error on empty text 😬

@naveen-corpusant
Copy link
Contributor Author

nice yeah exactly, the else statement assert breaks things. totally my bad for not testing more extensively on this, I liked your suggestion but should have run it to catch this

Copy link
Collaborator

@DouweM DouweM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@naveen-corpusant I agree a test would be good; since it's probably impossible to reproduce reliably, I suggest crafting a custom GenerateContentResponse and calling _process_response directly.

@DouweM DouweM changed the title Properly fix google empty text parts Fix error when Google returns only empty text parts Nov 10, 2025
@DouweM DouweM self-assigned this Nov 10, 2025
@naveen-corpusant
Copy link
Contributor Author

@DouweM I've addressed your comments, and added two unit tests (one for the stream iterator, and the other for processing messages).

I've also verified that these tests fail on main, and pass on this PR:

failing tests (for reference):
uv run pytest -s tests/models/test_google.py::test_gemini_streamed_response_emits_text_events_for_non_empty_parts

                else:
>                   assert part.function_response is not None, f'Unexpected part: {part}'  # pragma: no cover
E                   AssertionError: Unexpected part: video_metadata=None thought=None inline_data=None file_data=None thought_signature=None function_call=None code_execution_result=None executable_code=None function_response=None text=''

pydantic_ai_slim/pydantic_ai/models/google.py

uv run pytest -s tests/models/test_google.py::test_google_process_response_filters_empty_text_parts

            else:  # pragma: no cover
>               raise UnexpectedModelBehavior(f'Unsupported response from Gemini: {part!r}')
E               pydantic_ai.exceptions.UnexpectedModelBehavior: Unsupported response from Gemini: Part(
E                 text=''
E               )

pydantic_ai_slim/pydantic_ai/models/google.py:842: UnexpectedModelBehavior

@naveen-corpusant
Copy link
Contributor Author

@DouweM addressed the comments! thanks for the pointer on snapshot(), that's neat :)

@DouweM DouweM enabled auto-merge (squash) November 10, 2025 22:49
@DouweM DouweM merged commit aa1fe33 into pydantic:main Nov 10, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants