-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathutils.py
More file actions
69 lines (50 loc) · 2.27 KB
/
Copy pathutils.py
File metadata and controls
69 lines (50 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import json
from typing import Any
from haystack import logging
logger = logging.getLogger(__name__)
PRIMITIVE_TYPES = (bool, str, int, float)
def coerce_tag_value(value: Any) -> bool | str | int | float:
"""
Coerces span tag values to compatible types for the tracing backend.
Most tracing libraries don't support sending complex types to the backend. Hence, we need to convert them to
compatible types.
:param value: an arbitrary value which should be coerced to a compatible type
:return: the value coerced to a compatible type
"""
if isinstance(value, PRIMITIVE_TYPES):
return value
if value is None:
return ""
try:
# do that with-in try-except because who knows what kind of objects are being passed
serializable = _serializable_value(value=value, use_placeholders=True)
return json.dumps(serializable)
except Exception as error:
logger.debug("Failed to coerce tag value to string: {error}", error=error)
# Our last resort is to convert the value to a string
return str(value)
def _serializable_value(value: Any, use_placeholders: bool = True) -> Any:
"""
Serializes a value into a format suitable for tracing.
One-way only: this is never deserialized, so it's allowed to lose data (e.g. replace large
objects with a placeholder). Don't swap it for `base_serialization`'s schema serializer, which
is built to round-trip exactly.
:param value:
The value to serialize.
:param use_placeholders:
Whether to use string placeholders for large objects like ByteStream and ImageContent.
:returns:
The serialized value.
"""
if isinstance(value, list):
return [_serializable_value(value=v, use_placeholders=use_placeholders) for v in value]
if isinstance(value, dict):
return {k: _serializable_value(value=v, use_placeholders=use_placeholders) for k, v in value.items()}
if use_placeholders and getattr(value, "_to_trace_dict", None):
return _serializable_value(value._to_trace_dict())
if getattr(value, "to_dict", None):
return _serializable_value(value.to_dict())
return value