Skip to content

Commit 103ca1a

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 17711c9 commit 103ca1a

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
@@ -842,7 +842,10 @@ def _process_thinking_config(self, generation_kwargs: dict[str, Any]) -> dict[st
842842
thinking_budget = -1
843843

844844
# Create thinking config
845-
thinking_config = types.ThinkingConfig(thinking_budget=thinking_budget, include_thoughts=True)
845+
# When thinking_budget is 0, thinking is disabled so include_thoughts must be False
846+
thinking_config = types.ThinkingConfig(
847+
thinking_budget=thinking_budget, include_thoughts=thinking_budget != 0
848+
)
846849
generation_kwargs["thinking_config"] = thinking_config
847850

848851
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
@@ -1433,23 +1433,27 @@ def test_process_thinking_budget(monkeypatch):
14331433
assert "thinking_budget" not in result
14341434
assert "thinking_config" in result
14351435
assert result["thinking_config"].thinking_budget == 1024
1436+
assert result["thinking_config"].include_thoughts is True
14361437
# Other kwargs should be preserved
14371438
assert result["temperature"] == 0.7
14381439

14391440
# Test dynamic allocation (-1)
14401441
generation_kwargs = {"thinking_budget": -1}
14411442
result = component._process_thinking_config(generation_kwargs.copy())
14421443
assert result["thinking_config"].thinking_budget == -1
1444+
assert result["thinking_config"].include_thoughts is True
14431445

14441446
# Test zero (disable thinking)
14451447
generation_kwargs = {"thinking_budget": 0}
14461448
result = component._process_thinking_config(generation_kwargs.copy())
14471449
assert result["thinking_config"].thinking_budget == 0
1450+
assert result["thinking_config"].include_thoughts is False
14481451

14491452
# Test large value
14501453
generation_kwargs = {"thinking_budget": 24576}
14511454
result = component._process_thinking_config(generation_kwargs.copy())
14521455
assert result["thinking_config"].thinking_budget == 24576
1456+
assert result["thinking_config"].include_thoughts is True
14531457

14541458
# Test when thinking_budget is not present
14551459
generation_kwargs = {"temperature": 0.5}

0 commit comments

Comments
 (0)