8000 fix: Simplify content for ollama provider · google/adk-python@eaee49b · GitHub
[go: up one dir, main page]

Skip to content

Commit eaee49b

Browse files
selcukguncopybara-github
authored andcommitted
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
1 parent 32c5ffa commit eaee49b

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/google/adk/models/lite_llm.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ def _content_to_message_param(
196196
content_present = True
197197

198198
final_content = message_content if content_present else None
199+
if final_content and isinstance(final_content, list):
200+
# when the content is a single text object, we can use it directly.
201+
# this is needed for ollama_chat provider which fails if content is a list
202+
final_content = (
203+
final_content[0].get("text", "")
204+
if final_content[0].get("type", None) == "text"
205+
else final_content
206+
)
199207

200208
return ChatCompletionAssistantMessage(
201209
role=role,

tests/unittests/models/test_litellm.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,16 @@ def test_content_to_message_param_function_call():
889889
content = types.Content(
890890
role="assistant",
891891
parts=[
892+
types.Part.from_text(text="test response"),
892893
types.Part.from_function_call(
893894
name="test_function", args={"test_arg": "test_value"}
894-
)
895+
),
895896
],
896897
)
897-
content.parts[0].function_call.id = "test_tool_call_id"
898+
content.parts[1].function_call.id = "test_tool_call_id"
898899
message = _content_to_message_param(content)
899900
assert message["role"] == "assistant"
900-
assert message["content"] == None
901+
assert message["content"] == "test response"
901902

902903
tool_call = message["tool_calls"][0]
903904
assert tool_call["type"] == "function"

0 commit comments

Comments
 (0)
0