From 8c5db701500f18b3fa5b12f21a0d2d644bf709a3 Mon Sep 17 00:00:00 2001 From: Simin Yuan <281164480+simin-yuan@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:03:34 +0800 Subject: [PATCH 1/5] Python: do not run tool calls whose arguments were never written A reply cut off at the token limit right after the tool name arrives with an empty argument string. When every parameter of the function is optional, the auto-invoke loops run it with the defaults, because they never read finish_reason - although the model was stopped before it wrote the arguments that decide what the tool does. Refuse such a call when finish_reason is LENGTH and the argument string is empty, record a result explaining why, and let the model retry within maximum_auto_invoke_attempts. Arguments that exist, complete or truncated, keep the parsing they have today, and a call whose arguments were finished still runs even if the reply is marked length afterwards. Adds regression tests through the real OpenAIChatCompletion connector, streaming and non-streaming, including both compatibility cases. --- .../ai/chat_completion_client_base.py | 58 +++++- .../connectors/ai/test_cut_off_tool_call.py | 170 ++++++++++++++++++ 2 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 python/tests/unit/connectors/ai/test_cut_off_tool_call.py diff --git a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py index 974d59af92be..68f6b3fa29bb 100644 --- a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py +++ b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py @@ -14,8 +14,12 @@ from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceType from semantic_kernel.const import AUTO_FUNCTION_INVOCATION_SPAN_NAME from semantic_kernel.contents.annotation_content import AnnotationContent +from semantic_kernel.contents.chat_message_content import ChatMessageContent from semantic_kernel.contents.file_reference_content import FileReferenceContent from semantic_kernel.contents.function_call_content import FunctionCallContent +from semantic_kernel.contents.function_result_content import FunctionResultContent +from semantic_kernel.contents.utils.author_role import AuthorRole +from semantic_kernel.contents.utils.finish_reason import FinishReason from semantic_kernel.exceptions.service_exceptions import ServiceInvalidExecutionSettingsError from semantic_kernel.services.ai_service_client_base import AIServiceClientBase from semantic_kernel.utils.telemetry.model_diagnostics.gen_ai_attributes import AVAILABLE_FUNCTIONS @@ -24,13 +28,49 @@ from semantic_kernel.connectors.ai.function_call_choice_configuration import FunctionCallChoiceConfiguration from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings from semantic_kernel.contents.chat_history import ChatHistory - from semantic_kernel.contents.chat_message_content import ChatMessageContent from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent from semantic_kernel.kernel import Kernel logger: logging.Logger = logging.getLogger(__name__) tracer: Tracer = get_tracer(__name__) +# A reply that stops right after the tool name leaves an empty argument string. A function whose parameters +# are all optional is then invoked with its defaults, although the model was cut off before writing the +# arguments that decide what the tool does. Arguments that are present, complete or not, are left to the +# existing parsing; only the empty case is a call the model never started. +CUT_OFF_TOOL_CALL_MESSAGE = ( + "The model's reply was cut off at the token limit before this tool call had complete arguments, " + "so it was not executed. Retry with a smaller request or a larger token budget." +) + + +def _arguments_never_written(finish_reason: "FinishReason | None", function_call: FunctionCallContent) -> bool: + """A cut-off reply that stopped before writing any argument: the tool would run on its defaults.""" + if finish_reason != FinishReason.LENGTH: + return False + raw = function_call.arguments + return raw is None or (isinstance(raw, str) and not raw.strip()) + + +def _record_refused_calls(chat_history: "ChatHistory", function_calls: list[FunctionCallContent]) -> None: + """Record a result per refused call so the model learns why nothing ran, instead of seeing silence.""" + logger.warning( + "The model's reply was cut off at the token limit; %d tool call(s) with incomplete arguments " + "were not executed.", + len(function_calls), + ) + for function_call in function_calls: + chat_history.add_message( + ChatMessageContent( + role=AuthorRole.TOOL, + items=[ + FunctionResultContent( + id=function_call.id, name=function_call.name, result=CUT_OFF_TOOL_CALL_MESSAGE + ) + ], + ) + ) + class ChatCompletionClientBase(AIServiceClientBase, ABC): """Base class for chat completion AI services.""" @@ -146,6 +186,14 @@ async def get_chat_message_contents( # Since we have a function call, add the assistant's tool call message to the history chat_history.add_message(message=completions[0]) + if refused := [ + call for call in function_calls if _arguments_never_written(completions[0].finish_reason, call) + ]: + _record_refused_calls(chat_history, refused) + function_calls = [call for call in function_calls if call not in refused] + if (fc_count := len(function_calls)) == 0: + continue + logger.info(f"processing {fc_count} tool calls in parallel.") # This function either updates the chat history with the function call results @@ -280,7 +328,13 @@ async def get_streaming_chat_message_contents( function_calls = [item for item in full_completion.items if isinstance(item, FunctionCallContent)] chat_history.add_message(message=full_completion) - fc_count = len(function_calls) + if refused := [ + call for call in function_calls if _arguments_never_written(full_completion.finish_reason, call) + ]: + _record_refused_calls(chat_history, refused) + function_calls = [call for call in function_calls if call not in refused] + if (fc_count := len(function_calls)) == 0: + continue logger.info(f"processing {fc_count} tool calls in parallel.") # This function either updates the chat history with the function call results diff --git a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py new file mode 100644 index 000000000000..979d16116577 --- /dev/null +++ b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py @@ -0,0 +1,170 @@ +# Copyright (c) Microsoft. All rights reserved. + +"""A cut-off reply must not run a tool call whose arguments never arrived. + +These tests go through the real `OpenAIChatCompletion` connector: the OpenAI client gets a mock HTTP +transport, so the request and the streamed response are handled exactly as in production. +""" + +import json + +import httpx +import pytest +from openai import AsyncOpenAI + +from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior +from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion +from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings +from semantic_kernel.contents.chat_history import ChatHistory +from semantic_kernel.functions.kernel_function_decorator import kernel_function +from semantic_kernel.kernel import Kernel + +TOOL = "set_mode" + + +class RecordingPlugin: + """`set_mode` has only optional parameters; `do_action` requires one.""" + + def __init__(self) -> None: + self.calls: list[str] = [] + + @kernel_function(name=TOOL, description="set the mode") + def set_mode(self, mode: str = "default") -> str: + self.calls.append(f"set_mode(mode={mode!r})") + return "done" + + @kernel_function(name="do_action", description="do the action") + def do_action(self, target: str) -> str: + self.calls.append(f"do_action(target={target!r})") + return "done" + + +def _chunk(delta: dict, finish: str | None = None) -> str: + return ( + "data: " + + json.dumps({ + "id": "chatcmpl-mock", + "object": "chat.completion.chunk", + "created": 1, + "model": "gpt-4o-mini", + "choices": [{"index": 0, "delta": delta, "finish_reason": finish}], + }) + + "\n\n" + ) + + +def _tool_delta(arguments: str, name: str = TOOL) -> dict: + return { + "role": "assistant", + "tool_calls": [ + {"index": 0, "id": "call_1", "type": "function", "function": {"name": name, "arguments": arguments}} + ], + } + + +def _stream_body(arguments: str, finish: str, name: str = TOOL) -> bytes: + return (_chunk(_tool_delta(arguments, name)) + _chunk({}, finish) + "data: [DONE]\n\n").encode() + + +def _body(arguments: str, finish: str, name: str = TOOL) -> bytes: + return json.dumps({ + "id": "chatcmpl-mock", + "object": "chat.completion", + "created": 1, + "model": "gpt-4o-mini", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": None, + "tool_calls": [ + {"id": "call_1", "type": "function", "function": {"name": name, "arguments": arguments}} + ], + }, + "finish_reason": finish, + } + ], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }).encode() + + +def _service(body: bytes, sse: bool) -> OpenAIChatCompletion: + def handler(request: httpx.Request) -> httpx.Response: + return httpx.Response( + 200, + headers={"content-type": "text/event-stream" if sse else "application/json"}, + content=body, + ) + + client = AsyncOpenAI( + api_key="sk-" + "a" * 48, + base_url="http://mock/v1", + http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)), + ) + return OpenAIChatCompletion(ai_model_id="gpt-4o-mini", async_client=client) + + +def _settings() -> PromptExecutionSettings: + return PromptExecutionSettings( + function_choice_behavior=FunctionChoiceBehavior.Auto(auto_invoke=True, maximum_auto_invoke_attempts=1) + ) + + +def _kernel(plugin: RecordingPlugin) -> Kernel: + kernel = Kernel() + kernel.add_plugin(plugin, plugin_name="record") + return kernel + + +async def _invoke(plugin: RecordingPlugin, service: OpenAIChatCompletion, stream: bool) -> None: + history = ChatHistory() + history.add_user_message("do it") + if stream: + async for _ in service.get_streaming_chat_message_content(history, _settings(), kernel=_kernel(plugin)): + pass + else: + await service.get_chat_message_contents(history, _settings(), kernel=_kernel(plugin)) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("stream", [False, True]) +async def test_cut_off_reply_with_empty_arguments_does_not_run_the_tool(stream: bool) -> None: + """max_tokens stopped the model right after the tool name, so no parameter was ever written.""" + plugin = RecordingPlugin() + body = _stream_body("", "length") if stream else _body("", "length") + await _invoke(plugin, _service(body, stream), stream) + + assert plugin.calls == [], f"tool ran with defaults: {plugin.calls!r}" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("stream", [False, True]) +async def test_cut_off_reply_with_empty_arguments_does_not_run_a_tool_needing_one(stream: bool) -> None: + plugin = RecordingPlugin() + body = _stream_body("", "length", "do_action") if stream else _body("", "length", "do_action") + await _invoke(plugin, _service(body, stream), stream) + + assert plugin.calls == [] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("stream", [False, True]) +async def test_cut_off_reply_with_complete_arguments_still_runs_the_tool(stream: bool) -> None: + """Compatibility: the model finished writing its arguments and only then hit the limit.""" + plugin = RecordingPlugin() + body = _stream_body('{"mode": "silent"}', "length") if stream else _body('{"mode": "silent"}', "length") + await _invoke(plugin, _service(body, stream), stream) + + assert plugin.calls == ["set_mode(mode='silent')"] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("stream", [False, True]) +async def test_zero_argument_call_in_a_complete_reply_still_runs(stream: bool) -> None: + """Compatibility: an empty argument string is normal for a tool without parameters.""" + plugin = RecordingPlugin() + body = _stream_body("", "tool_calls") if stream else _body("", "tool_calls") + await _invoke(plugin, _service(body, stream), stream) + + assert plugin.calls == ["set_mode(mode='default')"] From f6dcdf4dfb5ee5dfb1e3f5e04ab7d85789153aa5 Mon Sep 17 00:00:00 2001 From: Simin Yuan <281164480+simin-yuan@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:20:40 +0800 Subject: [PATCH 2/5] Python: keep streamed cut-off detection after argument aggregation Three gaps in the previous commit: - Streaming aggregation combines empty argument fragments into "{}", so a reply that stopped right after the tool name looked as if the model had written an empty object and ran. Track the fragments before aggregation: a reply that produced only empty ones is still a reply that wrote nothing, while a model that really wrote {} produces non-empty fragments and keeps running. - The refused calls were recorded in the history but never reached the streaming caller. They are now merged and yielded like any other tool result, and the model sees the explanation on the next auto-invoke attempt. - A connector that reports no argument text at all (None, as the Anthropic connector does for a tool without input) was refused; it is now treated as "nothing was written", which is what it means for a cut-off reply. Adds tests for all three, plus one through ChatCompletionAgent.invoke_stream that checks the refusal reaches the model on the next request. --- .../ai/chat_completion_client_base.py | 67 ++++++++- .../connectors/ai/test_cut_off_tool_call.py | 127 +++++++++++++++--- 2 files changed, 172 insertions(+), 22 deletions(-) diff --git a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py index 68f6b3fa29bb..8d3f874942e7 100644 --- a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py +++ b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py @@ -45,11 +45,44 @@ def _arguments_never_written(finish_reason: "FinishReason | None", function_call: FunctionCallContent) -> bool: - """A cut-off reply that stopped before writing any argument: the tool would run on its defaults.""" + """A cut-off reply that stopped before writing any argument: the tool would run on its defaults. + + Nothing written counts as never written: an empty string, or no argument text at all, which is how the + Anthropic connector reports a tool without input. A mapping is a complete argument object and is left to + the existing parsing. + """ if finish_reason != FinishReason.LENGTH: return False raw = function_call.arguments - return raw is None or (isinstance(raw, str) and not raw.strip()) + if isinstance(raw, str): + return not raw.strip() + return raw is None + + +def _fragment_key(function_call: FunctionCallContent) -> str: + """Stable key for tracking one call's argument fragments across streamed chunks.""" + return function_call.id or f"index-{function_call.index}" + + +def _streamed_arguments_never_written( + finish_reason: "FinishReason | None", + function_call: FunctionCallContent, + fragments: tuple[bool, bool], +) -> bool: + """Same question for a streamed reply, where empty fragments are combined into "{}" by the aggregation. + + `fragments` is `(wrote_text, saw_empty)` collected before aggregation: a model that really wrote `{}` + produces non-empty fragments, a cut-off reply produces only empty ones. + """ + if finish_reason != FinishReason.LENGTH: + return False + wrote_text, saw_empty = fragments + if wrote_text: + return False + raw = function_call.arguments + if raw is not None and not isinstance(raw, str): + return False + return saw_empty or str(raw).strip() in ("", "{}") def _record_refused_calls(chat_history: "ChatHistory", function_calls: list[FunctionCallContent]) -> None: @@ -305,6 +338,7 @@ async def get_streaming_chat_message_contents( for request_index in range(settings.function_choice_behavior.maximum_auto_invoke_attempts): # Hold the messages, if there are more than one response, it will not be used, so we flatten all_messages: list["StreamingChatMessageContent"] = [] + argument_fragments: dict[str, tuple[bool, bool]] = {} function_call_returned = False async for messages in self._inner_get_streaming_chat_message_contents( chat_history, settings, request_index @@ -312,6 +346,19 @@ async def get_streaming_chat_message_contents( for msg in messages: if msg is not None: all_messages.append(msg) + for item in msg.items: + if isinstance(item, FunctionCallContent): + key = _fragment_key(item) + wrote_text, saw_empty = argument_fragments.get(key, (False, False)) + raw_arguments = item.arguments + if isinstance(raw_arguments, str): + if raw_arguments.strip(): + wrote_text = True + else: + saw_empty = True + elif raw_arguments is not None: + wrote_text = True + argument_fragments[key] = (wrote_text, saw_empty) if not function_call_returned and any( isinstance(item, FunctionCallContent) for item in msg.items ): @@ -329,10 +376,24 @@ async def get_streaming_chat_message_contents( chat_history.add_message(message=full_completion) if refused := [ - call for call in function_calls if _arguments_never_written(full_completion.finish_reason, call) + call + for call in function_calls + if _streamed_arguments_never_written( + full_completion.finish_reason, + call, + argument_fragments.get(_fragment_key(call), (False, False)), + ) ]: _record_refused_calls(chat_history, refused) function_calls = [call for call in function_calls if call not in refused] + # The results of refused calls reach the caller like any other tool result + refused_messages = merge_streaming_function_results( + messages=chat_history.messages[-len(refused) :], + ai_model_id=self._get_ai_model_id(settings), # type: ignore + function_invoke_attempt=request_index, + ) + if self._yield_function_result_messages(refused_messages): + yield refused_messages if (fc_count := len(function_calls)) == 0: continue logger.info(f"processing {fc_count} tool calls in parallel.") diff --git a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py index 979d16116577..559210a22504 100644 --- a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py +++ b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py @@ -16,6 +16,7 @@ from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings from semantic_kernel.contents.chat_history import ChatHistory +from semantic_kernel.contents.function_result_content import FunctionResultContent from semantic_kernel.functions.kernel_function_decorator import kernel_function from semantic_kernel.kernel import Kernel @@ -53,20 +54,29 @@ def _chunk(delta: dict, finish: str | None = None) -> str: ) -def _tool_delta(arguments: str, name: str = TOOL) -> dict: +def _tool_delta(arguments: str | None = None, name: str | None = TOOL) -> dict: + function: dict = {} + if name is not None: + function["name"] = name + if arguments is not None: + function["arguments"] = arguments return { "role": "assistant", - "tool_calls": [ - {"index": 0, "id": "call_1", "type": "function", "function": {"name": name, "arguments": arguments}} - ], + "tool_calls": [{"index": 0, "id": "call_1", "type": "function", "function": function}], } -def _stream_body(arguments: str, finish: str, name: str = TOOL) -> bytes: - return (_chunk(_tool_delta(arguments, name)) + _chunk({}, finish) + "data: [DONE]\n\n").encode() +def _stream_body(fragments: list[str], finish: str, name: str | None = TOOL) -> bytes: + """One tool call streamed as several argument fragments, then a chunk carrying the finish reason.""" + body = _chunk(_tool_delta(name=name)) + body += "".join(_chunk(_tool_delta(fragment)) for fragment in fragments) + return (body + _chunk({}, finish) + "data: [DONE]\n\n").encode() -def _body(arguments: str, finish: str, name: str = TOOL) -> bytes: +def _body(arguments: str | None, finish: str, name: str = TOOL) -> bytes: + function: dict = {"name": name} + if arguments is not None: + function["arguments"] = arguments return json.dumps({ "id": "chatcmpl-mock", "object": "chat.completion", @@ -78,9 +88,7 @@ def _body(arguments: str, finish: str, name: str = TOOL) -> bytes: "message": { "role": "assistant", "content": None, - "tool_calls": [ - {"id": "call_1", "type": "function", "function": {"name": name, "arguments": arguments}} - ], + "tool_calls": [{"id": "call_1", "type": "function", "function": function}], }, "finish_reason": finish, } @@ -117,14 +125,16 @@ def _kernel(plugin: RecordingPlugin) -> Kernel: return kernel -async def _invoke(plugin: RecordingPlugin, service: OpenAIChatCompletion, stream: bool) -> None: +async def _invoke(plugin: RecordingPlugin, service: OpenAIChatCompletion, stream: bool) -> list: history = ChatHistory() history.add_user_message("do it") - if stream: - async for _ in service.get_streaming_chat_message_content(history, _settings(), kernel=_kernel(plugin)): - pass - else: + if not stream: await service.get_chat_message_contents(history, _settings(), kernel=_kernel(plugin)) + return [] + yielded = [] + async for message in service.get_streaming_chat_message_content(history, _settings(), kernel=_kernel(plugin)): + yielded.append(message) + return yielded @pytest.mark.asyncio @@ -132,17 +142,35 @@ async def _invoke(plugin: RecordingPlugin, service: OpenAIChatCompletion, stream async def test_cut_off_reply_with_empty_arguments_does_not_run_the_tool(stream: bool) -> None: """max_tokens stopped the model right after the tool name, so no parameter was ever written.""" plugin = RecordingPlugin() - body = _stream_body("", "length") if stream else _body("", "length") + body = _stream_body([""], "length") if stream else _body("", "length") await _invoke(plugin, _service(body, stream), stream) assert plugin.calls == [], f"tool ran with defaults: {plugin.calls!r}" +@pytest.mark.asyncio +async def test_cut_off_reply_with_several_empty_fragments_does_not_run_the_tool() -> None: + """Empty fragments are combined into "{}" by the aggregation, which must not look like real arguments.""" + plugin = RecordingPlugin() + await _invoke(plugin, _service(_stream_body(["", "", ""], "length"), True), True) + + assert plugin.calls == [], f"tool ran with defaults: {plugin.calls!r}" + + +@pytest.mark.asyncio +async def test_cut_off_reply_with_an_explicit_empty_object_still_runs_the_tool() -> None: + """The model really wrote {}, so the call is complete even though the reply was cut off afterwards.""" + plugin = RecordingPlugin() + await _invoke(plugin, _service(_stream_body(["{", "}"], "length"), True), True) + + assert plugin.calls == ["set_mode(mode='default')"] + + @pytest.mark.asyncio @pytest.mark.parametrize("stream", [False, True]) async def test_cut_off_reply_with_empty_arguments_does_not_run_a_tool_needing_one(stream: bool) -> None: plugin = RecordingPlugin() - body = _stream_body("", "length", "do_action") if stream else _body("", "length", "do_action") + body = _stream_body([""], "length", "do_action") if stream else _body("", "length", "do_action") await _invoke(plugin, _service(body, stream), stream) assert plugin.calls == [] @@ -153,7 +181,7 @@ async def test_cut_off_reply_with_empty_arguments_does_not_run_a_tool_needing_on async def test_cut_off_reply_with_complete_arguments_still_runs_the_tool(stream: bool) -> None: """Compatibility: the model finished writing its arguments and only then hit the limit.""" plugin = RecordingPlugin() - body = _stream_body('{"mode": "silent"}', "length") if stream else _body('{"mode": "silent"}', "length") + body = _stream_body(['{"mode": "silent"}'], "length") if stream else _body('{"mode": "silent"}', "length") await _invoke(plugin, _service(body, stream), stream) assert plugin.calls == ["set_mode(mode='silent')"] @@ -164,7 +192,68 @@ async def test_cut_off_reply_with_complete_arguments_still_runs_the_tool(stream: async def test_zero_argument_call_in_a_complete_reply_still_runs(stream: bool) -> None: """Compatibility: an empty argument string is normal for a tool without parameters.""" plugin = RecordingPlugin() - body = _stream_body("", "tool_calls") if stream else _body("", "tool_calls") + body = _stream_body([""], "tool_calls") if stream else _body("", "tool_calls") await _invoke(plugin, _service(body, stream), stream) assert plugin.calls == ["set_mode(mode='default')"] + + +@pytest.mark.asyncio +async def test_missing_arguments_from_the_connector_are_not_run_either() -> None: + """Anthropic reports a tool without input as no argument text; a cut-off reply still must not run it.""" + plugin = RecordingPlugin() + await _invoke(plugin, _service(_body(None, "length"), False), False) + + assert plugin.calls == [] + + +@pytest.mark.asyncio +async def test_refused_streamed_call_reports_its_result_to_the_caller() -> None: + """A refused call must reach the caller like any other tool result, not disappear.""" + plugin = RecordingPlugin() + yielded = await _invoke(plugin, _service(_stream_body([""], "length"), True), True) + + results = [ + item for message in yielded if message for item in message.items if isinstance(item, FunctionResultContent) + ] + assert plugin.calls == [] + assert [r.name for r in results] == [TOOL] + assert "cut off" in str(results[0].result) + + +@pytest.mark.asyncio +async def test_agent_stream_sends_the_refusal_back_to_the_model() -> None: + """The agent filters tool results from the caller stream, so what matters is that the model sees the refusal.""" + from semantic_kernel.agents import ChatCompletionAgent + from semantic_kernel.functions.kernel_arguments import KernelArguments + + requests: list[bytes] = [] + cut_off = _stream_body([""], "length") + follow_up = (_chunk({"role": "assistant", "content": "ok"}) + _chunk({}, "stop") + "data: [DONE]\n\n").encode() + + def handler(request: httpx.Request) -> httpx.Response: + requests.append(request.content) + return httpx.Response( + 200, + headers={"content-type": "text/event-stream"}, + content=cut_off if len(requests) == 1 else follow_up, + ) + + client = AsyncOpenAI( + api_key="sk-" + "a" * 48, + base_url="http://mock/v1", + http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)), + ) + plugin = RecordingPlugin() + agent = ChatCompletionAgent( + kernel=_kernel(plugin), + service=OpenAIChatCompletion(ai_model_id="gpt-4o-mini", async_client=client), + name="probe", + ) + + async for _ in agent.invoke_stream(messages=ChatHistory(), arguments=KernelArguments()): + pass + + assert plugin.calls == [] + assert len(requests) == 2, "the cut-off reply should have been followed by another attempt" + assert "cut off at the token limit" in requests[1].decode() From 612d72c3f6598220cc6f84db706d58e932696841 Mon Sep 17 00:00:00 2001 From: Simin Yuan <281164480+simin-yuan@users.noreply.github.com> Date: Thu, 5 Nov 2026 12:11:11 +0800 Subject: [PATCH 3/5] Python: keep streamed cut-off detection keyed by index The first streamed tool-call chunk carries the call id, every later chunk carries an empty id and only the index. Tracking argument fragments by id filed the text the model did write under a different key than the one looked up when the reply ended, so a complete call that still reported finish_reason=length was refused, and so was a tool whose input really was {}. Key fragments by index, record only the calls that carried argument text, and refuse a call at finish_reason=length when it is not in that set. The Anthropic streaming connector now keeps a complete empty input ({}) instead of reporting it as no arguments at all. --- .../services/anthropic_chat_completion.py | 2 +- .../ai/chat_completion_client_base.py | 79 ++++++------------- .../test_anthropic_chat_completion.py | 21 +++++ .../connectors/ai/test_cut_off_tool_call.py | 22 ++++-- 4 files changed, 63 insertions(+), 61 deletions(-) diff --git a/python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py b/python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py index a73e03373951..e5528b4eb75d 100644 --- a/python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py +++ b/python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py @@ -311,7 +311,7 @@ def _create_streaming_chat_message_content( id=tool_use_block.id, index=stream_event.index, name=tool_use_block.name, - arguments=json.dumps(tool_use_block.input) if tool_use_block.input else None, + arguments=json.dumps(tool_use_block.input) if tool_use_block.input is not None else None, ) ) elif isinstance(stream_event, RawMessageDeltaEvent): diff --git a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py index 8d3f874942e7..c0b459aaaf69 100644 --- a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py +++ b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py @@ -4,7 +4,7 @@ import copy import logging from abc import ABC -from collections.abc import AsyncGenerator, Callable +from collections.abc import AsyncGenerator, Callable, Mapping from functools import reduce from typing import TYPE_CHECKING, Any, ClassVar @@ -34,55 +34,43 @@ logger: logging.Logger = logging.getLogger(__name__) tracer: Tracer = get_tracer(__name__) -# A reply that stops right after the tool name leaves an empty argument string. A function whose parameters -# are all optional is then invoked with its defaults, although the model was cut off before writing the -# arguments that decide what the tool does. Arguments that are present, complete or not, are left to the -# existing parsing; only the empty case is a call the model never started. +# A reply cut off right after the tool name leaves an empty argument string, and a function whose parameters +# are all optional then runs on its defaults. Only that case is refused; whatever the model did write is +# left to the existing parsing. CUT_OFF_TOOL_CALL_MESSAGE = ( - "The model's reply was cut off at the token limit before this tool call had complete arguments, " - "so it was not executed. Retry with a smaller request or a larger token budget." + "The model's reply was cut off at the token limit before this tool call had complete arguments, so it was " + "not executed. Retry with a smaller request or a larger token budget." ) -def _arguments_never_written(finish_reason: "FinishReason | None", function_call: FunctionCallContent) -> bool: +def _arguments_never_written( + finish_reason: "FinishReason | None", function_call: FunctionCallContent, arguments_written: bool | None = None +) -> bool: """A cut-off reply that stopped before writing any argument: the tool would run on its defaults. - Nothing written counts as never written: an empty string, or no argument text at all, which is how the - Anthropic connector reports a tool without input. A mapping is a complete argument object and is left to - the existing parsing. + `arguments_written` is the streamed evidence, collected before the aggregation turns empty fragments into + `"{}"`; without it the finished arguments are judged directly. A mapping is left to the existing parsing. """ if finish_reason != FinishReason.LENGTH: return False raw = function_call.arguments + if arguments_written is not None: + return not arguments_written and not isinstance(raw, Mapping) if isinstance(raw, str): return not raw.strip() return raw is None def _fragment_key(function_call: FunctionCallContent) -> str: - """Stable key for tracking one call's argument fragments across streamed chunks.""" - return function_call.id or f"index-{function_call.index}" - + """Which call a streamed chunk belongs to: the index, because only the first chunk carries the call id.""" + return f"index-{function_call.index}" if function_call.index is not None else (function_call.id or "") -def _streamed_arguments_never_written( - finish_reason: "FinishReason | None", - function_call: FunctionCallContent, - fragments: tuple[bool, bool], -) -> bool: - """Same question for a streamed reply, where empty fragments are combined into "{}" by the aggregation. - `fragments` is `(wrote_text, saw_empty)` collected before aggregation: a model that really wrote `{}` - produces non-empty fragments, a cut-off reply produces only empty ones. - """ - if finish_reason != FinishReason.LENGTH: - return False - wrote_text, saw_empty = fragments - if wrote_text: - return False - raw = function_call.arguments - if raw is not None and not isinstance(raw, str): - return False - return saw_empty or str(raw).strip() in ("", "{}") +def _note_written_arguments(arguments_written: set[str], message: "StreamingChatMessageContent | None") -> None: + """Remember the streamed calls that already carried argument text, before the aggregation runs.""" + for item in message.items if message is not None else (): + if isinstance(item, FunctionCallContent) and item.arguments: + arguments_written.add(_fragment_key(item)) def _record_refused_calls(chat_history: "ChatHistory", function_calls: list[FunctionCallContent]) -> None: @@ -338,34 +326,17 @@ async def get_streaming_chat_message_contents( for request_index in range(settings.function_choice_behavior.maximum_auto_invoke_attempts): # Hold the messages, if there are more than one response, it will not be used, so we flatten all_messages: list["StreamingChatMessageContent"] = [] - argument_fragments: dict[str, tuple[bool, bool]] = {} - function_call_returned = False + arguments_written: set[str] = set() async for messages in self._inner_get_streaming_chat_message_contents( chat_history, settings, request_index ): for msg in messages: if msg is not None: all_messages.append(msg) - for item in msg.items: - if isinstance(item, FunctionCallContent): - key = _fragment_key(item) - wrote_text, saw_empty = argument_fragments.get(key, (False, False)) - raw_arguments = item.arguments - if isinstance(raw_arguments, str): - if raw_arguments.strip(): - wrote_text = True - else: - saw_empty = True - elif raw_arguments is not None: - wrote_text = True - argument_fragments[key] = (wrote_text, saw_empty) - if not function_call_returned and any( - isinstance(item, FunctionCallContent) for item in msg.items - ): - function_call_returned = True + _note_written_arguments(arguments_written, msg) yield messages - if not function_call_returned: + if not any(isinstance(item, FunctionCallContent) for msg in all_messages for item in msg.items): return # There is one FunctionCallContent response stream in the messages, combining now to create @@ -378,10 +349,10 @@ async def get_streaming_chat_message_contents( if refused := [ call for call in function_calls - if _streamed_arguments_never_written( + if _arguments_never_written( full_completion.finish_reason, call, - argument_fragments.get(_fragment_key(call), (False, False)), + _fragment_key(call) in arguments_written, ) ]: _record_refused_calls(chat_history, refused) diff --git a/python/tests/unit/connectors/ai/anthropic/services/test_anthropic_chat_completion.py b/python/tests/unit/connectors/ai/anthropic/services/test_anthropic_chat_completion.py index cb9d41045682..cecc09f0b7dc 100644 --- a/python/tests/unit/connectors/ai/anthropic/services/test_anthropic_chat_completion.py +++ b/python/tests/unit/connectors/ai/anthropic/services/test_anthropic_chat_completion.py @@ -704,3 +704,24 @@ def test_stop_reason_maps_to_finish_reason(anthropic_unit_test_env, stop_reason, assert content.finish_reason == finish_reason assert streaming_content.finish_reason == finish_reason + + +def test_empty_tool_input_streams_as_written_arguments(anthropic_unit_test_env) -> None: + """A tool whose input is a complete `{}` must not look like a call that never wrote arguments. + + The connector used to report `{}` as no arguments at all, which made a cut-off reply look like a tool + call that never started and got it refused. + """ + from anthropic.types import RawContentBlockStopEvent, ToolUseBlock + + chat_completion = AnthropicChatCompletion() + event = RawContentBlockStopEvent( + index=1, + type="content_block_stop", + content_block=ToolUseBlock(id="call_1", input={}, name="set_mode", type="tool_use"), + ) + + streaming_content = chat_completion._create_streaming_chat_message_content(event) + + calls = [item for item in streaming_content.items if isinstance(item, FunctionCallContent)] + assert [call.arguments for call in calls] == ["{}"] diff --git a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py index 559210a22504..4cad537e9db3 100644 --- a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py +++ b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py @@ -54,7 +54,7 @@ def _chunk(delta: dict, finish: str | None = None) -> str: ) -def _tool_delta(arguments: str | None = None, name: str | None = TOOL) -> dict: +def _tool_delta(arguments: str | None = None, name: str | None = None, call_id: str = "") -> dict: function: dict = {} if name is not None: function["name"] = name @@ -62,13 +62,19 @@ def _tool_delta(arguments: str | None = None, name: str | None = TOOL) -> dict: function["arguments"] = arguments return { "role": "assistant", - "tool_calls": [{"index": 0, "id": "call_1", "type": "function", "function": function}], + "tool_calls": [{"index": 0, "id": call_id, "type": "function", "function": function}], } def _stream_body(fragments: list[str], finish: str, name: str | None = TOOL) -> bytes: - """One tool call streamed as several argument fragments, then a chunk carrying the finish reason.""" - body = _chunk(_tool_delta(name=name)) + """One tool call streamed the way the API streams it. + + The first delta carries the call id, the function name and an empty argument string; every later + delta carries an empty id and no name, so only the index identifies it. Captured from qwen-plus: + `{"index": 0, "id": "call_...", "function": {"name": "set_mode", "arguments": ""}}` then + `{"index": 0, "id": "", "function": {"arguments": "{\\""}}`. + """ + body = _chunk(_tool_delta(arguments="", name=name, call_id="call_1")) body += "".join(_chunk(_tool_delta(fragment)) for fragment in fragments) return (body + _chunk({}, finish) + "data: [DONE]\n\n").encode() @@ -179,9 +185,13 @@ async def test_cut_off_reply_with_empty_arguments_does_not_run_a_tool_needing_on @pytest.mark.asyncio @pytest.mark.parametrize("stream", [False, True]) async def test_cut_off_reply_with_complete_arguments_still_runs_the_tool(stream: bool) -> None: - """Compatibility: the model finished writing its arguments and only then hit the limit.""" + """Compatibility: the model finished writing its arguments and only then hit the limit. + + Streamed, the arguments arrive in several chunks that carry no call id (captured from qwen-plus at + `max_tokens=9`), so the pieces must be recognised as one call that did write its arguments. + """ plugin = RecordingPlugin() - body = _stream_body(['{"mode": "silent"}'], "length") if stream else _body('{"mode": "silent"}', "length") + body = _stream_body(['{"', 'mode": "', 'silent"}'], "length") if stream else _body('{"mode": "silent"}', "length") await _invoke(plugin, _service(body, stream), stream) assert plugin.calls == ["set_mode(mode='silent')"] From 081d926e9cabbcba59c0d8b475f7b2d5b8b569af Mon Sep 17 00:00:00 2001 From: Simin Yuan <281164480+simin-yuan@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:06:25 +0800 Subject: [PATCH 4/5] Python: refuse a cut-off call whose arguments arrive as an empty mapping Bedrock's non-streamed reply hands the tool input over as a parsed object instead of argument text, so a call cut off right after the tool name reaches the auto-invoke loop as `{}` rather than as an empty string. The cut-off check only recognised a string and `None`, so that shape still ran the tool on its parameter defaults, while the streamed shape was already refused. An empty mapping under finish_reason=length means nothing was written: a deliberate no-argument call ends with a tool_use stop reason, not with the length limit. The condition and the Bedrock evidence come from @kimnamu in #14723, who measured both shapes live on Bedrock with Claude Haiku 4.5. Adds the cut-off case and its compatibility counterpart through the Anthropic connector, which reports the same parsed object shape. --- .../ai/chat_completion_client_base.py | 6 +- .../connectors/ai/test_cut_off_tool_call.py | 65 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py index c0b459aaaf69..c1087a0c45aa 100644 --- a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py +++ b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py @@ -49,7 +49,9 @@ def _arguments_never_written( """A cut-off reply that stopped before writing any argument: the tool would run on its defaults. `arguments_written` is the streamed evidence, collected before the aggregation turns empty fragments into - `"{}"`; without it the finished arguments are judged directly. A mapping is left to the existing parsing. + `"{}"`; without it the finished arguments are judged directly. Connectors that hand over the parsed input + instead of the raw text (Anthropic, Bedrock) report a call cut off before its arguments as an empty + mapping, while a deliberate no-argument call there ends with `tool_use` rather than `length`. """ if finish_reason != FinishReason.LENGTH: return False @@ -58,7 +60,7 @@ def _arguments_never_written( return not arguments_written and not isinstance(raw, Mapping) if isinstance(raw, str): return not raw.strip() - return raw is None + return raw is None or (isinstance(raw, Mapping) and not raw) def _fragment_key(function_call: FunctionCallContent) -> str: diff --git a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py index 4cad537e9db3..4641c99965fd 100644 --- a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py +++ b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py @@ -267,3 +267,68 @@ def handler(request: httpx.Request) -> httpx.Response: assert plugin.calls == [] assert len(requests) == 2, "the cut-off reply should have been followed by another attempt" assert "cut off at the token limit" in requests[1].decode() + + +def _anthropic_service(stop_reason: str) -> object: + """Anthropic-format connectors hand over the parsed tool input, not the raw argument text. + + Bedrock reports a call cut off right after the tool name as `input={}` in the non-streamed reply + (verified on Bedrock with Claude Haiku 4.5, `max_tokens=40`), so this loop sees an empty mapping + where the OpenAI connector would have handed it the text `""`. + """ + from unittest.mock import AsyncMock, MagicMock + + from anthropic import AsyncAnthropic + from anthropic.types import Message, ToolUseBlock, Usage + + from semantic_kernel.connectors.ai.anthropic.services.anthropic_chat_completion import ( + AnthropicChatCompletion, + ) + + message = Message( + id="msg_1", + content=[ToolUseBlock(id="toolu_1", type="tool_use", name=TOOL, input={})], + model="claude-3-5-haiku-20241022", + role="assistant", + stop_reason=stop_reason, + stop_sequence=None, + type="message", + usage=Usage(input_tokens=10, output_tokens=10), + ) + client = MagicMock(spec=AsyncAnthropic) + client.messages.create = AsyncMock(return_value=message) + return AnthropicChatCompletion( + ai_model_id="claude-3-5-haiku-20241022", service_id="test", api_key="", async_client=client + ) + + +async def _invoke_anthropic(plugin: RecordingPlugin, service) -> None: + from semantic_kernel.connectors.ai.anthropic.prompt_execution_settings.anthropic_prompt_execution_settings import ( + AnthropicChatPromptExecutionSettings, + ) + from semantic_kernel.functions.kernel_arguments import KernelArguments + + settings = AnthropicChatPromptExecutionSettings( + function_choice_behavior=FunctionChoiceBehavior.Auto(auto_invoke=True, maximum_auto_invoke_attempts=1) + ) + history = ChatHistory(system_message="Test") + history.add_user_message("do it") + await service.get_chat_message_contents(history, settings, kernel=_kernel(plugin), arguments=KernelArguments()) + + +@pytest.mark.asyncio +async def test_cut_off_reply_that_arrives_as_an_empty_mapping_does_not_run_the_tool() -> None: + """The arguments never arrived, they were just reported as an empty mapping instead of an empty string.""" + plugin = RecordingPlugin() + await _invoke_anthropic(plugin, _anthropic_service("max_tokens")) + + assert plugin.calls == [], f"tool ran with defaults: {plugin.calls!r}" + + +@pytest.mark.asyncio +async def test_empty_mapping_in_a_reply_that_was_not_cut_off_still_runs_the_tool() -> None: + """A deliberate no-argument call ends with `tool_use`, so an empty mapping is only nothing-written under length.""" + plugin = RecordingPlugin() + await _invoke_anthropic(plugin, _anthropic_service("tool_use")) + + assert plugin.calls == ["set_mode(mode='default')"] From 52a775afa1aff297618758bb62417b4f2012e209 Mon Sep 17 00:00:00 2001 From: Simin Yuan <281164480+simin-yuan@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:30:23 +0800 Subject: [PATCH 5/5] Python: treat a streamed cut-off reply reported as "{}" as nothing written MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Anthropic connector serializes the tool input it receives, so a call cut off before its arguments reaches the streaming loop as the text "{}" — the same emptiness the non-streamed shape reports as an empty mapping, and just as unreadable as a call whose arguments were written. The pre- aggregation evidence treated any non-empty text as written, so that shape still ran the tool on its defaults. Argument text that holds only an empty object is now not counted as written. Nothing changes for a reply that was not cut off, where the finish reason is not the limit, and a call the model wrote itself still counts as written however it was chunked. Adds both cases through the streaming Anthropic connector; the cut-off one ran the tool on the previous commit. --- .../ai/chat_completion_client_base.py | 16 +++- .../connectors/ai/test_cut_off_tool_call.py | 96 +++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py index c1087a0c45aa..5c4904555694 100644 --- a/python/semantic_kernel/connectors/ai/chat_completion_client_base.py +++ b/python/semantic_kernel/connectors/ai/chat_completion_client_base.py @@ -69,12 +69,24 @@ def _fragment_key(function_call: FunctionCallContent) -> str: def _note_written_arguments(arguments_written: set[str], message: "StreamingChatMessageContent | None") -> None: - """Remember the streamed calls that already carried argument text, before the aggregation runs.""" + """Remember the streamed calls that already carried argument text, before the aggregation runs. + + A value that holds only an empty object is not text the model wrote: a connector that hands the parsed + input over serializes a call cut off before its arguments to `"{}"`, and the aggregation would otherwise + make that look like a call whose arguments were written. + """ for item in message.items if message is not None else (): - if isinstance(item, FunctionCallContent) and item.arguments: + if isinstance(item, FunctionCallContent) and _wrote_argument_text(item.arguments): arguments_written.add(_fragment_key(item)) +def _wrote_argument_text(arguments: "str | Mapping[str, Any] | None") -> bool: + """Whether a streamed chunk carried argument text the model did write.""" + if isinstance(arguments, str): + return arguments.strip() not in ("", "{}") + return bool(arguments) + + def _record_refused_calls(chat_history: "ChatHistory", function_calls: list[FunctionCallContent]) -> None: """Record a result per refused call so the model learns why nothing ran, instead of seeing silence.""" logger.warning( diff --git a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py index 4641c99965fd..26e5374c26b6 100644 --- a/python/tests/unit/connectors/ai/test_cut_off_tool_call.py +++ b/python/tests/unit/connectors/ai/test_cut_off_tool_call.py @@ -332,3 +332,99 @@ async def test_empty_mapping_in_a_reply_that_was_not_cut_off_still_runs_the_tool await _invoke_anthropic(plugin, _anthropic_service("tool_use")) assert plugin.calls == ["set_mode(mode='default')"] + + +def _anthropic_streaming_service(stop_reason: str) -> object: + """The same connector, streamed: the tool input is serialized to `"{}"` instead of handed over as a mapping.""" + from unittest.mock import AsyncMock, MagicMock + + from anthropic import AsyncAnthropic + from anthropic.types import ( + ContentBlockStopEvent, + Message, + MessageDeltaUsage, + RawMessageDeltaEvent, + RawMessageStartEvent, + ToolUseBlock, + Usage, + ) + from anthropic.types.raw_message_delta_event import Delta + + from semantic_kernel.connectors.ai.anthropic.services.anthropic_chat_completion import ( + AnthropicChatCompletion, + ) + + events = [ + RawMessageStartEvent( + message=Message( + id="msg_1", + content=[], + model="claude-3-5-haiku-20241022", + role="assistant", + stop_reason=None, + stop_sequence=None, + type="message", + usage=Usage(input_tokens=10, output_tokens=1), + ), + type="message_start", + ), + ContentBlockStopEvent( + index=0, + type="content_block_stop", + content_block=ToolUseBlock(id="toolu_1", type="tool_use", name=TOOL, input={}), + ), + RawMessageDeltaEvent( + delta=Delta(stop_reason=stop_reason, stop_sequence=None), + type="message_delta", + usage=MessageDeltaUsage(output_tokens=5), + ), + ] + + async def async_generator(): + for event in events: + yield event + + stream = AsyncMock() + stream.__aenter__.return_value = async_generator() + + client = MagicMock(spec=AsyncAnthropic) + client.messages = MagicMock() + client.messages.stream.return_value = stream + return AnthropicChatCompletion( + ai_model_id="claude-3-5-haiku-20241022", service_id="test", api_key="", async_client=client + ) + + +async def _invoke_anthropic_stream(plugin: RecordingPlugin, service) -> None: + from semantic_kernel.connectors.ai.anthropic.prompt_execution_settings.anthropic_prompt_execution_settings import ( + AnthropicChatPromptExecutionSettings, + ) + from semantic_kernel.functions.kernel_arguments import KernelArguments + + settings = AnthropicChatPromptExecutionSettings( + function_choice_behavior=FunctionChoiceBehavior.Auto(auto_invoke=True, maximum_auto_invoke_attempts=1) + ) + history = ChatHistory(system_message="Test") + history.add_user_message("do it") + async for _ in service.get_streaming_chat_message_content( + history, settings, kernel=_kernel(plugin), arguments=KernelArguments() + ): + pass + + +@pytest.mark.asyncio +async def test_cut_off_streamed_reply_that_arrives_as_an_empty_object_does_not_run_the_tool() -> None: + """The connector serializes the cut-off input to `"{}"`, which the aggregation cannot tell from written text.""" + plugin = RecordingPlugin() + await _invoke_anthropic_stream(plugin, _anthropic_streaming_service("max_tokens")) + + assert plugin.calls == [], f"tool ran with defaults: {plugin.calls!r}" + + +@pytest.mark.asyncio +async def test_empty_object_in_a_streamed_reply_that_was_not_cut_off_still_runs_the_tool() -> None: + """Same shape, but the model ended the turn on `tool_use`: the empty object is the arguments it meant.""" + plugin = RecordingPlugin() + await _invoke_anthropic_stream(plugin, _anthropic_streaming_service("tool_use")) + + assert plugin.calls == ["set_mode(mode='default')"]