Skip to content

Commit 39799f0

Browse files
author
Maxime Brugel
committed
fix: types parsing
1 parent 1cd5230 commit 39799f0

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

‎rlm/core/types.py‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
def _serialize_value(value: Any) -> Any:
1010
"""Convert a value to a JSON-serializable representation."""
11-
if value is None or isinstance(value, bool | int | float | str):
11+
if value is None or isinstance(value, (bool, int, float, str)):
1212
return value
1313
if isinstance(value, ModuleType):
1414
return f"<module '{value.__name__}'>"
15-
if isinstance(value, list | tuple):
15+
if isinstance(value, (list, tuple)):
1616
return [_serialize_value(v) for v in value]
1717
if isinstance(value, dict):
1818
return {str(k): _serialize_value(v) for k, v in value.items()}
@@ -213,20 +213,40 @@ class QueryMetadata:
213213
context_total_length: int
214214
context_type: str
215215

216-
def __init__(self, prompt: str | list[str] | dict[Any, str] | list[dict[Any, str]]):
216+
def __init__(self, prompt: str | list[str] | dict[Any, Any] | list[dict[Any, Any]]):
217217
if isinstance(prompt, str):
218218
self.context_lengths = [len(prompt)]
219219
self.context_type = "str"
220-
elif isinstance(prompt, dict[Any, str]):
221-
self.context_lengths = [len(chunk) for chunk in prompt.values()]
220+
elif isinstance(prompt, dict):
221+
self.context_type = "dict"
222+
self.context_lengths = []
223+
for chunk in prompt.values():
224+
if isinstance(chunk, str):
225+
self.context_lengths.append(len(chunk))
226+
continue
227+
try:
228+
import json
229+
230+
self.context_lengths.append(len(json.dumps(chunk, default=str)))
231+
except Exception:
232+
self.context_lengths.append(len(repr(chunk)))
222233
self.context_type = "dict"
223234
elif isinstance(prompt, list):
224235
self.context_type = "list"
225-
if isinstance(prompt[0], dict):
236+
if len(prompt) == 0:
237+
self.context_lengths = [0]
238+
elif isinstance(prompt[0], dict):
226239
if "content" in prompt[0]:
227-
self.context_lengths = [len(chunk["content"]) for chunk in prompt]
240+
self.context_lengths = [len(str(chunk.get("content", ""))) for chunk in prompt]
228241
else:
229-
self.context_lengths = [len(chunk) for chunk in prompt]
242+
self.context_lengths = []
243+
for chunk in prompt:
244+
try:
245+
import json
246+
247+
self.context_lengths.append(len(json.dumps(chunk, default=str)))
248+
except Exception:
249+
self.context_lengths.append(len(repr(chunk)))
230250
else:
231251
self.context_lengths = [len(chunk) for chunk in prompt]
232252
else:

‎rlm/utils/parsing.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def format_execution_result(result: REPLResult) -> str:
106106
"__doc__",
107107
]:
108108
# Only show simple types or short representations
109-
if isinstance(value, str | int | float | bool | list | dict | tuple):
109+
if isinstance(value, (str, int, float, bool, list, dict, tuple)):
110110
important_vars[key] = ""
111111

112112
if important_vars:

0 commit comments

Comments
 (0)