<!--
title: sanitize trace metadata
domain: ai-agents
topic: Observability
language: Python
source: langflow-ai/langflow
updated: 2025-08-21
url: https://awesomereviewers.com/reviewers/langflow-sanitize-trace-metadata/
-->

Always sanitize trace metadata and attributes before sending to observability platforms to ensure compatibility with OpenTelemetry attribute type restrictions. Complex objects, custom types, and non-primitive values should be converted to supported types (str, int, float, bool, None) or JSON strings to prevent warnings and ensure reliable trace data.

Implement sanitization methods that:
- Keep primitive types (str, int, float, bool, None) as-is
- Convert lists/tuples by sanitizing each element
- Serialize dicts and complex objects to JSON strings with fallback to str()

Example implementation:
```python
def sanitize_for_otel(value):
    """Sanitize values for OpenTelemetry attribute compatibility."""
    if value is None or isinstance(value, (str, int, float, bool)):
        return value
    elif isinstance(value, (list, tuple)):
        return [sanitize_for_otel(item) for item in value]
    elif isinstance(value, dict):
        try:
            return json.dumps(value)
        except (TypeError, ValueError):
            return str(value)
    else:
        return str(value)

# Usage in trace metadata
span.set_attributes({
    key: sanitize_for_otel(value) 
    for key, value in metadata.items()
})
```

This prevents OpenTelemetry warnings like "Invalid type StructuredTool in attribute" and ensures consistent trace data across different observability platforms (Traceloop, Opik, Arize Phoenix).
