mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-06 08:16:43 +00:00
add safe_dumps
This commit is contained in:
parent
6bb963dca2
commit
eec76cbe49
1 changed files with 1 additions and 18 deletions
|
|
@ -42,28 +42,11 @@ def safe_dumps(data: Any, max_depth: int = DEFAULT_MAX_RECURSE_DEPTH) -> str:
|
|||
seen.remove(id(obj))
|
||||
return result
|
||||
else:
|
||||
# Check if object is a Pydantic model and convert to dict
|
||||
if hasattr(obj, "model_dump"):
|
||||
# Pydantic v2
|
||||
try:
|
||||
seen.remove(id(obj))
|
||||
return _serialize(obj.model_dump(), seen, depth)
|
||||
except Exception:
|
||||
pass
|
||||
elif hasattr(obj, "dict"):
|
||||
# Pydantic v1
|
||||
try:
|
||||
seen.remove(id(obj))
|
||||
return _serialize(obj.dict(), seen, depth)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Fall back to string conversion for non-serializable objects.
|
||||
try:
|
||||
seen.remove(id(obj))
|
||||
return str(obj)
|
||||
except Exception:
|
||||
return "Unserializable Object"
|
||||
|
||||
safe_data = _serialize(data, set(), 0)
|
||||
return json.dumps(safe_data, default=str)
|
||||
return json.dumps(safe_data, default=str)
|
||||
Loading…
Add table
Reference in a new issue