Skip to content

Fix 400 Error with Parallel Tool Calls (LiteLLM Adapter) #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

yuri12344
Copy link

API calls via the LiteLLM adapter failed with a 400 Bad Request when models like OpenAI returned multiple tool_calls (requesting parallel execution). The adapter incorrectly sent only one tool response message back, while the API requires a separate tool message for each requested tool_call_id.

Cause:

The _get_completion_inputs function in lite_llm.py only processed the first function_response part from the ADK's aggregated tool response event, ignoring subsequent ones.

Solution:

Modified _get_completion_inputs to iterate through all function_response parts in the aggregated event. It now correctly generates a distinct ChatCompletionToolMessage (role: 'tool') for each tool_call_id.

Impact:

Resolves the 400 Bad Request error. Ensures correct message formatting for the API when handling parallel tool calls, enabling agent flows with multiple simultaneous tool executions.

When the LLM returns multiple tool calls in a single assistant message,
the OpenAI API requires a subsequent 'tool' message for each
tool_call_id provided.

The previous implementation in `_get_completion_inputs` only processed
the first `function_response` found within an aggregated ADK event
(Content with multiple parts), creating only one 'tool' message.
This resulted in a `400 Bad Request` error from the API.

This commit modifies `_get_completion_inputs` in `lite_llm.py` to
iterate through all parts containing a `function_response` in the
aggregated event and generate a distinct `ChatCompletionToolMessage`
for each one, ensuring API compliance.
Copy link

google-cla bot commented Apr 17, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant