mirror of
https://github.com/EvolutionAPI/adk-python.git
synced 2025-12-18 11:22:22 -06:00
fix: Handle non-indexed function call chunks with incremental fallback index
This is in response to the litellm v1.71.2 + ollama v0.9.0 sending function call chunks with 0 indices across multiple calls and lacking call ids. Solutions introduced: 1. increment fallback index when accumulated arg becomes json parsable. 2. tolerate finish reason == stop when tool calls are present 3. fallback to index when tool call id is None Fixes https://github.com/google/adk-python/issues/294 PiperOrigin-RevId: 766258344
This commit is contained in:
committed by
Copybara-Service
parent
bd588bce50
commit
b181cbc8bc
@@ -290,6 +290,105 @@ def mock_response():
|
||||
)
|
||||
|
||||
|
||||
# Test case reflecting litellm v1.71.2, ollama v0.9.0 streaming response
|
||||
# no tool call ids
|
||||
# indices all 0
|
||||
# finish_reason stop instead of tool_calls
|
||||
NON_COMPLIANT_MULTIPLE_FUNCTION_CALLS_STREAM = [
|
||||
ModelResponse(
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
finish_reason=None,
|
||||
delta=Delta(
|
||||
role="assistant",
|
||||
tool_calls=[
|
||||
ChatCompletionDeltaToolCall(
|
||||
type="function",
|
||||
id=None,
|
||||
function=Function(
|
||||
name="function_1",
|
||||
arguments='{"arg": "val',
|
||||
),
|
||||
index=0,
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
]
|
||||
),
|
||||
ModelResponse(
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
finish_reason=None,
|
||||
delta=Delta(
|
||||
role="assistant",
|
||||
tool_calls=[
|
||||
ChatCompletionDeltaToolCall(
|
||||
type="function",
|
||||
id=None,
|
||||
function=Function(
|
||||
name=None,
|
||||
arguments='ue1"}',
|
||||
),
|
||||
index=0,
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
]
|
||||
),
|
||||
ModelResponse(
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
finish_reason=None,
|
||||
delta=Delta(
|
||||
role="assistant",
|
||||
tool_calls=[
|
||||
ChatCompletionDeltaToolCall(
|
||||
type="function",
|
||||
id=None,
|
||||
function=Function(
|
||||
name="function_2",
|
||||
arguments='{"arg": "val',
|
||||
),
|
||||
index=0,
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
]
|
||||
),
|
||||
ModelResponse(
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
finish_reason=None,
|
||||
delta=Delta(
|
||||
role="assistant",
|
||||
tool_calls=[
|
||||
ChatCompletionDeltaToolCall(
|
||||
type="function",
|
||||
id=None,
|
||||
function=Function(
|
||||
name=None,
|
||||
arguments='ue2"}',
|
||||
),
|
||||
index=0,
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
]
|
||||
),
|
||||
ModelResponse(
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
finish_reason="stop",
|
||||
)
|
||||
]
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_acompletion(mock_response):
|
||||
return AsyncMock(return_value=mock_response)
|
||||
@@ -1257,3 +1356,76 @@ async def test_generate_content_async_multiple_function_calls(
|
||||
assert final_response.content.parts[1].function_call.name == "function_2"
|
||||
assert final_response.content.parts[1].function_call.id == "call_2"
|
||||
assert final_response.content.parts[1].function_call.args == {"arg": "value2"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_content_async_non_compliant_multiple_function_calls(
|
||||
mock_completion, lite_llm_instance
|
||||
):
|
||||
"""Test handling of multiple function calls with same 0 indices in streaming mode.
|
||||
|
||||
This test verifies that:
|
||||
1. Multiple function calls with same indices (0) are handled correctly
|
||||
2. Arguments and names are properly accumulated for each function call
|
||||
3. The final response contains all function calls with correct incremented indices
|
||||
"""
|
||||
mock_completion.return_value = NON_COMPLIANT_MULTIPLE_FUNCTION_CALLS_STREAM
|
||||
|
||||
llm_request = LlmRequest(
|
||||
contents=[
|
||||
types.Content(
|
||||
role="user",
|
||||
parts=[types.Part.from_text(text="Test multiple function calls")],
|
||||
)
|
||||
],
|
||||
config=types.GenerateContentConfig(
|
||||
tools=[
|
||||
types.Tool(
|
||||
function_declarations=[
|
||||
types.FunctionDeclaration(
|
||||
name="function_1",
|
||||
description="First test function",
|
||||
parameters=types.Schema(
|
||||
type=types.Type.OBJECT,
|
||||
properties={
|
||||
"arg": types.Schema(type=types.Type.STRING),
|
||||
},
|
||||
),
|
||||
),
|
||||
types.FunctionDeclaration(
|
||||
name="function_2",
|
||||
description="Second test function",
|
||||
parameters=types.Schema(
|
||||
type=types.Type.OBJECT,
|
||||
properties={
|
||||
"arg": types.Schema(type=types.Type.STRING),
|
||||
},
|
||||
),
|
||||
),
|
||||
]
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
|
||||
responses = []
|
||||
async for response in lite_llm_instance.generate_content_async(
|
||||
llm_request, stream=True
|
||||
):
|
||||
responses.append(response)
|
||||
|
||||
# Verify we got the final response with both function calls
|
||||
assert len(responses) > 0
|
||||
final_response = responses[-1]
|
||||
assert final_response.content.role == "model"
|
||||
assert len(final_response.content.parts) == 2
|
||||
|
||||
# Verify first function call
|
||||
assert final_response.content.parts[0].function_call.name == "function_1"
|
||||
assert final_response.content.parts[0].function_call.id == "0"
|
||||
assert final_response.content.parts[0].function_call.args == {"arg": "value1"}
|
||||
|
||||
# Verify second function call
|
||||
assert final_response.content.parts[1].function_call.name == "function_2"
|
||||
assert final_response.content.parts[1].function_call.id == "1"
|
||||
assert final_response.content.parts[1].function_call.args == {"arg": "value2"}
|
||||
|
||||
Reference in New Issue
Block a user