Skip to content

Commit 921ae68

Browse files
shawn-yang-googlecopybara-github
authored andcommitted
chore: Exclude thought_signature from ADK event outputs
PiperOrigin-RevId: 781686120
1 parent 116a0a6 commit 921ae68

3 files changed

Lines changed: 50 additions & 5 deletions

File tree

tests/unit/vertex_adk/test_reasoning_engine_templates_adk.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def run(self, *args, **kwargs):
102102
"content": {
103103
"parts": [
104104
{
105+
"thought_signature": b"test_signature",
105106
"function_call": {
106107
"args": {
107108
"currency_date": "2025-04-03",
@@ -110,7 +111,7 @@ def run(self, *args, **kwargs):
110111
},
111112
"id": "af-c5a57692-9177-4091-a3df-098f834ee849",
112113
"name": "get_exchange_rate",
113-
}
114+
},
114115
}
115116
],
116117
"role": "model",
@@ -129,6 +130,7 @@ async def run_async(self, *args, **kwargs):
129130
"content": {
130131
"parts": [
131132
{
133+
"thought_signature": b"test_signature",
132134
"function_call": {
133135
"args": {
134136
"currency_date": "2025-04-03",
@@ -137,7 +139,7 @@ async def run_async(self, *args, **kwargs):
137139
},
138140
"id": "af-c5a57692-9177-4091-a3df-098f834ee849",
139141
"name": "get_exchange_rate",
140-
}
142+
},
141143
}
142144
],
143145
"role": "model",
@@ -400,6 +402,30 @@ def test_enable_tracing_warning(self, caplog):
400402
# assert "enable_tracing=True but proceeding with tracing disabled" in caplog.text
401403

402404

405+
def test_dump_event_for_json():
406+
from google.adk.events import event
407+
408+
test_event = event.Event(
409+
**{
410+
"author": "test_agent",
411+
"content": {
412+
"parts": [
413+
{
414+
"thought_signature": b"test_signature",
415+
"text": "This is a test",
416+
}
417+
],
418+
"role": "model",
419+
},
420+
"id": "test_id",
421+
"invocation_id": "test_invocation_id",
422+
}
423+
)
424+
dumped_event = _utils.dump_event_for_json(test_event)
425+
assert "thought_signature" not in dumped_event["content"]["parts"][0]
426+
assert "text" in dumped_event["content"]["parts"][0]
427+
428+
403429
@pytest.mark.usefixtures("mock_adk_version")
404430
class TestAdkAppErrors:
405431
def test_raise_get_session_not_found_error(self):

vertexai/agent_engines/_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@
9191
except ImportError:
9292
AutogenRunResponse = Any
9393

94+
try:
95+
import pydantic
96+
97+
BaseModel = pydantic.BaseModel
98+
except ImportError:
99+
BaseModel = Any
100+
94101
JsonDict = Dict[str, Any]
95102

96103

@@ -610,6 +617,14 @@ def is_noop_or_proxy_tracer_provider(tracer_provider) -> bool:
610617
return isinstance(tracer_provider, (NoOpTracerProvider, ProxyTracerProvider))
611618

612619

620+
def dump_event_for_json(event: BaseModel) -> Dict[str, Any]:
621+
"""Dumps an ADK event to a JSON-serializable dictionary."""
622+
return event.model_dump(
623+
exclude_none=True,
624+
exclude={"content": {"parts": {"__all__": {"thought_signature"}}}},
625+
)
626+
627+
613628
def _import_cloud_storage_or_raise() -> types.ModuleType:
614629
"""Tries to import the Cloud Storage module."""
615630
try:

vertexai/preview/reasoning_engines/templates/adk.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ def __init__(self, **kwargs):
181181
# List of artifacts belonging to the session.
182182

183183
def dump(self) -> Dict[str, Any]:
184+
from vertexai.agent_engines import _utils
185+
184186
result = {}
185187
if self.events:
186188
result["events"] = []
187189
for event in self.events:
188-
event_dict = event.model_dump(exclude_none=True)
190+
event_dict = _utils.dump_event_for_json(event)
189191
event_dict["invocation_id"] = event_dict.get("invocation_id", "")
190192
result["events"].append(event_dict)
191193
if self.artifacts:
@@ -556,6 +558,7 @@ def stream_query(
556558
Yields:
557559
The output of querying the ADK application.
558560
"""
561+
from vertexai.agent_engines import _utils
559562
from google.genai import types
560563

561564
if isinstance(message, Dict):
@@ -576,7 +579,7 @@ def stream_query(
576579
for event in self._tmpl_attrs.get("runner").run(
577580
user_id=user_id, session_id=session_id, new_message=content, **kwargs
578581
):
579-
yield event.model_dump(exclude_none=True)
582+
yield _utils.dump_event_for_json(event)
580583

581584
async def async_stream_query(
582585
self,
@@ -603,6 +606,7 @@ async def async_stream_query(
603606
Yields:
604607
Event dictionaries asynchronously.
605608
"""
609+
from vertexai.agent_engines import _utils
606610
from google.genai import types
607611

608612
if isinstance(message, Dict):
@@ -627,7 +631,7 @@ async def async_stream_query(
627631

628632
async for event in events_async:
629633
# Yield the event data as a dictionary
630-
yield event.model_dump(exclude_none=True)
634+
yield _utils.dump_event_for_json(event)
631635

632636
def streaming_agent_run_with_events(self, request_json: str):
633637
import json

0 commit comments

Comments
 (0)