Skip to content

Conversation

@Jainish-S
Copy link
Contributor

@Jainish-S Jainish-S commented Nov 22, 2025

Related PR: google/adk-python#3673
Related issue: google/adk-python#575

Summary

Documents the new nested state access feature for instruction templates, allowing developers to use dot notation and optional chaining to access nested properties.

What's New

Dot notation: Access nested values with {user.profile.role}
Optional chaining: Safely handle missing values with {user?.profile?.role?}

Example

def inject_nested_state(callback_context: CallbackContext):
    callback_context.state["user"] = {
        "name": "Alice",
        "profile": {"role": "Software Engineer"}
    }

async def build_instruction(readonly_context: ReadonlyContext) -> str:
    template = "User {user.name} has role {user.profile.role}"
    return await instructions_utils.inject_session_state(template, readonly_context)

Changes

  • Added "Accessing Nested State with Dot Notation" section to docs/sessions/state.md
  • Documented syntax, behavior, and error handling
  • Provided Python code examples
  • Updated "Important Considerations" section

This commit adds comprehensive documentation for the nested state access
feature in template injection, which allows developers to access deeply
nested properties using dot notation and optional chaining.

Previously, developers could only access top-level state variables in
instruction templates (e.g., {user}). This enhancement enables accessing
nested properties like {user.profile.role} without needing to flatten
the state structure.

Key features documented:
- Dot notation for accessing nested dictionaries and object attributes
- Optional chaining with ? operator for safe navigation
- Error handling behavior for missing keys
- Support for mixed dictionary and object attribute access

Example usage:
```python
# Define nested state
callback_context.state["user"] = {
    "name": "Alice",
    "profile": {"role": "Software Engineer"}
}

# Use in instruction template
template = "Current user is {user.name} and {user.profile.role}"
```

This documentation supports the implementation in adk-python.

Related issue: google/adk-python#575
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant