Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mcp_bridge/openai_clients/chatCompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def chat_completions(
request.messages.append(msg)

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

Expand Down
29 changes: 8 additions & 21 deletions mcp_bridge/openai_clients/streamChatCompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ async def chat_completions(request: CreateChatCompletionRequest, http_request: R

# logger.debug(json_data)

last: Optional[CreateChatCompletionStreamResponse] = None # last message

tool_call: bool = False
tool_call_name: str = ""
tool_call_json: str = ""
should_forward: bool = True
Expand Down Expand Up @@ -113,20 +112,16 @@ async def chat_completions(request: CreateChatCompletionRequest, http_request: R
content = content if content is not None else ""
response_content += content

# handle stop reasons
if len(parsed_data.choices) > 0 and parsed_data.choices[0].finish_reason is not None:
if parsed_data.choices[0].finish_reason.value in [
"stop",
"length",
]:
fully_done = True
else:
should_forward = False
# handle stopping for length
if len(parsed_data.choices) > 0 and parsed_data.choices[0].finish_reason == "length":
fully_done = True

# this manages the incoming tool call schema
# most of this is assertions to please mypy
if len(parsed_data.choices) > 0 and parsed_data.choices[0].delta.tool_calls is not None:
if len(parsed_data.choices) > 0 and parsed_data.choices[0].delta.tool_calls:
should_forward = False
tool_call = True

assert (
parsed_data.choices[0].delta.tool_calls[0].function is not None
)
Expand All @@ -149,15 +144,7 @@ async def chat_completions(request: CreateChatCompletionRequest, http_request: R
logger.debug("forwarding message")
yield SSEData.model_validate_json(sse.data).model_dump_json()

# save the last message
last = parsed_data

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

if len(last.choices) > 0 and last.choices[0].finish_reason.value in ["stop", "length"]:
if fully_done or tool_call == False:
logger.debug("no tool calls found")
fully_done = True
continue
Copy link

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use not tool_call instead of tool_call == False for boolean comparison. This is more pythonic and consistent with PEP 8 style guidelines.

Suggested change
continue
if fully_done or not tool_call:
logger.debug("no tool calls found")
fully_done = True
continue

Copilot uses AI. Check for mistakes.

Expand Down