Skip to content

Commit 99bd851

Browse files
giulio-leoneCopilot
andcommitted
fix(telemetry): serialize Pydantic BaseModel in _safe_json_serialize
_safe_json_serialize replaces Pydantic BaseModel instances with '<not serializable>' because json.dumps cannot handle them natively. Any tool that returns a Pydantic model has its traced output lost. Replace the generic lambda default with a _default function that calls model_dump(mode='json') for BaseModel instances before falling back to '<not serializable>' for truly non-serializable objects. BaseModel is already imported in tracing.py. Fixes #4629 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8ddddc0 commit 99bd851

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/google/adk/telemetry/tracing.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,14 @@ def _safe_json_serialize(obj) -> str:
116116
The JSON-serialized object string or <non-serializable> if the object cannot be serialized.
117117
"""
118118

119+
def _default(o: Any) -> Any:
120+
if isinstance(o, BaseModel):
121+
return o.model_dump(mode='json')
122+
return '<not serializable>'
123+
119124
try:
120125
# Try direct JSON serialization first
121-
return json.dumps(
122-
obj, ensure_ascii=False, default=lambda o: '<not serializable>'
123-
)
126+
return json.dumps(obj, ensure_ascii=False, default=_default)
124127
except (TypeError, OverflowError):
125128
return '<not serializable>'
126129

0 commit comments

Comments
 (0)