From c5a29c6d167a41c1757d3d0455159e8174b0150b Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Fri, 16 Feb 2024 11:34:50 -0800 Subject: [PATCH] Fixes #166. Thanks @piresramon. --- google/generativeai/types/content_types.py | 2 ++ tests/test_content.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/google/generativeai/types/content_types.py b/google/generativeai/types/content_types.py index 1c01a039c..0011619d6 100644 --- a/google/generativeai/types/content_types.py +++ b/google/generativeai/types/content_types.py @@ -93,6 +93,8 @@ class BlobDict(TypedDict): def _convert_dict(d: Mapping) -> glm.Content | glm.Part | glm.Blob: if is_content_dict(d): content = dict(d) + if isinstance(parts := content["parts"], str): + content["parts"] = [parts] content["parts"] = [to_part(part) for part in content["parts"]] return glm.Content(content) elif is_part_dict(d): diff --git a/tests/test_content.py b/tests/test_content.py index c2b38fe1b..2c1253b02 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -137,12 +137,14 @@ def test_strict_to_contents_fails(self, examples): ["glm.Content", [glm.Content(parts=[{"text": "Hello world!"}])]], ["ContentDict", [{"parts": [{"text": "Hello world!"}]}]], ["ContentDict-unwraped", [{"parts": ["Hello world!"]}]], + ["ContentDict+str-part", [{"parts": "Hello world!"}]], ) def test_to_contents(self, example): contents = content_types.to_contents(example) part = contents[0].parts[0] self.assertLen(contents, 1) + self.assertLen(contents[0].parts, 1) self.assertIsInstance(part, glm.Part) self.assertEqual(part.text, "Hello world!")