Skip to content

Commit 460cb8c

Browse files
ayushjainnGWeale
authored andcommitted
feat: forward custom_metadata from run requests into the run config
Add a custom_metadata field to the /run and /run_sse request payloads and forward it to the runner via RunConfig.custom_metadata, so callers can attach arbitrary request metadata for a single agent run. Change-Id: I363da000a4bc6545961d17969b9ddc8687445e04
1 parent 2a68c4e commit 460cb8c

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/google/adk/cli/api_server.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ class RunAgentRequest(common.BaseModel):
384384
function_call_event_id: Optional[str] = None
385385
# for resume long-running functions
386386
invocation_id: Optional[str] = None
387+
custom_metadata: Optional[dict[str, Any]] = None
387388

388389

389390
class CreateSessionRequest(common.BaseModel):
@@ -1415,6 +1416,11 @@ async def run_agent(req: RunAgentRequest) -> list[Event]:
14151416
self.current_app_name_ref.value = req.app_name
14161417
runner = await self.get_runner_async(req.app_name)
14171418
_set_telemetry_context_if_needed(runner)
1419+
run_config = (
1420+
RunConfig(custom_metadata=req.custom_metadata)
1421+
if req.custom_metadata
1422+
else None
1423+
)
14181424
try:
14191425
async with Aclosing(
14201426
runner.run_async(
@@ -1423,6 +1429,7 @@ async def run_agent(req: RunAgentRequest) -> list[Event]:
14231429
new_message=req.new_message,
14241430
state_delta=req.state_delta,
14251431
invocation_id=req.invocation_id,
1432+
run_config=run_config,
14261433
)
14271434
) as agen:
14281435
events = [event async for event in agen]
@@ -1472,7 +1479,10 @@ async def event_generator():
14721479
session_id=req.session_id,
14731480
new_message=req.new_message,
14741481
state_delta=req.state_delta,
1475-
run_config=RunConfig(streaming_mode=stream_mode),
1482+
run_config=RunConfig(
1483+
streaming_mode=stream_mode,
1484+
custom_metadata=req.custom_metadata,
1485+
),
14761486
invocation_id=req.invocation_id,
14771487
)
14781488
) as agen:

tests/unittests/cli/test_fast_api.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,45 @@ async def run_async_capture(
13811381
assert captured_invocation_id["invocation_id"] == payload["invocation_id"]
13821382

13831383

1384+
def test_agent_run_passes_custom_metadata(
1385+
test_app, create_test_session, monkeypatch
1386+
):
1387+
"""Test /run forwards custom_metadata via the run config."""
1388+
info = create_test_session
1389+
captured: dict[str, Optional[RunConfig]] = {"run_config": None}
1390+
1391+
async def run_async_capture(
1392+
self,
1393+
*,
1394+
user_id: str,
1395+
session_id: str,
1396+
invocation_id: Optional[str] = None,
1397+
new_message: Optional[types.Content] = None,
1398+
state_delta: Optional[dict[str, Any]] = None,
1399+
run_config: Optional[RunConfig] = None,
1400+
):
1401+
del self, user_id, session_id, invocation_id, new_message, state_delta
1402+
captured["run_config"] = run_config
1403+
yield _event_1()
1404+
1405+
monkeypatch.setattr(Runner, "run_async", run_async_capture)
1406+
1407+
payload = {
1408+
"app_name": info["app_name"],
1409+
"user_id": info["user_id"],
1410+
"session_id": info["session_id"],
1411+
"new_message": {"role": "user", "parts": [{"text": "Hello"}]},
1412+
"streaming": False,
1413+
"custom_metadata": {"tenant": "acme", "trace": "abc123"},
1414+
}
1415+
1416+
response = test_app.post("/run", json=payload)
1417+
1418+
assert response.status_code == 200
1419+
assert captured["run_config"] is not None
1420+
assert captured["run_config"].custom_metadata == payload["custom_metadata"]
1421+
1422+
13841423
def test_agent_run_sse_splits_artifact_delta(
13851424
test_app, create_test_session, monkeypatch
13861425
):

0 commit comments

Comments
 (0)