|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import datetime |
| 16 | +import io |
15 | 17 | import json |
16 | 18 | import os |
17 | 19 | import unittest |
18 | | -from unittest.mock import patch |
| 20 | +from unittest.mock import Mock, patch |
| 21 | + |
| 22 | +from pydantic import BaseModel |
19 | 23 |
|
20 | 24 | from opentelemetry import trace |
21 | 25 | from opentelemetry.instrumentation._semconv import ( |
|
42 | 46 | OutputMessage, |
43 | 47 | Text, |
44 | 48 | ) |
45 | | -from opentelemetry.util.genai.utils import get_content_capturing_mode |
| 49 | +from opentelemetry.util.genai.utils import ( |
| 50 | + gen_ai_json_dump, |
| 51 | + gen_ai_json_dumps, |
| 52 | + get_content_capturing_mode, |
| 53 | +) |
46 | 54 |
|
47 | 55 |
|
48 | 56 | def patch_env_vars(stability_mode, content_capturing): |
@@ -70,7 +78,9 @@ class TestVersion(unittest.TestCase): |
70 | 78 | stability_mode="gen_ai_latest_experimental", |
71 | 79 | content_capturing="SPAN_ONLY", |
72 | 80 | ) |
73 | | - def test_get_content_capturing_mode_parses_valid_envvar(self): # pylint: disable=no-self-use |
| 81 | + def test_get_content_capturing_mode_parses_valid_envvar( |
| 82 | + self, |
| 83 | + ): # pylint: disable=no-self-use |
74 | 84 | assert get_content_capturing_mode() == ContentCapturingMode.SPAN_ONLY |
75 | 85 |
|
76 | 86 | @patch_env_vars( |
@@ -287,3 +297,68 @@ class BoomError(RuntimeError): |
287 | 297 | assert span.start_time is not None |
288 | 298 | assert span.end_time is not None |
289 | 299 | assert span.end_time >= span.start_time |
| 300 | + |
| 301 | + |
| 302 | +class TestGenAiJson(unittest.TestCase): |
| 303 | + def test_gen_ai_json_dumps(self): |
| 304 | + class Unserializable: |
| 305 | + # pylint: disable=no-self-use |
| 306 | + def __str__(self): |
| 307 | + return "unserializable" |
| 308 | + |
| 309 | + obj = { |
| 310 | + "bytes": b"test", |
| 311 | + "datetime": datetime.datetime(2023, 1, 1, 12, 0, 0), |
| 312 | + "date": datetime.date(2023, 1, 1), |
| 313 | + "unserializable": Unserializable(), |
| 314 | + } |
| 315 | + result = gen_ai_json_dumps(obj) |
| 316 | + expected = '{"bytes":"dGVzdA==","datetime":"2023-01-01T12:00:00","date":"2023-01-01","unserializable":"unserializable"}' |
| 317 | + self.assertEqual(result, expected) |
| 318 | + |
| 319 | + def test_gen_ai_json_dumps_pydantic(self): |
| 320 | + class ExamplePydanticModel(BaseModel): |
| 321 | + datetime_field: datetime.datetime |
| 322 | + string_field: str |
| 323 | + |
| 324 | + pydantic_model = ExamplePydanticModel( |
| 325 | + datetime_field=datetime.datetime(2023, 1, 1, 12, 0, 0), |
| 326 | + string_field="test", |
| 327 | + ) |
| 328 | + # spy the model |
| 329 | + pydantic_model = Mock(wraps=pydantic_model) |
| 330 | + |
| 331 | + result = gen_ai_json_dumps({"some": {"pydantic": [pydantic_model]}}) |
| 332 | + self.assertEqual(result, '{"key":"pydantic"}') |
| 333 | + |
| 334 | + pydantic_model.model_dump_json.assert_called_once() |
| 335 | + |
| 336 | + def test_gen_ai_json_dump(self): |
| 337 | + class Unserializable: |
| 338 | + # pylint: disable=no-self-use |
| 339 | + def __str__(self): |
| 340 | + return "unserializable" |
| 341 | + |
| 342 | + obj = { |
| 343 | + "bytes": b"test", |
| 344 | + "datetime": datetime.datetime(2023, 1, 1, 12, 0, 0), |
| 345 | + "date": datetime.date(2023, 1, 1), |
| 346 | + "unserializable": Unserializable(), |
| 347 | + } |
| 348 | + string_io = io.StringIO() |
| 349 | + gen_ai_json_dump(obj, string_io) |
| 350 | + result = string_io.getvalue() |
| 351 | + expected = '{"bytes":"dGVzdA==","datetime":"2023-01-01T12:00:00","date":"2023-01-01","unserializable":"unserializable"}' |
| 352 | + self.assertEqual(result, expected) |
| 353 | + |
| 354 | + def test_gen_ai_json_dump_pydantic(self): |
| 355 | + class PydanticModel: |
| 356 | + # pylint: disable=no-self-use |
| 357 | + def model_dump_json(self, **kwargs): |
| 358 | + return '{"key":"pydantic"}' |
| 359 | + |
| 360 | + obj = PydanticModel() |
| 361 | + string_io = io.StringIO() |
| 362 | + gen_ai_json_dump(obj, string_io) |
| 363 | + result = string_io.getvalue() |
| 364 | + self.assertEqual(result, '{"key":"pydantic"}') |
0 commit comments