Skip to content

Commit d6fe6a7

Browse files
committed
Handle flattening errors more gracefully.
1 parent f583614 commit d6fe6a7

File tree

2 files changed

+29
-12
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-google-genai

2 files changed

+29
-12
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/dict_util.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
15+
import logging
1616
import json
1717
from typing import Any, Callable, Dict, Optional, Sequence, Set, Tuple, Union
1818

@@ -26,6 +26,9 @@
2626
FlattenedDict = Dict[str, FlattenedValue]
2727

2828

29+
_logger = logging.getLogger(__name__)
30+
31+
2932
def _concat_key(prefix: Optional[str], suffix: str):
3033
if not prefix:
3134
return suffix
@@ -136,15 +139,13 @@ def _flatten_compound_value(
136139
flatten_functions=flatten_functions,
137140
)
138141
if _from_json:
139-
raise ValueError(
140-
f"Cannot flatten value with key {key}; value: {value}"
141-
)
142+
_logger.debug("Cannot flatten value with key %s; value: %s", key, value)
143+
return {}
142144
try:
143145
json_string = json.dumps(value)
144-
except TypeError as exc:
145-
raise ValueError(
146-
f"Cannot flatten value with key {key}; value: {value}. Not JSON serializable."
147-
) from exc
146+
except TypeError:
147+
_logger.debug("Cannot flatten value with key %s; value: %s. Not JSON serializable.", key, value)
148+
return {}
148149
json_value = json.loads(json_string)
149150
return _flatten_value(
150151
key,

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/utils/test_dict_util.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import pytest
1615
from pydantic import BaseModel
17-
1816
from opentelemetry.instrumentation.google_genai import dict_util
1917

2018

@@ -209,12 +207,30 @@ def test_flatten_with_mixed_structures():
209207

210208

211209
def test_flatten_with_complex_object_not_json_serializable():
212-
with pytest.raises(ValueError):
213-
dict_util.flatten_dict(
210+
result = dict_util.flatten_dict(
214211
{
215212
"cannot_serialize_directly": NotJsonSerializable(),
216213
}
217214
)
215+
assert result is not None
216+
assert isinstance(result, dict)
217+
assert len(result) == 0
218+
219+
220+
def test_flatten_good_with_non_serializable_complex_object():
221+
result = dict_util.flatten_dict(
222+
{
223+
"foo": {
224+
"bar": "blah",
225+
"baz": 5,
226+
},
227+
"cannot_serialize_directly": NotJsonSerializable(),
228+
}
229+
)
230+
assert result == {
231+
"foo.bar": "blah",
232+
"foo.baz": 5,
233+
}
218234

219235

220236
def test_flatten_with_complex_object_not_json_serializable_and_custom_flatten_func():

0 commit comments

Comments
 (0)