fix(utils): skip null tool_calls when formatting prompts for moderation hooks

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
yucheng 2026-09-19 01:11:02 +00:00
parent c7028761aa
commit 4e6bf1cfe3
2 changed files with 25 additions and 1 deletions

View file

@ -30,7 +30,7 @@ def get_formatted_prompt(
if c["type"] == "text":
prompt += c["text"]
if "tool_calls" in message:
for tool_call in message["tool_calls"]:
for tool_call in message["tool_calls"] or ():
if "function" in tool_call:
function_arguments = tool_call["function"]["arguments"]
prompt += function_arguments

View file

@ -0,0 +1,24 @@
from typing import Final, Literal
import pytest
from litellm.litellm_core_utils.llm_response_utils.get_formatted_prompt import (
get_formatted_prompt,
)
@pytest.mark.parametrize("call_type", ["acompletion", "completion"])
def test_null_tool_calls_are_skipped(call_type: Literal["acompletion", "completion"]) -> None:
data: Final = {
"messages": [
{"role": "user", "content": "ping"},
{"role": "assistant", "content": "pong", "tool_calls": None},
{
"role": "assistant",
"content": None,
"tool_calls": [{"function": {"name": "f", "arguments": '{"x":1}'}}],
},
]
}
assert get_formatted_prompt(data=data, call_type=call_type) == 'pingpong{"x":1}'