| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | from typing import TypeVar |
| 4 | |
| 5 | from vllm.entrypoints.generate.base.protocol import DeltaMessage, FunctionCall |
| 6 | from vllm.entrypoints.openai.chat_completion.protocol import ( |
| 7 | ChatCompletionRequest, |
| 8 | ChatCompletionResponseChoice, |
| 9 | ChatCompletionResponseStreamChoice, |
| 10 | ) |
| 11 | from vllm.entrypoints.openai.responses.protocol import ResponsesRequest |
| 12 | |
| 13 | # Used internally |
| 14 | _ToolCallsContainerT = TypeVar( |
| 15 | "_ToolCallsContainerT", |
| 16 | ChatCompletionResponseChoice, |
| 17 | ChatCompletionResponseStreamChoice, |
| 18 | DeltaMessage, |
| 19 | list[FunctionCall], |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | def maybe_filter_parallel_tool_calls( |
| 24 | item: _ToolCallsContainerT, |
| 25 | request: ChatCompletionRequest | ResponsesRequest, |
| 26 | ) -> _ToolCallsContainerT: |
| 27 | """Filter to first tool call only when parallel_tool_calls is explicitly False.""" |
| 28 | if request.parallel_tool_calls is not False: |
| 29 | return item |
| 30 | |
| 31 | if isinstance(item, list): |
| 32 | return item[:1] |
| 33 | if isinstance(item, ChatCompletionResponseChoice): |
| 34 | if item.message.tool_calls: |
| 35 | item.message.tool_calls = item.message.tool_calls[:1] |
| 36 | return item |
| 37 | |
| 38 | delta = item.delta if isinstance(item, ChatCompletionResponseStreamChoice) else item |
| 39 | if delta.tool_calls: |
| 40 | delta.tool_calls = [ |
| 41 | tool_call for tool_call in delta.tool_calls if tool_call.index == 0 |
| 42 | ] |
| 43 | return item |