Skip to content

Commit 8460180

Browse files
committed
feat(utils): Resolve PR Comments
for the PR google#3673 & commit: 96e2f82
1 parent d257d0b commit 8460180

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import agent
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import logging
2+
3+
from google.adk.agents import Agent
4+
from google.adk.agents.callback_context import CallbackContext
5+
from google.adk.agents.readonly_context import ReadonlyContext
6+
from google.adk.utils.instructions_utils import inject_session_state
7+
8+
9+
def inject_nested_state(callback_context: CallbackContext):
10+
callback_context.state["user"] = {
11+
# "name": "Jainish",
12+
# "profile": {"age": 24, "role": "Software Engineer"},
13+
}
14+
logging.info("State populated with nested user object.")
15+
16+
17+
async def build_instruction(readonly_context: ReadonlyContext) -> str:
18+
print(readonly_context.state)
19+
template = (
20+
"Current user is {{user?.name?}} and {{user?.profile?.role?}}. Please greet"
21+
" them by name and designation."
22+
)
23+
return await inject_session_state(template, readonly_context)
24+
25+
26+
agent = Agent(
27+
name="nested_state_agent",
28+
model="gemini-2.0-flash-lite",
29+
instruction=build_instruction,
30+
before_agent_callback=[inject_nested_state],
31+
)
32+
33+
root_agent = agent

src/google/adk/utils/instructions_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def _get_nested_value(obj: Any, path: str) -> Any:
9393
return None
9494

9595
optional = part.endswith('?')
96-
key = part[:-1] if optional else part
96+
key = part.removesuffix('?')
9797

9898
# Try dictionary access first
9999
if hasattr(current, '__getitem__'):
@@ -127,7 +127,7 @@ async def _replace_match(match) -> str:
127127
var_name = full_path.removeprefix('artifact.')
128128
optional = var_name.endswith('?')
129129
if optional:
130-
var_name = var_name[:-1]
130+
var_name = var_name.removesuffix('?')
131131

132132
if invocation_context.artifact_service is None:
133133
raise ValueError('Artifact service is not initialized.')

0 commit comments

Comments
 (0)