Skip to content

Commit 026efd3

Browse files
committed
Apply pre-commit fixes; handle structured previous output in LlmAgent accumulation
1 parent e5df038 commit 026efd3

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/google/adk/agents/llm_agent.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,14 @@ def __maybe_save_output_to_state(
972972
previous = ctx.session.state.get(self.output_key, '') or ''
973973
except Exception:
974974
previous = ''
975+
# If the previously stored value is not a string, we cannot safely
976+
# accumulate streamed string fragments by concatenation. In that
977+
# case, fall back to final-only behavior for this key: ignore
978+
# non-final fragments and only save on final responses.
979+
if previous is not None and not isinstance(previous, str):
980+
previous_is_structured = True
981+
else:
982+
previous_is_structured = False
975983
# If accumulation disabled, ignore non-final fragments and save only
976984
# the final fragment as legacy behavior.
977985
if not self.accumulate_output_key:
@@ -993,7 +1001,13 @@ def __maybe_save_output_to_state(
9931001
# then validate and save. Non-final events append current fragment to
9941002
# previous value so it is available to future finalization.
9951003
if event.is_final_response():
996-
combined = (previous or '') + (result or '')
1004+
# If previous held a structured value (dict/list), do not attempt
1005+
# to concatenate; treat the final fragment as the authoritative
1006+
# value (legacy final-only behavior).
1007+
if previous_is_structured:
1008+
combined = result
1009+
else:
1010+
combined = (previous or '') + (result or '')
9971011
if not combined:
9981012
return
9991013
if self.output_schema:
@@ -1077,7 +1091,7 @@ def model_post_init(self, __context: Any) -> None:
10771091
self.tools.append(_SingleTurnAgentTool(sub_agent))
10781092
elif mode == 'task':
10791093
self.tools.append(_TaskAgentTool(sub_agent))
1080-
1094+
10811095
@override
10821096
@classmethod
10831097
@experimental(FeatureName.AGENT_CONFIG)

0 commit comments

Comments
 (0)