Skip to content

Commit 31d7e68

Browse files
committed
Support non-auto tool_choice
Non-auto tool choices will result in a finish_reason of "stop" rather than "tool_calls". The current code assumes "stop" means there are no tool calls to be made when this isn't true. Instead, it should simply check for any available tool calls.
1 parent dd43d73 commit 31d7e68

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

mcp_bridge/openai_clients/chatCompletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def chat_completions(
5050
request.messages.append(msg)
5151

5252
logger.debug(f"finish reason: {response.choices[0].finish_reason}")
53-
if response.choices[0].finish_reason.value in ["stop", "length"]:
53+
if response.choices[0].finish_reason == "length" or not response.choices[0].message.tool_calls:
5454
logger.debug("no tool calls found")
5555
return response
5656

mcp_bridge/openai_clients/streamChatCompletion.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ async def chat_completions(request: CreateChatCompletionRequest, http_request: R
5454

5555
last: Optional[CreateChatCompletionStreamResponse] = None # last message
5656

57+
tool_call: bool = False
5758
tool_call_name: str = ""
5859
tool_call_json: str = ""
5960
should_forward: bool = True
@@ -125,8 +126,10 @@ async def chat_completions(request: CreateChatCompletionRequest, http_request: R
125126

126127
# this manages the incoming tool call schema
127128
# most of this is assertions to please mypy
128-
if len(parsed_data.choices) > 0 and parsed_data.choices[0].delta.tool_calls is not None:
129+
if len(parsed_data.choices) > 0 and parsed_data.choices[0].delta.tool_calls:
129130
should_forward = False
131+
tool_call = True
132+
130133
assert (
131134
parsed_data.choices[0].delta.tool_calls[0].function is not None
132135
)
@@ -154,14 +157,15 @@ async def chat_completions(request: CreateChatCompletionRequest, http_request: R
154157

155158
# ideally we should check this properly
156159
assert last is not None
157-
if len(last.choices) > 0:
158-
assert last.choices[0].finish_reason is not None
159160

160-
if len(last.choices) > 0 and last.choices[0].finish_reason.value in ["stop", "length"]:
161+
if last.choices[0].finish_reason == "length" or tool_call == False:
161162
logger.debug("no tool calls found")
162163
fully_done = True
163164
continue
164165

166+
# Tool call was found, therefore we aren't done yet
167+
fully_done = False
168+
165169
logger.debug("tool calls found")
166170
logger.debug(
167171
f"{tool_call_name=} {tool_call_json=}"

0 commit comments

Comments
 (0)