Commit a4c5c84
committed
fix: filter empty-text parts in GoogleGenAiChatModel to prevent API errors
Fixes #4556
When the Google GenAI API returns a candidate whose parts contain no text
(e.g. thought-signature-only or server-side tool-invocation parts), the
response converter was producing AssistantMessages with empty content.
When these empty messages were stored in chat memory and sent in subsequent
requests, the Google API rejected them with 'missing parts field' errors,
breaking multi-turn conversations with tool calling.
Two fixes applied:
1. responseCandidateToGeneration: add filter on part.text() presence
so only parts with actual text content produce generations.
Uses part.text().get() after the filter guard (no orElse needed).
2. toGeminiContent: filter out Content items whose parts list is empty
before building the API request, so a stale empty-content
AssistantMessage in chat memory cannot cause a 400 error.
Also adds two unit tests in GoogleGenAiChatModelExtendedUsageTests
covering both the empty-part candidate response and the clean text-part
round-trip, so regressions are caught without a live API key.
Signed-off-by: anuragg-saxenaa <anuragg.saxenaa@gmail.com>1 parent 01130c5 commit a4c5c84
File tree
2 files changed
+81
-3
lines changed- models/spring-ai-google-genai/src
- main/java/org/springframework/ai/google/genai
- test/java/org/springframework/ai/google/genai
2 files changed
+81
-3
lines changedLines changed: 8 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
728 | 728 | | |
729 | 729 | | |
730 | 730 | | |
| 731 | + | |
731 | 732 | | |
732 | 733 | | |
733 | 734 | | |
734 | 735 | | |
735 | | - | |
| 736 | + | |
736 | 737 | | |
737 | 738 | | |
738 | 739 | | |
739 | 740 | | |
740 | 741 | | |
741 | 742 | | |
742 | | - | |
743 | | - | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
744 | 748 | | |
745 | 749 | | |
746 | 750 | | |
| |||
1005 | 1009 | | |
1006 | 1010 | | |
1007 | 1011 | | |
| 1012 | + | |
1008 | 1013 | | |
1009 | 1014 | | |
1010 | 1015 | | |
| |||
Lines changed: 73 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
441 | 441 | | |
442 | 442 | | |
443 | 443 | | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
444 | 517 | | |
0 commit comments