Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion skyvern/forge/skyvern_json_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ def _encode_value(self, value: Any) -> Any:
@classmethod
def dumps(cls, obj: Any, **kwargs: Any) -> str:
"""Helper method to properly encode objects to JSON string"""
return json.dumps(obj, cls=cls, **kwargs)
# Directly insert 'cls' into kwargs for performance (avoid re-binding at runtime)
if 'cls' not in kwargs:
kwargs['cls'] = cls
return json.dumps(obj, **kwargs)
15 changes: 14 additions & 1 deletion skyvern/forge/skyvern_log_encoder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import json
from datetime import datetime
from typing import Any
Expand All @@ -21,7 +22,14 @@ def __init__(self) -> None:

@classmethod
def _format_value(cls, value: Any) -> str:
return SkyvernJSONLogEncoder.dumps(value, sort_keys=True)
# Add simple caching for repeated values. Only hashable values are cached.
try:
# Only cache immutable values, since mutable objects might change between calls
# Values that are not hashable will raise TypeError
return _cached_skyvern_json_dump(value)
except TypeError:
# Fallback for unhashable types (e.g., dict, list)
return SkyvernJSONLogEncoder.dumps(value, sort_keys=True)

@staticmethod
def _parse_json_entry(entry: dict[str, Any]) -> dict[str, Any]:
Expand Down Expand Up @@ -81,3 +89,8 @@ def encode(cls, log_entries: list[dict[str, Any]]) -> str:
formatted_lines.append(encoder.renderer(None, None, error_entry))

return "\n".join(formatted_lines)

@functools.lru_cache(maxsize=128)
def _cached_skyvern_json_dump(value: Any) -> str:
# For immutable/hashable values only
return SkyvernJSONLogEncoder.dumps(value, sort_keys=True)