Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/google/adk/models/lite_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,9 +1725,20 @@ def _message_to_generate_content_response(
for tool_call in tool_calls:
if tool_call.type == "function":
thought_signature = _extract_thought_signature_from_tool_call(tool_call)
try:
args = json.loads(tool_call.function.arguments or "{}")
except json.JSONDecodeError:
logger.warning(
"Failed to parse tool_call.function.arguments as JSON for"
" tool_call.id=%s, tool_call.function.name=%s: %s",
tool_call.id,
tool_call.function.name,
tool_call.function.arguments,
)
args = {}
part = types.Part.from_function_call(
name=tool_call.function.name,
args=json.loads(tool_call.function.arguments or "{}"),
args=args,
)
part.function_call.id = tool_call.id
if thought_signature:
Expand Down
24 changes: 24 additions & 0 deletions tests/unittests/models/test_litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,30 @@ def test_message_to_generate_content_response_tool_call():
assert response.content.parts[0].function_call.id == "test_tool_call_id"


def test_message_to_generate_content_response_malformed_tool_args_json():
"""Malformed JSON in tool_call.function.arguments should not crash."""
message = ChatCompletionAssistantMessage(
role="assistant",
content=None,
tool_calls=[
ChatCompletionMessageToolCall(
type="function",
id="malformed_call_id",
function=Function(
name="demo_tool",
arguments='{"city":"unterminated',
),
)
],
)

response = _message_to_generate_content_response(message)
assert response.content.role == "model"
assert response.content.parts[0].function_call.name == "demo_tool"
assert response.content.parts[0].function_call.args == {}
assert response.content.parts[0].function_call.id == "malformed_call_id"


def test_message_to_generate_content_response_inline_tool_call_text():
message = ChatCompletionAssistantMessage(
role="assistant",
Expand Down