Skip to content

Commit d0102ec

Browse files
GWealecopybara-github
authored andcommitted
fix: Recognize function responses as non-empty parts in LiteLLM
The `_part_has_payload` helper now returns False for parts containing a function response, preventing the addition of fallback user content when a user turn consists solely of tool outputs. Close #4249 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 863319497
1 parent 63a8eba commit d0102ec

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/google/adk/models/lite_llm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ def _part_has_payload(part: types.Part) -> bool:
497497
return True
498498
if part.file_data and (part.file_data.file_uri or part.file_data.data):
499499
return True
500+
if part.function_response:
501+
return True
500502
return False
501503

502504

tests/unittests/models/test_litellm.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from unittest.mock import Mock
2525
import warnings
2626

27+
from google.adk.models.lite_llm import _append_fallback_user_content_if_missing
2728
from google.adk.models.lite_llm import _content_to_message_param
2829
from google.adk.models.lite_llm import _FILE_ID_REQUIRED_PROVIDERS
2930
from google.adk.models.lite_llm import _FINISH_REASON_MAPPING
@@ -990,6 +991,28 @@ async def test_generate_content_async_adds_fallback_user_message(
990991
)
991992

992993

994+
def test_append_fallback_user_content_ignores_function_response_parts():
995+
llm_request = LlmRequest(
996+
contents=[
997+
types.Content(
998+
role="user",
999+
parts=[
1000+
types.Part.from_function_response(
1001+
name="add", response={"result": 6}
1002+
)
1003+
],
1004+
)
1005+
]
1006+
)
1007+
1008+
_append_fallback_user_content_if_missing(llm_request)
1009+
1010+
assert len(llm_request.contents) == 1
1011+
assert len(llm_request.contents[0].parts) == 1
1012+
assert llm_request.contents[0].parts[0].function_response is not None
1013+
assert llm_request.contents[0].parts[0].text is None
1014+
1015+
9931016
litellm_append_user_content_test_cases = [
9941017
pytest.param(
9951018
LlmRequest(

0 commit comments

Comments
 (0)