Skip to content

Commit 5768269

Browse files
feat: truncate extracted message preview for root traces
Signed-off-by: Patrick Chin <[email protected]>
1 parent 01644fa commit 5768269

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

packages/nvidia_nat_weave/src/nat/plugins/weave/weave_exporter.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from nat.observability.exporter.base_exporter import IsolatedAttribute
2424
from nat.observability.exporter.span_exporter import SpanExporter
2525
from nat.utils.log_utils import LogFilter
26+
from nat.utils.string_utils import truncate_string
2627
from nat.utils.type_utils import override
2728
from weave.trace.context import weave_client_context
2829
from weave.trace.context.call_context import get_current_call
@@ -198,7 +199,7 @@ def _extract_output_message(self, output_data: Any, outputs: dict[str, Any]) ->
198199
# Handle direct "choices" attribute (non-streaming: output.choices[0].message.content)
199200
choices = getattr(output_data, 'choices', None)
200201
if choices:
201-
outputs["output_message"] = choices[0].message.content
202+
outputs["output_message"] = truncate_string(choices[0].message.content)
202203
return
203204

204205
# Handle list-based output (streaming or websocket) – content may be in the following formats:
@@ -214,13 +215,13 @@ def _extract_output_message(self, output_data: Any, outputs: dict[str, Any]) ->
214215
delta = getattr(choices[0], 'delta', None)
215216

216217
if message:
217-
outputs["output_message"] = getattr(message, 'content', None)
218+
outputs["output_message"] = truncate_string(getattr(message, 'content', None))
218219
elif delta:
219-
outputs["output_preview"] = getattr(delta, 'content', None)
220+
outputs["output_preview"] = truncate_string(getattr(delta, 'content', None))
220221
else:
221222
value = getattr(output_data[0], 'value', None)
222223
if value:
223-
outputs["output_preview"] = value
224+
outputs["output_preview"] = truncate_string(str(value))
224225

225226
def _finish_weave_call(self, step: IntermediateStep) -> None:
226227
"""

src/nat/utils/string_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,19 @@ def convert_to_str(value: Any) -> str:
3636
return str(value)
3737
else:
3838
raise ValueError(f"Unsupported type for conversion to string: {type(value)}")
39+
40+
41+
def truncate_string(text: str | None, max_length: int = 100) -> str | None:
42+
"""
43+
Truncate a string to a maximum length, adding ellipsis if truncated.
44+
45+
Args:
46+
text: The text to truncate (can be None)
47+
max_length: Maximum allowed length (default: 100)
48+
49+
Returns:
50+
The truncated text with ellipsis if needed, or None if input was None
51+
"""
52+
if not text or len(text) <= max_length:
53+
return text
54+
return text[:max_length - 3] + "..."

0 commit comments

Comments
 (0)