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
This commit is contained in:
Selcuk Gun 2025-06-03 17:37:12 -07:00 committed by Copybara-Service
parent 32c5ffa8ca
commit eaee49bc89
2 changed files with 12 additions and 3 deletions

View File

@ -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,

View File

@ -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"