Skip to content

Python: skip tool calls with no arguments when the reply is cut off by the length limit - #14727

Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/skip-tool-call-empty-args-length
Open

alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/skip-tool-call-empty-args-length

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Fixes #14723

Root cause: the auto function-calling loops in ChatCompletionClientBase (streaming and non-streaming) invoke every FunctionCallContent without looking at finish_reason. When the model hits max_tokens right after the tool name, the arguments are empty and a function whose parameters are all optional runs with its defaults.

Fix: a small helper drops function calls with no arguments when finish_reason is length, before they are added to the history or invoked. If no call is left, the loop returns the reply as it does for a reply without tool calls. Calls with any arguments (truncated or not), and replies not cut off, behave as before.

Tests: new test_openai_chat_completion_truncated_tool_call.py goes through the real OpenAIChatCompletion with an httpx.MockTransport, for both paths: empty arguments + length is skipped (red before the change); arguments + length and empty arguments + tool_calls still run.

Disclosure: this change was prepared with AI assistance (Claude) and reviewed before submission.

…y the length limit

With automatic function calling, a tool call whose arguments were never written because the model hit max_tokens ran with its default parameter values. Drop such calls (empty arguments with finish_reason=length) in both the streaming and non-streaming loops; calls with any arguments, and replies that were not cut off, behave as before.
Copilot AI balanced review requested due to automatic review settings November 5, 2026 15:19
@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Nov 5, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Argument filtering still drops valid empty-object calls and can incorrectly invoke tools with defaults after aggregating empty argument deltas when a stream reaches the length limit.

2 open issues
What changed in this PR

This PR addresses #14723 by filtering unwritten tool calls from Python’s automatic function-calling loops when a response reaches the token limit, preventing unintended execution with default arguments.

Changes:

  • Adds shared filtering before history insertion and invocation in streaming and non-streaming paths.
  • Adds mocked OpenAI connector tests for skipped calls and compatibility cases.
File Description
python/​tests/​unit/​connectors/​ai/​open_ai/​services/​test_openai_chat_completion_truncated_tool_call.py Adds six regression tests covering both response paths.
python/​semantic_kernel/​connectors/​ai/​chat_completion_client_base.py Filters empty-argument calls in length-limited replies and exits when none remain.

Review effort: Balanced

Comment on lines +190 to +194
unwritten = [call for call in function_calls if not call.arguments]
if unwritten:
logger.warning("Skipping tool calls with no arguments because the reply was cut off by the length limit.")
message.items = [item for item in message.items if not any(item is call for call in unwritten)]
return [call for call in function_calls if call.arguments]
# content and others
full_completion: StreamingChatMessageContent = reduce(lambda x, y: x + y, all_messages)
function_calls = [item for item in full_completion.items if isinstance(item, FunctionCallContent)]
function_calls = self._drop_unwritten_function_calls(full_completion)

This branch was previously deployed

1 inactive deployment
github-app-auth — 6aee7b65 Deployed Nov 5, 2026 by pcbeingused333 via add_label #29604
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: a tool runs with its defaults when the reply is cut off before any argument is written

2 participants