Skip to content

Commit fcea86f

Browse files
GWealecopybara-github
authored andcommitted
chore: Update _flatten_ollama_content return type and add tests
The return type of _flatten_ollama_content is now strictly str | None Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 845963722
1 parent f35d129 commit fcea86f

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/google/adk/models/lite_llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ def _is_ollama_chat_provider(
640640

641641
def _flatten_ollama_content(
642642
content: OpenAIMessageContent | str | None,
643-
) -> str | OpenAIMessageContent | None:
643+
) -> str | None:
644644
"""Flattens multipart content to text for ollama_chat compatibility.
645645
646646
Ollama's chat endpoint rejects arrays for `content`. We keep textual parts,

tests/unittests/models/test_litellm.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,58 @@ def test_flatten_ollama_content_accepts_tuple_blocks():
15601560
assert flattened == "first\nsecond"
15611561

15621562

1563+
@pytest.mark.parametrize(
1564+
"content, expected",
1565+
[
1566+
(None, None),
1567+
("hello", "hello"),
1568+
(
1569+
[
1570+
{"type": "text", "text": "first"},
1571+
{"type": "text", "text": "second"},
1572+
],
1573+
"first\nsecond",
1574+
),
1575+
(
1576+
[
1577+
{"type": "text", "text": "Describe this image."},
1578+
{
1579+
"type": "image_url",
1580+
"image_url": {"url": "http://example.com"},
1581+
},
1582+
],
1583+
"Describe this image.",
1584+
),
1585+
],
1586+
)
1587+
def test_flatten_ollama_content_returns_str_or_none(content, expected):
1588+
from google.adk.models.lite_llm import _flatten_ollama_content
1589+
1590+
flattened = _flatten_ollama_content(content)
1591+
assert flattened == expected
1592+
assert flattened is None or isinstance(flattened, str)
1593+
1594+
1595+
def test_flatten_ollama_content_serializes_non_text_blocks_to_json():
1596+
from google.adk.models.lite_llm import _flatten_ollama_content
1597+
1598+
blocks = [
1599+
{"type": "image_url", "image_url": {"url": "http://example.com"}},
1600+
]
1601+
flattened = _flatten_ollama_content(blocks)
1602+
assert isinstance(flattened, str)
1603+
assert json.loads(flattened) == blocks
1604+
1605+
1606+
def test_flatten_ollama_content_serializes_dict_to_json():
1607+
from google.adk.models.lite_llm import _flatten_ollama_content
1608+
1609+
content = {"type": "image_url", "image_url": {"url": "http://example.com"}}
1610+
flattened = _flatten_ollama_content(content)
1611+
assert isinstance(flattened, str)
1612+
assert json.loads(flattened) == content
1613+
1614+
15631615
@pytest.mark.asyncio
15641616
async def test_content_to_message_param_user_message():
15651617
content = types.Content(

0 commit comments

Comments
 (0)