Commit 0e263f1
fix(agents): persist
Merge google#4838
- Closes: google#4837
**Problem:**
When `LlmAgent` uses both `output_key` and `before_agent_callback`, and the before-agent callback returns `types.Content`, execution is correctly short-circuited and the response is returned to the user, but `session.state[output_key]` is not updated because the normal `LlmAgent` output-saving path is bypassed.
**Solution:**
Override `_handle_before_agent_callback` in `LlmAgent` to call `__maybe_save_output_to_state()` on any returned event. This keeps the fix scoped to `LlmAgent`, which owns `output_key`, avoids introducing `BaseAgent` changes for subclass-specific behavior, and fixes both `run_async` and `run_live` through the shared callback path.
### Testing Plan
Added a regression test that runs `LlmAgent` with `output_key` and a short-circuiting `before_agent_callback`, then checks that `session.state["result"]` contains the returned text.
**Unit Tests:**
- [X] I have added or updated unit tests for my change.
- [X] All unit tests pass locally.
**Manual End-to-End (E2E) Tests:**
Run repro code in google#4837
In that example, `session.state["result"]` should equal "cached answer"
### Checklist
- [X] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document.
- [X] I have performed a self-review of my own code.
- [X] I have commented my code, particularly in hard-to-understand areas.
- [X] I have added tests that prove my fix is effective or that my feature works.
- [X] New and existing unit tests pass locally with my changes.
- [X] I have manually tested my changes end-to-end.
- [X] Any dependent changes have been merged and published in downstream modules.
Co-authored-by: Liang Wu <wuliang@google.com>
COPYBARA_INTEGRATE_REVIEW=google#4838 from gautamvarmadatla:fix/output-key-before-agent-callback 7f084e0
PiperOrigin-RevId: 934022327output_key when before_agent_callback short-circuits LlmAgent1 parent 1c7255b commit 0e263f1
2 files changed
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
491 | 491 | | |
492 | 492 | | |
493 | 493 | | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
494 | 503 | | |
495 | 504 | | |
496 | 505 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| 28 | + | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
| |||
277 | 280 | | |
278 | 281 | | |
279 | 282 | | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
280 | 304 | | |
281 | 305 | | |
282 | 306 | | |
| |||
0 commit comments