Skip to content

Commit a3edf91

Browse files
committed
fix(agents): use getattr for include_contents and consolidate include_sources validation
- contents.py: read agent.include_contents via getattr with 'default' fallback, fixing the mypy union-attr error (agent is typed BaseNode | None) and the AttributeError raised by spec'd Mock agents in tests (pydantic v2 field names are not class attributes, so they are absent from a MagicMock(spec=Agent)). - llm_agent.py: fold the include_sources=[] check into the existing __model_validator_after instead of a separate field_validator. This avoids introducing a new untyped-decorator mypy error rather than suppressing it with type: ignore.
1 parent 2505a8d commit a3edf91

2 files changed

Lines changed: 7 additions & 13 deletions

File tree

src/google/adk/agents/llm_agent.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,10 @@ def __maybe_save_output_to_state(self, event: Event):
978978

979979
@model_validator(mode='after')
980980
def __model_validator_after(self) -> LlmAgent:
981+
if self.include_sources is not None and len(self.include_sources) == 0:
982+
raise ValueError(
983+
'include_sources=[] keeps nothing. Use None to disable filtering.'
984+
)
981985
if self.include_contents == 'none' and self.include_sources is not None:
982986
warnings.warn(
983987
"include_contents='none' with include_sources may produce empty"
@@ -990,17 +994,6 @@ def __model_validator_after(self) -> LlmAgent:
990994
)
991995
return self
992996

993-
@field_validator('include_sources', mode='after') # type: ignore[misc]
994-
@classmethod
995-
def _validate_include_sources(
996-
cls, v: Optional[list[str]]
997-
) -> Optional[list[str]]:
998-
if v is not None and len(v) == 0:
999-
raise ValueError(
1000-
'include_sources=[] keeps nothing. Use None to disable filtering.'
1001-
)
1002-
return v
1003-
1004997
@field_validator('generate_content_config', mode='after')
1005998
@classmethod
1006999
def validate_generate_content_config(

src/google/adk/flows/llm_flows/contents.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ async def run_async(
6969

7070
is_single_turn = getattr(agent, 'mode', None) == 'single_turn'
7171
source_filter = getattr(agent, 'include_sources', None)
72-
if agent.include_contents == 'default':
72+
include_contents = getattr(agent, 'include_contents', 'default')
73+
if include_contents == 'default':
7374
# Include full conversation history
7475
llm_request.contents = _get_contents(
7576
invocation_context.branch,
@@ -85,7 +86,7 @@ async def run_async(
8586
# 'current': anchor at last user message — all sibling agent outputs
8687
# within this invocation are included.
8788
# 'none': anchor at last turn boundary (user OR other-agent event).
88-
stop_at_user_only = agent.include_contents == 'current'
89+
stop_at_user_only = include_contents == 'current'
8990
llm_request.contents = _get_current_turn_contents(
9091
invocation_context.branch,
9192
invocation_context.session.events,

0 commit comments

Comments
 (0)