MCPcopy
hub / github.com/microsoft/agent-lightning / sanitize_attribute_value

Function sanitize_attribute_value

agentlightning/utils/otel.py:462–478  ·  view source on GitHub ↗

Sanitize an attribute value to be a valid OpenTelemetry attribute value.

(object: Any, force: bool = True)

Source from the content-addressed store, hash-verified

460
461
462def sanitize_attribute_value(object: Any, force: bool = True) -> AttributeValue:
463 """Sanitize an attribute value to be a valid OpenTelemetry attribute value."""
464 if isinstance(object, (str, int, float, bool)):
465 return object
466
467 if isinstance(object, list):
468 try:
469 return sanitize_list_attribute_sanity(cast(List[Any], object))
470 except ValueError as exc:
471 logger.warning(f"Failed to sanitize list attribute. Fallback to JSON serialization: {exc}")
472
473 try:
474 # This include null, dict, etc.
475 serialized = json.dumps(object, default=str if force else None)
476 except (TypeError, ValueError) as exc:
477 raise ValueError(f"Object must be JSON serializable, got: {type(cast(Any, object))}.") from exc
478 return serialized
479
480
481def sanitize_attributes(attributes: Dict[str, Any], force: bool = True) -> Attributes:

Calls 1