Skip to content

Commit ccc3e82

Browse files
committed
Move serialization logic to SessionManager
1 parent ff0dfeb commit ccc3e82

File tree

3 files changed

+51
-30
lines changed

3 files changed

+51
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
66

77

8-
## [0.1.79] - 2026-03-31
8+
## [0.1.79] - 2026-04-01
99
- Added input/output attributes across LLM, traceloop, and custom spans
1010
- Added utility function to explicitly set input/output attributes on the active span
11+
- Move serialization logic to SessionManager
1112

1213

1314
## [0.1.78] - 2026-03-31

netra/__init__.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import atexit
2-
import json
32
import logging
43
import threading
54
from typing import Any, Dict, List, Optional, Set
@@ -389,43 +388,23 @@ def add_conversation(cls, conversation_type: ConversationType, role: str, conten
389388

390389
@classmethod
391390
def set_input(cls, value: Any) -> None:
392-
"""Set the ``input`` attribute on the current active span.
393-
394-
Accepts any value. Dicts and lists are JSON-serialised; primitives are
395-
converted with ``str()``. The result is truncated to
396-
``Config.ATTRIBUTE_MAX_LEN`` characters.
391+
"""
392+
Set the input attribute on the current active span.
397393
398394
Args:
399-
value: The input value to record.
395+
value: The input value to record
400396
"""
401-
try:
402-
if isinstance(value, (dict, list)):
403-
serialized = json.dumps(value, default=str)[: Config.ATTRIBUTE_MAX_LEN]
404-
else:
405-
serialized = str(value)[: Config.ATTRIBUTE_MAX_LEN]
406-
SessionManager.set_attribute_on_active_span("input", serialized)
407-
except Exception:
408-
logger.exception("Netra.set_input: failed to set input attribute")
397+
SessionManager.set_input(value)
409398

410399
@classmethod
411400
def set_output(cls, value: Any) -> None:
412-
"""Set the ``output`` attribute on the current active span.
413-
414-
Accepts any value. Dicts and lists are JSON-serialised; primitives are
415-
converted with ``str()``. The result is truncated to
416-
``Config.ATTRIBUTE_MAX_LEN`` characters.
401+
"""
402+
Set the output attribute on the current active span.
417403
418404
Args:
419-
value: The output value to record.
405+
value: The output value to record
420406
"""
421-
try:
422-
if isinstance(value, (dict, list)):
423-
serialized = json.dumps(value, default=str)[: Config.ATTRIBUTE_MAX_LEN]
424-
else:
425-
serialized = str(value)[: Config.ATTRIBUTE_MAX_LEN]
426-
SessionManager.set_attribute_on_active_span("output", serialized)
427-
except Exception:
428-
logger.exception("Netra.set_output: failed to set output attribute")
407+
SessionManager.set_output(value)
429408

430409
@classmethod
431410
def start_span(

netra/session_manager.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
from datetime import datetime
34
from enum import Enum
@@ -372,6 +373,46 @@ def add_conversation(cls, conversation_type: ConversationType, role: str, conten
372373
except Exception as e:
373374
logger.exception("Failed to add conversation attribute: %s", e)
374375

376+
@classmethod
377+
def set_input(cls, value: Any) -> None:
378+
"""Set the ``input`` attribute on the current active span.
379+
380+
Accepts any value. Dicts and lists are JSON-serialised; primitives are
381+
converted with ``str()``. The result is truncated to
382+
``Config.ATTRIBUTE_MAX_LEN`` characters.
383+
384+
Args:
385+
value: The input value to record.
386+
"""
387+
try:
388+
if isinstance(value, (dict, list)):
389+
serialized = json.dumps(value, default=str)[: Config.ATTRIBUTE_MAX_LEN]
390+
else:
391+
serialized = str(value)[: Config.ATTRIBUTE_MAX_LEN]
392+
cls.set_attribute_on_active_span("input", serialized)
393+
except Exception:
394+
logger.exception("SessionManager.set_input: failed to set input attribute")
395+
396+
@classmethod
397+
def set_output(cls, value: Any) -> None:
398+
"""Set the ``output`` attribute on the current active span.
399+
400+
Accepts any value. Dicts and lists are JSON-serialised; primitives are
401+
converted with ``str()``. The result is truncated to
402+
``Config.ATTRIBUTE_MAX_LEN`` characters.
403+
404+
Args:
405+
value: The output value to record.
406+
"""
407+
try:
408+
if isinstance(value, (dict, list)):
409+
serialized = json.dumps(value, default=str)[: Config.ATTRIBUTE_MAX_LEN]
410+
else:
411+
serialized = str(value)[: Config.ATTRIBUTE_MAX_LEN]
412+
cls.set_attribute_on_active_span("output", serialized)
413+
except Exception:
414+
logger.exception("SessionManager.set_output: failed to set output attribute")
415+
375416
@staticmethod
376417
def set_attribute_on_active_span(attr_key: str, attr_value: Any) -> None:
377418
"""

0 commit comments

Comments
 (0)