From d70953315bef618dae5f3011a64c79f8b133d27e Mon Sep 17 00:00:00 2001 From: NIZAMUDHEEN_T_I <33263597+nizamudheenti@users.noreply.github.com> Date: Sat, 5 Dec 2026 15:14:55 +0530 Subject: [PATCH] fix(planners): handle leading whitespace before PlanReActPlanner tags --- .../adk/planners/plan_re_act_planner.py | 4 ++- .../planners/test_plan_re_act_planner.py | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/google/adk/planners/plan_re_act_planner.py b/src/google/adk/planners/plan_re_act_planner.py index f0d00fe997b..68d4988e632 100644 --- a/src/google/adk/planners/plan_re_act_planner.py +++ b/src/google/adk/planners/plan_re_act_planner.py @@ -154,7 +154,9 @@ def _handle_non_function_call_parts( response_text = response_part.text or '' # If the part is a text part with a leading planning/reasoning/action tag, # label it as reasoning and strip all tags to produce clean text. - if response_text and response_text.startswith(self._PLANNING_TAGS): + if response_text and response_text.lstrip().startswith( + self._PLANNING_TAGS + ): response_part.text = self._strip_planning_tags(response_text) self._mark_as_thought(response_part) preserved_parts.append(response_part) diff --git a/tests/unittests/planners/test_plan_re_act_planner.py b/tests/unittests/planners/test_plan_re_act_planner.py index 01830433199..d299a2645e5 100644 --- a/tests/unittests/planners/test_plan_re_act_planner.py +++ b/tests/unittests/planners/test_plan_re_act_planner.py @@ -126,6 +126,33 @@ def test_part_without_leading_tag_not_marked_as_thought(): assert result[0].text == "Here is the answer /*PLANNING*/ with stray tag." +def test_leading_whitespace_before_planning_tag_is_marked_as_thought(): + """Leading whitespace/newlines before a planning tag must not leak raw tags.""" + planner = PlanReActPlanner() + response_parts = [ + types.Part( + text="\n /*PLANNING*/\n1. Look up weather.\n/*ACTION*/", + thought_signature=b"sig1", + ), + types.Part(text=" \t/*REASONING*/Checking forecast."), + types.Part.from_function_call(name="get_weather", args={"city": "SF"}), + ] + + result = planner.process_planning_response( + callback_context=None, response_parts=response_parts + ) + + text_parts = [p for p in result if p.text] + assert len(text_parts) == 2 + for p in text_parts: + assert p.thought is True + assert "/*PLANNING*/" not in p.text + assert "/*REASONING*/" not in p.text + assert "/*ACTION*/" not in p.text + assert text_parts[0].thought_signature == b"sig1" + assert _function_call_names(result) == ["get_weather"] + + def test_bare_tag_part_is_marked_as_thought(): """A part containing only planning tags is kept, stripped, and marked as thought.""" planner = PlanReActPlanner()