Skip to content

Build and run agents you can see, understand and trust.

fix(tracing): serialize pydantic tool results as JSON instead of repr (#3263)

Manuel Ahumada4f575d1a29dc
diff --git a/src/agentscope/middleware/_tracing/_utils.py b/src/agentscope/middleware/_tracing/_utils.py
index b06bbbcb8..244ea7d82 100644
--- a/src/agentscope/middleware/_tracing/_utils.py
+++ b/src/agentscope/middleware/_tracing/_utils.py
@@ -36,7 +36,16 @@ def _to_serializable(
elif isinstance(obj, dict):
res = {str(key): _to_serializable(val) for (key, val) in obj.items()}
- elif isinstance(obj, (Msg, BaseModel)) or is_dataclass(obj):
+ elif isinstance(obj, (Msg, BaseModel)):
+ # Dump the model's fields, so the span carries readable JSON
+ # rather than a Python repr string.
+ try:
+ res = _to_serializable(obj.model_dump())
+ except Exception:
+ # Tracing must not fail because model serialization fails.
+ res = repr(obj)
+
+ elif is_dataclass(obj):
res = repr(obj)
elif inspect.isclass(obj) and issubclass(obj, BaseModel):