From 42df0bc172b51479b530c72ac3b63a1168d394ac Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Tue, 14 Jan 2025 18:08:33 +0000 Subject: [PATCH] Try to_dict method when encoding JSON --- logfire/_internal/json_encoder.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/logfire/_internal/json_encoder.py b/logfire/_internal/json_encoder.py index 0db85a06..8a48518d 100644 --- a/logfire/_internal/json_encoder.py +++ b/logfire/_internal/json_encoder.py @@ -275,6 +275,13 @@ def to_json_value(o: Any, seen: set[int]) -> JsonValue: if isinstance(o, Sequence): return [to_json_value(item, seen) for item in o] # type: ignore + + try: + # Some VertexAI classes have this method. They have no common base class. + # Seems like a sensible thing to try in general. + return to_json_value(o.to_dict(), seen) + except Exception: # currently redundant, but future-proof + pass except Exception: # pragma: no cover pass