Skip to content

Commit 79ba5af

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: propagate custom metadata from RunConfig to InvocationContext
Ensure custom metadata defined in RunConfig is copied to InvocationContext's private custom metadata attribute during model post-initialization. This enables tools to retrieve context-dependent metadata during execution. PiperOrigin-RevId: 951981807
1 parent e6604e1 commit 79ba5af

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/google/adk/agents/invocation_context.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pydantic import ConfigDict
2525
from pydantic import Field
2626
from pydantic import PrivateAttr
27+
from typing_extensions import override
2728

2829
from ..apps._configs import EventsCompactionConfig
2930
from ..apps._configs import ResumabilityConfig
@@ -272,6 +273,12 @@ class InvocationContext(BaseModel):
272273
of this invocation.
273274
"""
274275

276+
@override
277+
def model_post_init(self, __context: Any) -> None:
278+
super().model_post_init(__context)
279+
if self.run_config and self.run_config.custom_metadata:
280+
self._custom_metadata.update(self.run_config.custom_metadata)
281+
275282
@property
276283
def is_resumable(self) -> bool:
277284
"""Returns whether the current invocation is resumable."""

tests/unittests/agents/test_context.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ def test_actions_property(self, mock_invocation_context):
135135

136136
assert context.actions is context._event_actions
137137

138+
def test_custom_metadata_property(self, mock_invocation_context):
139+
"""Test that custom_metadata property delegates to invocation context."""
140+
mock_invocation_context._custom_metadata = {"key": "value"}
141+
context = Context(mock_invocation_context)
142+
assert context.custom_metadata == {"key": "value"}
143+
138144

139145
class TestContextListArtifacts:
140146
"""Test the list_artifacts method in Context."""

tests/unittests/agents/test_invocation_context.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from google.adk.agents.base_agent import BaseAgent
1818
from google.adk.agents.base_agent import BaseAgentState
1919
from google.adk.agents.invocation_context import InvocationContext
20+
from google.adk.agents.run_config import RunConfig
2021
from google.adk.apps import ResumabilityConfig
2122
from google.adk.events.event import Event
2223
from google.adk.events.event_actions import EventActions
@@ -127,6 +128,45 @@ def test_get_events_with_no_matching_events(self, mock_invocation_context):
127128
assert not events
128129

129130

131+
class TestInvocationContextInitialization:
132+
"""Test suite for InvocationContext initialization."""
133+
134+
def test_custom_metadata_propagation(self):
135+
"""Tests that custom_metadata from RunConfig is propagated to InvocationContext."""
136+
run_cfg = RunConfig(custom_metadata={'test_key': 'test_value'})
137+
inv_ctx = InvocationContext(
138+
session_service=Mock(spec=BaseSessionService),
139+
agent=Mock(spec=BaseAgent),
140+
invocation_id='inv_1',
141+
session=Mock(spec=Session, events=[]),
142+
run_config=run_cfg,
143+
)
144+
# Access private attribute to verify
145+
assert inv_ctx._custom_metadata == {'test_key': 'test_value'}
146+
147+
def test_custom_metadata_default_empty(self):
148+
"""Tests that _custom_metadata is empty by default when no RunConfig is provided."""
149+
inv_ctx = InvocationContext(
150+
session_service=Mock(spec=BaseSessionService),
151+
agent=Mock(spec=BaseAgent),
152+
invocation_id='inv_1',
153+
session=Mock(spec=Session, events=[]),
154+
)
155+
assert inv_ctx._custom_metadata == {}
156+
157+
def test_custom_metadata_empty_run_config(self):
158+
"""Tests that _custom_metadata is empty when RunConfig has no custom_metadata."""
159+
run_cfg = RunConfig()
160+
inv_ctx = InvocationContext(
161+
session_service=Mock(spec=BaseSessionService),
162+
agent=Mock(spec=BaseAgent),
163+
invocation_id='inv_1',
164+
session=Mock(spec=Session, events=[]),
165+
run_config=run_cfg,
166+
)
167+
assert inv_ctx._custom_metadata == {}
168+
169+
130170
class TestInvocationContextWithAppResumablity:
131171
"""Test suite for InvocationContext regarding app resumability."""
132172

0 commit comments

Comments
 (0)