Skip to content

feat(callback): expose active lifecycle hook metadata - #6995

Open
kaligautier wants to merge 1 commit into
google:mainfrom
kaligautier:feat/callback-metadata-issue-6979
Open

feat(callback): expose active lifecycle hook metadata#6995
kaligautier wants to merge 1 commit into
google:mainfrom
kaligautier:feat/callback-metadata-issue-6979

Conversation

@kaligautier

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Problem:

Callbacks reused across lifecycle stages cannot determine which hook invoked them from the context they receive.

Solution:

Expose immutable CallbackInvocationInfo through Context.callback_info while a callback is running. The hook value is task-local, so nested or concurrent invocations cannot leak metadata into one another. Both canonical callbacks and plugin callbacks are covered, and the value resets to None outside callback execution.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Results:

  • Callback, plugin, model, tool, agent, and live-flow suites: 247 passed
  • Repository suite excluding the unrelated local Anthropic SDK incompatibility: 13,680 passed; the one sandbox-only loopback networking failure passes when rerun with local networking enabled
  • pre-commit run --from-ref origin/main --to-ref HEAD: passed

Manual End-to-End (E2E) Tests:

Ran an InMemoryRunner with a MockModel and shared before/after model callbacks. The callbacks observed the expected scoped values and the context reset after execution:

[('before', 'before_model'), ('after', 'after_model')]

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The remaining local Anthropic failures are also present without this change and come from the installed SDK version lacking OutputTokensDetails; this PR does not modify Anthropic code or dependency files.

Expose immutable callback-scoped metadata through Context so shared callbacks can identify the lifecycle hook that invoked them. Keep the value task-local to isolate concurrent invocations.

Closes google#6979

NO_UNIT_GUIDE=This extends the existing Context API and does not introduce a standalone code unit.
@kaligautier
kaligautier force-pushed the feat/callback-metadata-issue-6979 branch from 33c7728 to c861259 Compare September 3, 2026 13:50
@kaligautier

Copy link
Copy Markdown
Contributor Author

Hi @DeanChensj, I’ve rebased the PR onto the latest main and resolved the conflict. It is mergeable again. Thanks!

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.

[Feature Request]: Expose current callback hook metadata in Context

2 participants