fix: handle None content in _get_content_as_str

When content is None (e.g. assistant messages with only tool_calls),
the function previously fell through to str(None) producing the
literal string 'None'. Now returns empty string for None content.

Added test case for this scenario.
This commit is contained in:
voidborne-d 2026-03-17 08:07:21 +00:00
parent e46501494f
commit 1970157d93
2 changed files with 22 additions and 2 deletions

View file

@ -86,8 +86,10 @@ DEFAULT_ASSISTANT_CONTINUE_MESSAGE = ChatCompletionAssistantMessage(
) # similar to autogen. Only used if `litellm.modify_params=True`.
def _get_content_as_str(content: Union[str, list]) -> str:
"""Extract text from content that may be a string or a list of content blocks."""
def _get_content_as_str(content: Union[str, list, None]) -> str:
"""Extract text from content that may be a string, a list of content blocks, or None."""
if content is None:
return ""
if isinstance(content, str):
return content
if isinstance(content, list):

View file

@ -110,6 +110,24 @@ def test_supports_system_message_list_content_last_message():
assert new_messages[0]["content"] == "Only system"
def test_supports_system_message_none_content():
"""
Test map_system_message_pt when next message has content=None (e.g. assistant
tool-call messages). Should not produce the literal string 'None'.
"""
messages = [
{"role": "system", "content": "Be helpful."},
{"role": "assistant", "content": None, "tool_calls": [{"id": "1", "type": "function", "function": {"name": "f", "arguments": "{}"}}]},
]
new_messages = map_system_message_pt(messages=messages)
assert len(new_messages) == 1
# content should start with system text, not contain literal "None"
assert "None" not in new_messages[0]["content"]
assert "Be helpful." in new_messages[0]["content"]
@pytest.mark.parametrize(
"stop_sequence, expected_count", [("\n", 0), (["\n"], 0), (["finish_reason"], 1)]
)