Commit 0feec27
fix(utils): Fix optional chaining error handling in inject_session_state
The previous implementation incorrectly treated entire paths as optional
if any segment contained '?', violating per-segment optional chaining
semantics. For example, {user.profile?} with missing 'user' would
incorrectly return empty string instead of raising an error.
Root cause: The catch block checked 'if '?' in full_path' to decide
whether to suppress KeyError. This was wrong because _get_nested_value()
already handles optional segments correctly by returning None for
missing optional parts. Any KeyError that propagates means a required
segment was missing and should be fatal.
Fix: Removed the conditional suppression logic. Now all KeyErrors from
_get_nested_value() are properly re-raised, maintaining correct
per-segment optional chaining semantics.
Examples:
- {user?.profile} with user missing → empty string (correct: user? is optional)
- {user.profile?} with user missing → KeyError (correct: user is required)
- {user?.profile?.role} with user missing → empty string (correct: chain stops at user?)
Added test case to verify required parent with optional child raises error.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>1 parent ecf09a6 commit 0feec27
File tree
2 files changed
+20
-9
lines changed- src/google/adk/utils
- tests/unittests/utils
2 files changed
+20
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
| 159 | + | |
| 160 | + | |
168 | 161 | | |
169 | 162 | | |
170 | 163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
391 | 391 | | |
392 | 392 | | |
393 | 393 | | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
394 | 412 | | |
395 | 413 | | |
396 | 414 | | |
| |||
0 commit comments