Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/google/adk/planners/plan_re_act_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions tests/unittests/planners/test_plan_re_act_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down