Skip to content

Commit f45f584

Browse files
committed
fix: set include_thoughts=False when thinking_budget is 0
When thinking_budget is set to 0 (disabling thinking), include_thoughts was hardcoded to True, causing a 400 INVALID_ARGUMENT error from the Vertex AI API. Now include_thoughts is set based on whether thinking is actually enabled (thinking_budget != 0). Closes deepset-ai/haystack#2845
1 parent 6c8c1bf commit f45f584

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,10 @@ def _process_thinking_config(generation_kwargs: dict[str, Any]) -> dict[str, Any
901901
thinking_budget = -1
902902

903903
# Create thinking config
904-
thinking_config = types.ThinkingConfig(thinking_budget=thinking_budget, include_thoughts=True)
904+
# When thinking_budget is 0, thinking is disabled so include_thoughts must be False
905+
thinking_config = types.ThinkingConfig(
906+
thinking_budget=thinking_budget, include_thoughts=thinking_budget != 0
907+
)
905908
generation_kwargs["thinking_config"] = thinking_config
906909

907910
if "thinking_level" in generation_kwargs:

integrations/google_genai/tests/test_chat_generator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,23 +1508,27 @@ def test_process_thinking_budget(monkeypatch):
15081508
assert "thinking_budget" not in result
15091509
assert "thinking_config" in result
15101510
assert result["thinking_config"].thinking_budget == 1024
1511+
assert result["thinking_config"].include_thoughts is True
15111512
# Other kwargs should be preserved
15121513
assert result["temperature"] == 0.7
15131514

15141515
# Test dynamic allocation (-1)
15151516
generation_kwargs = {"thinking_budget": -1}
15161517
result = GoogleGenAIChatGenerator._process_thinking_config(generation_kwargs.copy())
15171518
assert result["thinking_config"].thinking_budget == -1
1519+
assert result["thinking_config"].include_thoughts is True
15181520

15191521
# Test zero (disable thinking)
15201522
generation_kwargs = {"thinking_budget": 0}
15211523
result = GoogleGenAIChatGenerator._process_thinking_config(generation_kwargs.copy())
15221524
assert result["thinking_config"].thinking_budget == 0
1525+
assert result["thinking_config"].include_thoughts is False
15231526

15241527
# Test large value
15251528
generation_kwargs = {"thinking_budget": 24576}
15261529
result = GoogleGenAIChatGenerator._process_thinking_config(generation_kwargs.copy())
15271530
assert result["thinking_config"].thinking_budget == 24576
1531+
assert result["thinking_config"].include_thoughts is True
15281532

15291533
# Test when thinking_budget is not present
15301534
generation_kwargs = {"temperature": 0.5}

0 commit comments

Comments
 (0)