Repository navigation
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
Conversation
…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.
alexcastgonz (pcbeingused333)
temporarily deployed
to
github-app-auth
November 5, 2026 15:19 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
🟡 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #14723
Root cause: the auto function-calling loops in
ChatCompletionClientBase(streaming and non-streaming) invoke everyFunctionCallContentwithout looking atfinish_reason. When the model hitsmax_tokensright 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_reasonislength, 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.pygoes through the realOpenAIChatCompletionwith anhttpx.MockTransport, for both paths: empty arguments +lengthis skipped (red before the change); arguments +lengthand empty arguments +tool_callsstill run.Disclosure: this change was prepared with AI assistance (Claude) and reviewed before submission.