From eaee49bc897c20231ecacde6855cccfa5e80d849 Mon Sep 17 00:00:00 2001 From: Selcuk Gun Date: Tue, 3 Jun 2025 17:37:12 -0700 Subject: [PATCH] fix: Simplify content for ollama provider Even though litellm type definitions and openai API specifies content as list of dictionaries (with type and relevant attributes potentially to allow multimodal inputs/outputs in addition to text), ollama has been demonstrating marshal errors. As a workaround this change simplifies the content as string when there is no more than one content part. Fixes #642, #928, #376 PiperOrigin-RevId: 766890141 --- src/google/adk/models/lite_llm.py | 8 ++++++++ tests/unittests/models/test_litellm.py | 7 ++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/google/adk/models/lite_llm.py b/src/google/adk/models/lite_llm.py index b6c201e..ed54fae 100644 --- a/src/google/adk/models/lite_llm.py +++ b/src/google/adk/models/lite_llm.py @@ -196,6 +196,14 @@ def _content_to_message_param( content_present = True final_content = message_content if content_present else None + if final_content and isinstance(final_content, list): + # when the content is a single text object, we can use it directly. + # this is needed for ollama_chat provider which fails if content is a list + final_content = ( + final_content[0].get("text", "") + if final_content[0].get("type", None) == "text" + else final_content + ) return ChatCompletionAssistantMessage( role=role, diff --git a/tests/unittests/models/test_litellm.py b/tests/unittests/models/test_litellm.py index 1617863..f316e83 100644 --- a/tests/unittests/models/test_litellm.py +++ b/tests/unittests/models/test_litellm.py @@ -889,15 +889,16 @@ def test_content_to_message_param_function_call(): content = types.Content( role="assistant", parts=[ + types.Part.from_text(text="test response"), types.Part.from_function_call( name="test_function", args={"test_arg": "test_value"} - ) + ), ], ) - content.parts[0].function_call.id = "test_tool_call_id" + content.parts[1].function_call.id = "test_tool_call_id" message = _content_to_message_param(content) assert message["role"] == "assistant" - assert message["content"] == None + assert message["content"] == "test response" tool_call = message["tool_calls"][0] assert tool_call["type"] == "function"