Python: [BREAKING] Remove deprecated kwargs compatibility paths (#4858)

* [BREAKING] Remove deprecated kwargs compatibility paths

Remove the deprecated kwargs compatibility shims across core agents, clients, tools, middleware, and telemetry.

Keep workflow kwargs behavior intact in this branch and follow up separately in #4850.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Fix PR CI fallout for kwargs removal

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Address PR review feedback

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* updates

* Fix Azure AI CI fallout

Remove the stale _get_current_conversation_id override from the Azure AI client after the OpenAI base helper was deleted.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fixed new classes

* Fix Assistants deprecated import gating

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Fix integration replay regressions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Switch multi-agent hosting samples to Azure chat completions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Simplify Azure multi-agent sample config

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Eduard van Valkenburg
2026-03-27 22:00:12 +01:00
committed by GitHub
Unverified
parent ca6cdd142e
commit b1b528e4a8
52 changed files with 1136 additions and 971 deletions
+8 -12
View File
@@ -148,11 +148,9 @@ async def test_chat_client_agent_init_with_name(
assert agent.description == "Test"
def test_agent_init_warns_for_direct_additional_properties(client: SupportsChatGetResponse) -> None:
with pytest.warns(DeprecationWarning, match="additional_properties"):
agent = Agent(client=client, legacy_key="legacy-value")
assert agent.additional_properties["legacy_key"] == "legacy-value"
def test_agent_init_rejects_direct_additional_properties(client: SupportsChatGetResponse) -> None:
with pytest.raises(TypeError):
Agent(client=client, legacy_key="legacy-value")
async def test_chat_client_agent_run(client: SupportsChatGetResponse) -> None:
@@ -303,7 +301,6 @@ async def test_prepare_run_context_handles_function_kwargs(
},
compaction_strategy=None,
tokenizer=None,
legacy_kwargs={"legacy_key": "legacy-value"},
function_invocation_kwargs={"runtime_key": "runtime-value"},
client_kwargs={"client_key": "client-value"},
)
@@ -311,7 +308,6 @@ async def test_prepare_run_context_handles_function_kwargs(
assert ctx["chat_options"]["temperature"] == 0.4
assert "additional_function_arguments" not in ctx["chat_options"]
assert ctx["function_invocation_kwargs"]["from_options"] == "options-value"
assert ctx["function_invocation_kwargs"]["legacy_key"] == "legacy-value"
assert ctx["function_invocation_kwargs"]["runtime_key"] == "runtime-value"
assert "session" not in ctx["function_invocation_kwargs"]
assert ctx["client_kwargs"]["client_key"] == "client-value"
@@ -1181,8 +1177,8 @@ async def test_agent_run_accepts_prefixed_mcp_tools(chat_client_base: Any) -> No
assert tool_names == ["search", "docs_search"]
async def test_agent_tool_receives_session_in_kwargs(chat_client_base: Any) -> None:
"""Verify legacy **kwargs tools receive the session when agent.run() is called with one."""
async def test_agent_tool_without_context_does_not_receive_session(chat_client_base: Any) -> None:
"""Verify tools without FunctionInvocationContext no longer receive injected session kwargs."""
captured: dict[str, Any] = {}
@@ -1215,8 +1211,8 @@ async def test_agent_tool_receives_session_in_kwargs(chat_client_base: Any) -> N
result = await agent.run("hello", session=session)
assert result.text == "done"
assert captured.get("has_session") is True
assert captured.get("has_state") is True
assert captured.get("has_session") is False
assert captured.get("has_state") is False
async def test_agent_tool_receives_explicit_session_via_function_invocation_context_kwargs(
@@ -1278,7 +1274,7 @@ async def test_chat_agent_tool_choice_run_level_overrides_agent_level(chat_clien
agent = Agent(
client=chat_client_base,
tools=[tool_tool],
options={"tool_choice": "auto"},
default_options={"tool_choice": "auto"},
)
# Run with run-level tool_choice="required"
@@ -1,7 +1,6 @@
# Copyright (c) Microsoft. All rights reserved.
import inspect
from typing import Any
from unittest.mock import patch
@@ -15,11 +14,6 @@ from agent_framework import (
Message,
SlidingWindowStrategy,
SupportsChatGetResponse,
SupportsCodeInterpreterTool,
SupportsFileSearchTool,
SupportsImageGenerationTool,
SupportsMCPTool,
SupportsWebSearchTool,
TruncationStrategy,
)
@@ -53,11 +47,9 @@ def test_base_client(chat_client_base: SupportsChatGetResponse):
assert isinstance(chat_client_base, SupportsChatGetResponse)
def test_base_client_warns_for_direct_additional_properties(chat_client_base: SupportsChatGetResponse) -> None:
with pytest.warns(DeprecationWarning, match="additional_properties"):
client = type(chat_client_base)(legacy_key="legacy-value")
assert client.additional_properties["legacy_key"] == "legacy-value"
def test_base_client_rejects_direct_additional_properties(chat_client_base: SupportsChatGetResponse) -> None:
with pytest.raises(TypeError):
type(chat_client_base)(legacy_key="legacy-value")
def test_base_client_as_agent_uses_explicit_additional_properties(chat_client_base: SupportsChatGetResponse) -> None:
@@ -66,27 +58,6 @@ def test_base_client_as_agent_uses_explicit_additional_properties(chat_client_ba
assert agent.additional_properties == {"team": "core"}
def test_openai_chat_completion_client_get_response_docstring_surfaces_layered_runtime_docs() -> None:
from agent_framework.openai import OpenAIChatCompletionClient
docstring = inspect.getdoc(OpenAIChatCompletionClient.get_response)
assert docstring is not None
assert "Get a response from a chat client." in docstring
assert "function_invocation_kwargs" in docstring
assert "middleware: Optional per-call chat and function middleware." in docstring
assert "function_middleware: Optional per-call function middleware." not in docstring
def test_openai_chat_completion_client_get_response_is_defined_on_openai_class() -> None:
from agent_framework.openai import OpenAIChatCompletionClient
signature = inspect.signature(OpenAIChatCompletionClient.get_response)
assert OpenAIChatCompletionClient.get_response.__qualname__ == "OpenAIChatCompletionClient.get_response"
assert "middleware" in signature.parameters
async def test_base_client_get_response_uses_explicit_client_kwargs(chat_client_base: SupportsChatGetResponse) -> None:
async def fake_inner_get_response(**kwargs):
assert kwargs["trace_id"] == "trace-123"
@@ -333,66 +304,3 @@ async def test_chat_client_instructions_handling(chat_client_base: SupportsChatG
assert appended_messages[0].text == "You are a helpful assistant."
assert appended_messages[1].role == "user"
assert appended_messages[1].text == "hello"
# region Tool Support Protocol Tests
def test_openai_responses_client_supports_all_tool_protocols():
"""Test that OpenAIResponsesClient supports all hosted tool protocols."""
from agent_framework.openai import OpenAIResponsesClient
assert isinstance(OpenAIResponsesClient, SupportsCodeInterpreterTool)
assert isinstance(OpenAIResponsesClient, SupportsWebSearchTool)
assert isinstance(OpenAIResponsesClient, SupportsImageGenerationTool)
assert isinstance(OpenAIResponsesClient, SupportsMCPTool)
assert isinstance(OpenAIResponsesClient, SupportsFileSearchTool)
def test_openai_chat_completion_client_supports_web_search_only():
"""Test that OpenAIChatClient only supports web search tool."""
from agent_framework.openai import OpenAIChatCompletionClient
assert not isinstance(OpenAIChatCompletionClient, SupportsCodeInterpreterTool)
assert isinstance(OpenAIChatCompletionClient, SupportsWebSearchTool)
assert not isinstance(OpenAIChatCompletionClient, SupportsImageGenerationTool)
assert not isinstance(OpenAIChatCompletionClient, SupportsMCPTool)
assert not isinstance(OpenAIChatCompletionClient, SupportsFileSearchTool)
def test_openai_assistants_client_supports_code_interpreter_and_file_search():
"""Test that OpenAIAssistantsClient supports code interpreter and file search."""
from agent_framework.openai import OpenAIAssistantsClient
assert isinstance(OpenAIAssistantsClient, SupportsCodeInterpreterTool)
assert not isinstance(OpenAIAssistantsClient, SupportsWebSearchTool)
assert not isinstance(OpenAIAssistantsClient, SupportsImageGenerationTool)
assert not isinstance(OpenAIAssistantsClient, SupportsMCPTool)
assert isinstance(OpenAIAssistantsClient, SupportsFileSearchTool)
def test_protocol_isinstance_with_client_instance():
"""Test that protocol isinstance works with client instances."""
from agent_framework.openai import OpenAIResponsesClient
# Create mock client instance (won't connect to API)
client = OpenAIResponsesClient.__new__(OpenAIResponsesClient)
assert isinstance(client, SupportsCodeInterpreterTool)
assert isinstance(client, SupportsWebSearchTool)
def test_protocol_tool_methods_return_dict():
"""Test that static tool methods return dict[str, Any]."""
from agent_framework.openai import OpenAIResponsesClient
code_tool = OpenAIResponsesClient.get_code_interpreter_tool()
assert isinstance(code_tool, dict)
assert code_tool.get("type") == "code_interpreter"
web_tool = OpenAIResponsesClient.get_web_search_tool()
assert isinstance(web_tool, dict)
assert web_tool.get("type") == "web_search"
# endregion
@@ -13,6 +13,7 @@ from agent_framework import (
Content,
Message,
SupportsChatGetResponse,
chat_middleware,
tool,
)
from agent_framework._compaction import (
@@ -74,7 +75,7 @@ async def test_base_client_with_function_calling(chat_client_base: SupportsChatG
assert response.messages[2].text == "done"
async def test_base_client_with_function_calling_tools_in_kwargs(chat_client_base: SupportsChatGetResponse):
async def test_base_client_with_function_calling_string_input(chat_client_base: SupportsChatGetResponse):
exec_counter = 0
@tool(name="test_function", approval_mode="never_require")
@@ -95,7 +96,7 @@ async def test_base_client_with_function_calling_tools_in_kwargs(chat_client_bas
ChatResponse(messages=Message(role="assistant", text="done")),
]
response = await chat_client_base.get_response("hello", tools=[ai_func])
response = await chat_client_base.get_response("hello", options={"tool_choice": "auto", "tools": [ai_func]})
assert exec_counter == 1
assert len(response.messages) == 3
@@ -1429,6 +1430,36 @@ async def test_function_invocation_config_enabled_false(chat_client_base: Suppor
assert len(response.messages) > 0
async def test_function_invocation_config_enabled_false_preserves_invocation_kwargs(
chat_client_base: SupportsChatGetResponse,
):
"""Test disabled function invocation still forwards invocation kwargs downstream."""
captured_kwargs: dict[str, Any] = {}
@tool(name="test_function")
def ai_func(arg1: str) -> str:
return f"Processed {arg1}"
@chat_middleware
async def capture_middleware(context, call_next):
captured_kwargs.update(context.function_invocation_kwargs or {})
await call_next()
chat_client_base.chat_middleware = [capture_middleware]
chat_client_base.run_responses = [
ChatResponse(messages=Message(role="assistant", text="response without function calling")),
]
chat_client_base.function_invocation_configuration["enabled"] = False
await chat_client_base.get_response(
[Message(role="user", text="hello")],
options={"tool_choice": "auto", "tools": [ai_func]},
function_invocation_kwargs={"tool_request_id": "tool-123"},
)
assert captured_kwargs == {"tool_request_id": "tool-123"}
@pytest.mark.skip(reason="Error handling and failsafe behavior needs investigation in unified API")
async def test_function_invocation_config_max_consecutive_errors(chat_client_base: SupportsChatGetResponse):
"""Test that max_consecutive_errors_per_request limits error retries."""
@@ -1523,7 +1554,7 @@ async def test_function_invocation_stop_clears_conversation_id_non_stream(chat_c
response = await chat_client_base.get_response(
[Message(role="user", text="hello")],
options={"tool_choice": "auto", "tools": [error_func]},
session=session_stub,
client_kwargs={"session": session_stub},
)
assert response.conversation_id is None
@@ -1881,8 +1912,7 @@ async def test_hosted_tool_approval_response(chat_client_base: SupportsChatGetRe
# Send the approval response
response = await chat_client_base.get_response(
[Message(role="user", contents=[approval_response])],
tool_choice="auto",
tools=[local_func],
options={"tool_choice": "auto", "tools": [local_func]},
)
# The hosted tool approval should be returned as-is (not executed)
@@ -1930,8 +1960,7 @@ async def test_hosted_mcp_approval_response_passthrough(chat_client_base: Suppor
response = await chat_client_base.get_response(
messages,
tool_choice="auto",
tools=[local_func],
options={"tool_choice": "auto", "tools": [local_func]},
)
# The response should succeed without errors
@@ -2024,8 +2053,7 @@ async def test_mixed_local_and_hosted_approval_flow(chat_client_base: SupportsCh
response = await chat_client_base.get_response(
messages,
tool_choice="auto",
tools=[local_func],
options={"tool_choice": "auto", "tools": [local_func]},
)
assert response is not None
@@ -2799,7 +2827,7 @@ async def test_streaming_function_invocation_stop_clears_conversation_id(chat_cl
"hello",
options={"tool_choice": "auto", "tools": [error_func]},
stream=True,
session=session_stub,
client_kwargs={"session": session_stub},
)
async for _ in stream:
pass
@@ -1,351 +0,0 @@
# Copyright (c) Microsoft. All rights reserved.
"""Tests for kwargs propagation from get_response() to @tool functions."""
from collections.abc import AsyncIterable, Awaitable, MutableSequence, Sequence
from typing import Any
from agent_framework import (
Agent,
BaseChatClient,
ChatMiddlewareLayer,
ChatResponse,
ChatResponseUpdate,
Content,
FunctionInvocationContext,
FunctionInvocationLayer,
Message,
ResponseStream,
tool,
)
from agent_framework.observability import ChatTelemetryLayer
class _MockBaseChatClient(BaseChatClient[Any]):
"""Mock chat client for testing function invocation."""
def __init__(self) -> None:
super().__init__()
self.run_responses: list[ChatResponse] = []
self.streaming_responses: list[list[ChatResponseUpdate]] = []
self.call_count: int = 0
def _inner_get_response(
self,
*,
messages: MutableSequence[Message],
stream: bool,
options: dict[str, Any],
**kwargs: Any,
) -> Awaitable[ChatResponse] | ResponseStream[ChatResponseUpdate, ChatResponse]:
if stream:
return self._get_streaming_response(messages=messages, options=options, **kwargs)
async def _get() -> ChatResponse:
return await self._get_non_streaming_response(messages=messages, options=options, **kwargs)
return _get()
async def _get_non_streaming_response(
self,
*,
messages: MutableSequence[Message],
options: dict[str, Any],
**kwargs: Any,
) -> ChatResponse:
self.call_count += 1
if self.run_responses:
return self.run_responses.pop(0)
return ChatResponse(messages=Message(role="assistant", text="default response"))
def _get_streaming_response(
self,
*,
messages: MutableSequence[Message],
options: dict[str, Any],
**kwargs: Any,
) -> ResponseStream[ChatResponseUpdate, ChatResponse]:
async def _stream() -> AsyncIterable[ChatResponseUpdate]:
self.call_count += 1
if self.streaming_responses:
for update in self.streaming_responses.pop(0):
yield update
else:
yield ChatResponseUpdate(
contents=[Content.from_text("default streaming response")], role="assistant", finish_reason="stop"
)
def _finalize(updates: Sequence[ChatResponseUpdate]) -> ChatResponse:
response_format = options.get("response_format")
output_format_type = response_format if isinstance(response_format, type) else None
return ChatResponse.from_updates(updates, output_format_type=output_format_type)
return ResponseStream(_stream(), finalizer=_finalize)
class FunctionInvokingMockClient(
FunctionInvocationLayer[Any],
ChatMiddlewareLayer[Any],
ChatTelemetryLayer[Any],
_MockBaseChatClient,
):
"""Mock client with function invocation support."""
pass
class TestKwargsPropagationToFunctionTool:
"""Test cases for kwargs flowing from get_response() to @tool functions."""
async def test_kwargs_propagate_to_tool_with_kwargs(self) -> None:
"""Test that kwargs passed to get_response() are available in @tool **kwargs."""
# TODO(Copilot): Remove this legacy coverage once runtime ``**kwargs`` tool injection is removed.
captured_kwargs: dict[str, Any] = {}
@tool(approval_mode="never_require")
def capture_kwargs_tool(x: int, **kwargs: Any) -> str:
"""A tool that captures kwargs for testing."""
captured_kwargs.update(kwargs)
return f"result: x={x}"
client = FunctionInvokingMockClient()
client.run_responses = [
# First response: function call
ChatResponse(
messages=[
Message(
role="assistant",
contents=[
Content.from_function_call(
call_id="call_1", name="capture_kwargs_tool", arguments='{"x": 42}'
)
],
)
]
),
# Second response: final answer
ChatResponse(messages=[Message(role="assistant", text="Done!")]),
]
result = await client.get_response(
messages=[Message(role="user", text="Test")],
stream=False,
options={
"tools": [capture_kwargs_tool],
"additional_function_arguments": {
"user_id": "user-123",
"session_token": "secret-token",
"custom_data": {"key": "value"},
},
},
)
# Verify the tool was called and received the kwargs
assert "user_id" in captured_kwargs, f"Expected 'user_id' in captured kwargs: {captured_kwargs}"
assert captured_kwargs["user_id"] == "user-123"
assert "session_token" in captured_kwargs
assert captured_kwargs["session_token"] == "secret-token"
assert "custom_data" in captured_kwargs
assert captured_kwargs["custom_data"] == {"key": "value"}
# Verify result
assert result.messages[-1].text == "Done!"
async def test_kwargs_not_forwarded_to_tool_without_kwargs(self) -> None:
"""Test that kwargs are NOT forwarded to @tool that doesn't accept **kwargs."""
# TODO(Copilot): Remove this legacy coverage once runtime ``**kwargs`` tool injection is removed.
@tool(approval_mode="never_require")
def simple_tool(x: int) -> str:
"""A simple tool without **kwargs."""
return f"result: x={x}"
client = FunctionInvokingMockClient()
client.run_responses = [
ChatResponse(
messages=[
Message(
role="assistant",
contents=[
Content.from_function_call(call_id="call_1", name="simple_tool", arguments='{"x": 99}')
],
)
]
),
ChatResponse(messages=[Message(role="assistant", text="Completed!")]),
]
# Call with additional_function_arguments - the tool should work but not receive them
result = await client.get_response(
messages=[Message(role="user", text="Test")],
stream=False,
options={
"tools": [simple_tool],
"additional_function_arguments": {"user_id": "user-123"},
},
)
# Verify the tool was called successfully (no error from extra kwargs)
assert result.messages[-1].text == "Completed!"
async def test_kwargs_isolated_between_function_calls(self) -> None:
"""Test that kwargs are consistent across multiple function call invocations."""
# TODO(Copilot): Remove this legacy coverage once runtime ``**kwargs`` tool injection is removed.
invocation_kwargs: list[dict[str, Any]] = []
@tool(approval_mode="never_require")
def tracking_tool(name: str, **kwargs: Any) -> str:
"""A tool that tracks kwargs from each invocation."""
invocation_kwargs.append(dict(kwargs))
return f"called with {name}"
client = FunctionInvokingMockClient()
client.run_responses = [
# Two function calls in one response
ChatResponse(
messages=[
Message(
role="assistant",
contents=[
Content.from_function_call(
call_id="call_1", name="tracking_tool", arguments='{"name": "first"}'
),
Content.from_function_call(
call_id="call_2", name="tracking_tool", arguments='{"name": "second"}'
),
],
)
]
),
ChatResponse(messages=[Message(role="assistant", text="All done!")]),
]
result = await client.get_response(
messages=[Message(role="user", text="Test")],
stream=False,
options={
"tools": [tracking_tool],
"additional_function_arguments": {
"request_id": "req-001",
"trace_context": {"trace_id": "abc"},
},
},
)
# Both invocations should have received the same kwargs
assert len(invocation_kwargs) == 2
for kwargs in invocation_kwargs:
assert kwargs.get("request_id") == "req-001"
assert kwargs.get("trace_context") == {"trace_id": "abc"}
assert result.messages[-1].text == "All done!"
async def test_streaming_response_kwargs_propagation(self) -> None:
"""Test that kwargs propagate to @tool in streaming mode."""
# TODO(Copilot): Remove this legacy coverage once runtime ``**kwargs`` tool injection is removed.
captured_kwargs: dict[str, Any] = {}
@tool(approval_mode="never_require")
def streaming_capture_tool(value: str, **kwargs: Any) -> str:
"""A tool that captures kwargs during streaming."""
captured_kwargs.update(kwargs)
return f"processed: {value}"
client = FunctionInvokingMockClient()
client.streaming_responses = [
# First stream: function call
[
ChatResponseUpdate(
role="assistant",
contents=[
Content.from_function_call(
call_id="stream_call_1",
name="streaming_capture_tool",
arguments='{"value": "streaming-test"}',
)
],
finish_reason="stop",
)
],
# Second stream: final response
[
ChatResponseUpdate(
contents=[Content.from_text("Stream complete!")], role="assistant", finish_reason="stop"
)
],
]
# Collect streaming updates
updates: list[ChatResponseUpdate] = []
stream = client.get_response(
messages=[Message(role="user", text="Test")],
stream=True,
options={
"tools": [streaming_capture_tool],
"additional_function_arguments": {
"streaming_session": "session-xyz",
"correlation_id": "corr-123",
},
},
)
async for update in stream:
updates.append(update)
# Verify kwargs were captured by the tool
assert "streaming_session" in captured_kwargs, f"Expected 'streaming_session' in {captured_kwargs}"
assert captured_kwargs["streaming_session"] == "session-xyz"
assert captured_kwargs["correlation_id"] == "corr-123"
async def test_agent_run_injects_function_invocation_context(self) -> None:
"""Test that Agent.run injects FunctionInvocationContext for ctx-based tools."""
captured_context_kwargs: dict[str, Any] = {}
captured_client_kwargs: dict[str, Any] = {}
captured_options: dict[str, Any] = {}
@tool(approval_mode="never_require")
def capture_context_tool(x: int, ctx: FunctionInvocationContext) -> str:
captured_context_kwargs.update(ctx.kwargs)
return f"result: x={x}"
class CapturingFunctionInvokingMockClient(FunctionInvokingMockClient):
async def _get_non_streaming_response(
self,
*,
messages: MutableSequence[Message],
options: dict[str, Any],
**kwargs: Any,
) -> ChatResponse:
captured_options.update(options)
captured_client_kwargs.update(kwargs)
return await super()._get_non_streaming_response(messages=messages, options=options, **kwargs)
client = CapturingFunctionInvokingMockClient()
client.run_responses = [
ChatResponse(
messages=[
Message(
role="assistant",
contents=[
Content.from_function_call(
call_id="call_1",
name="capture_context_tool",
arguments='{"x": 42}',
)
],
)
]
),
ChatResponse(messages=[Message(role="assistant", text="Done!")]),
]
agent = Agent(client=client, tools=[capture_context_tool])
result = await agent.run(
[Message(role="user", text="Test")],
function_invocation_kwargs={"tool_request_id": "tool-123"},
client_kwargs={"client_request_id": "client-456"},
)
assert captured_context_kwargs["tool_request_id"] == "tool-123"
assert "client_request_id" not in captured_context_kwargs
assert captured_client_kwargs["client_request_id"] == "client-456"
assert "tool_request_id" not in captured_client_kwargs
assert "additional_function_arguments" not in captured_options
assert result.messages[-1].text == "Done!"
+16 -8
View File
@@ -1751,6 +1751,9 @@ async def test_mcp_tool_sampling_callback_no_valid_content():
assert isinstance(result, types.ErrorData)
assert result.code == types.INTERNAL_ERROR
assert "Failed to get right content types from the response." in result.message
mock_chat_client.get_response.assert_awaited_once()
_, kwargs = mock_chat_client.get_response.await_args
assert kwargs["options"] == {"max_tokens": None}
async def test_mcp_tool_sampling_callback_no_response_and_successful_message_creation():
@@ -3704,14 +3707,19 @@ async def test_mcp_tool_filters_framework_kwargs():
# Invoke the tool with framework kwargs that should be filtered out
await func.invoke(
param="test_value",
response_format=MockResponseFormat, # Should be filtered
chat_options={"some": "option"}, # Should be filtered
tools=[Mock()], # Should be filtered
tool_choice="auto", # Should be filtered
session=Mock(), # Should be filtered
conversation_id="conv-123", # Should be filtered
options={"metadata": "value"}, # Should be filtered
context=FunctionInvocationContext(
function=func,
arguments={"param": "test_value"},
kwargs={
"response_format": MockResponseFormat, # Should be filtered
"chat_options": {"some": "option"}, # Should be filtered
"tools": [Mock()], # Should be filtered
"tool_choice": "auto", # Should be filtered
"session": Mock(), # Should be filtered
"conversation_id": "conv-123", # Should be filtered
"options": {"metadata": "value"}, # Should be filtered
},
),
)
# Verify call_tool was called with only the valid argument
@@ -789,9 +789,10 @@ class TestChatAgentFunctionMiddlewareWithTools:
assert modified_kwargs["new_param"] == "added_by_middleware"
assert modified_kwargs["custom_param"] == "test_value"
async def test_run_kwargs_available_in_function_middleware(self, chat_client_base: "MockBaseChatClient") -> None:
"""Test that kwargs passed directly to agent.run() appear in FunctionInvocationContext.kwargs,
including complex nested values like dicts."""
async def test_function_invocation_kwargs_available_in_function_middleware(
self, chat_client_base: "MockBaseChatClient"
) -> None:
"""Test that function_invocation_kwargs appear in FunctionInvocationContext.kwargs."""
captured_kwargs: dict[str, Any] = {}
@function_middleware
@@ -822,18 +823,20 @@ class TestChatAgentFunctionMiddlewareWithTools:
session_metadata = {"tenant": "acme-corp", "region": "us-west"}
await agent.run(
[Message(role="user", text="Get weather")],
user_id="user-456",
session_metadata=session_metadata,
function_invocation_kwargs={
"user_id": "user-456",
"session_metadata": session_metadata,
},
)
assert "user_id" in captured_kwargs, f"Expected 'user_id' in kwargs: {captured_kwargs}"
assert captured_kwargs["user_id"] == "user-456"
assert captured_kwargs["session_metadata"] == {"tenant": "acme-corp", "region": "us-west"}
async def test_run_kwargs_merged_with_additional_function_arguments(
async def test_function_invocation_kwargs_merged_with_additional_function_arguments(
self, chat_client_base: "MockBaseChatClient"
) -> None:
"""Test that explicit additional_function_arguments in options take precedence over run kwargs."""
"""Test that explicit additional_function_arguments in options take precedence."""
captured_kwargs: dict[str, Any] = {}
@function_middleware
@@ -863,9 +866,10 @@ class TestChatAgentFunctionMiddlewareWithTools:
await agent.run(
[Message(role="user", text="Get weather")],
# This kwarg should be overridden by additional_function_arguments
user_id="from-kwargs",
tenant_id="from-kwargs",
function_invocation_kwargs={
"user_id": "from-kwargs",
"tenant_id": "from-kwargs",
},
options={
"additional_function_arguments": {
"user_id": "from-options",
@@ -876,15 +880,15 @@ class TestChatAgentFunctionMiddlewareWithTools:
# additional_function_arguments takes precedence for overlapping keys
assert captured_kwargs["user_id"] == "from-options"
# Non-overlapping kwargs from run() still come through
# Non-overlapping function_invocation_kwargs still come through
assert captured_kwargs["tenant_id"] == "from-kwargs"
# Keys only in additional_function_arguments are present
assert captured_kwargs["extra_key"] == "only-in-options"
async def test_run_kwargs_consistent_across_multiple_tool_calls(
async def test_function_invocation_kwargs_consistent_across_multiple_tool_calls(
self, chat_client_base: "MockBaseChatClient"
) -> None:
"""Test that kwargs are consistent across multiple tool invocations in a single run."""
"""Test that function_invocation_kwargs are consistent across tool invocations."""
invocation_kwargs: list[dict[str, Any]] = []
@function_middleware
@@ -917,8 +921,10 @@ class TestChatAgentFunctionMiddlewareWithTools:
await agent.run(
[Message(role="user", text="Get weather for both cities")],
user_id="user-456",
request_id="req-001",
function_invocation_kwargs={
"user_id": "user-456",
"request_id": "req-001",
},
)
assert len(invocation_kwargs) == 2
@@ -2060,23 +2066,21 @@ class TestChatAgentChatMiddleware:
"agent_middleware_after",
]
async def test_agent_middleware_can_access_and_override_custom_kwargs(self) -> None:
"""Test that agent middleware can access and override custom parameters like temperature."""
captured_kwargs: dict[str, Any] = {}
modified_kwargs: dict[str, Any] = {}
async def test_agent_middleware_can_access_and_override_options(self) -> None:
"""Test that agent middleware can access and override runtime options."""
captured_options: dict[str, Any] = {}
modified_options: dict[str, Any] = {}
@agent_middleware
async def kwargs_middleware(context: AgentContext, call_next: Callable[[], Awaitable[None]]) -> None:
# Capture the original kwargs
captured_kwargs.update(context.kwargs)
assert isinstance(context.options, dict)
captured_options.update(context.options)
# Modify some kwargs
context.kwargs["temperature"] = 0.9
context.kwargs["max_tokens"] = 500
context.kwargs["new_param"] = "added_by_middleware"
context.options["temperature"] = 0.9
context.options["max_tokens"] = 500
context.options["new_param"] = "added_by_middleware"
# Store modified kwargs for verification
modified_kwargs.update(context.kwargs)
modified_options.update(context.options)
await call_next()
@@ -2084,24 +2088,25 @@ class TestChatAgentChatMiddleware:
client = MockBaseChatClient()
agent = Agent(client=client, middleware=[kwargs_middleware])
# Execute the agent with custom parameters
# Execute the agent with runtime options
messages = [Message(role="user", text="test message")]
response = await agent.run(messages, temperature=0.7, max_tokens=100, custom_param="test_value")
response = await agent.run(
messages,
options={"temperature": 0.7, "max_tokens": 100, "custom_param": "test_value"},
)
# Verify response
assert response is not None
assert len(response.messages) > 0
# Verify middleware captured the original kwargs
assert captured_kwargs["temperature"] == 0.7
assert captured_kwargs["max_tokens"] == 100
assert captured_kwargs["custom_param"] == "test_value"
assert captured_options["temperature"] == 0.7
assert captured_options["max_tokens"] == 100
assert captured_options["custom_param"] == "test_value"
# Verify middleware could modify the kwargs
assert modified_kwargs["temperature"] == 0.9
assert modified_kwargs["max_tokens"] == 500
assert modified_kwargs["new_param"] == "added_by_middleware"
assert modified_kwargs["custom_param"] == "test_value" # Should still be there
assert modified_options["temperature"] == 0.9
assert modified_options["max_tokens"] == 500
assert modified_options["new_param"] == "added_by_middleware"
assert modified_options["custom_param"] == "test_value"
# class TestMiddlewareWithProtocolOnlyAgent:
@@ -2,6 +2,7 @@
from collections.abc import Awaitable, Callable
from typing import Any
from unittest.mock import patch
from agent_framework import (
Agent,
@@ -296,50 +297,77 @@ class TestChatMiddleware:
assert response3 is not None
assert execution_count["count"] == 2 # Should be 2 now
async def test_chat_client_middleware_can_access_and_override_custom_kwargs(
async def test_run_level_middleware_is_not_forwarded_to_inner_client(
self, chat_client_base: "MockBaseChatClient"
) -> None:
"""Test that chat client middleware can access and override custom parameters like temperature."""
captured_kwargs: dict[str, Any] = {}
modified_kwargs: dict[str, Any] = {}
"""Test that run-level middleware stays in the middleware pipeline only."""
observed_context_kwargs: dict[str, Any] = {}
@chat_middleware
async def inspecting_middleware(context: ChatContext, call_next: Callable[[], Awaitable[None]]) -> None:
observed_context_kwargs.update(context.kwargs)
await call_next()
async def fake_inner_get_response(**kwargs: Any) -> ChatResponse:
assert "middleware" not in kwargs
return ChatResponse(messages=[Message(role="assistant", text="ok")])
with patch.object(
chat_client_base,
"_inner_get_response",
side_effect=fake_inner_get_response,
) as mock_inner_get_response:
response = await chat_client_base.get_response(
[Message(role="user", text="hello")],
client_kwargs={"middleware": [inspecting_middleware], "trace_id": "trace-123"},
)
assert response.messages[0].text == "ok"
assert observed_context_kwargs == {"trace_id": "trace-123"}
mock_inner_get_response.assert_called_once()
async def test_chat_client_middleware_can_access_and_override_options(
self, chat_client_base: "MockBaseChatClient"
) -> None:
"""Test that chat client middleware can access and override runtime options."""
captured_options: dict[str, Any] = {}
modified_options: dict[str, Any] = {}
@chat_middleware
async def kwargs_middleware(context: ChatContext, call_next: Callable[[], Awaitable[None]]) -> None:
# Capture the original kwargs
captured_kwargs.update(context.kwargs)
assert isinstance(context.options, dict)
captured_options.update(context.options)
# Modify some kwargs
context.kwargs["temperature"] = 0.9
context.kwargs["max_tokens"] = 500
context.kwargs["new_param"] = "added_by_middleware"
context.options["temperature"] = 0.9
context.options["max_tokens"] = 500
context.options["new_param"] = "added_by_middleware"
# Store modified kwargs for verification
modified_kwargs.update(context.kwargs)
modified_options.update(context.options)
await call_next()
# Add middleware to chat client
chat_client_base.chat_middleware = [kwargs_middleware]
# Execute chat client with custom parameters
# Execute chat client with runtime options
messages = [Message(role="user", text="test message")]
response = await chat_client_base.get_response(
messages, temperature=0.7, max_tokens=100, custom_param="test_value"
messages,
options={"temperature": 0.7, "max_tokens": 100, "custom_param": "test_value"},
)
# Verify response
assert response is not None
assert len(response.messages) > 0
assert captured_kwargs["temperature"] == 0.7
assert captured_kwargs["max_tokens"] == 100
assert captured_kwargs["custom_param"] == "test_value"
assert captured_options["temperature"] == 0.7
assert captured_options["max_tokens"] == 100
assert captured_options["custom_param"] == "test_value"
# Verify middleware could modify the kwargs
assert modified_kwargs["temperature"] == 0.9
assert modified_kwargs["max_tokens"] == 500
assert modified_kwargs["new_param"] == "added_by_middleware"
assert modified_kwargs["custom_param"] == "test_value" # Should still be there
assert modified_options["temperature"] == 0.9
assert modified_options["max_tokens"] == 500
assert modified_options["new_param"] == "added_by_middleware"
assert modified_options["custom_param"] == "test_value"
def test_chat_middleware_pipeline_cache_reuses_matching_middleware(
self,
@@ -207,7 +207,7 @@ async def test_chat_client_observability(mock_chat_client, span_exporter: InMemo
messages = [Message(role="user", text="Test message")]
span_exporter.clear()
response = await client.get_response(messages=messages, model_id="Test")
response = await client.get_response(messages=messages, options={"model_id": "Test"})
assert response is not None
spans = span_exporter.get_finished_spans()
assert len(spans) == 1
@@ -232,7 +232,7 @@ async def test_chat_client_streaming_observability(
span_exporter.clear()
# Collect all yielded updates
updates = []
stream = client.get_response(stream=True, messages=messages, model_id="Test")
stream = client.get_response(stream=True, messages=messages, options={"model_id": "Test"})
async for update in stream:
updates.append(update)
await stream.get_final_response()
@@ -1540,7 +1540,7 @@ async def test_chat_client_observability_exception(mock_chat_client, span_export
span_exporter.clear()
with pytest.raises(ValueError, match="Test error"):
await client.get_response(messages=messages, model_id="Test")
await client.get_response(messages=messages, options={"model_id": "Test"})
spans = span_exporter.get_finished_spans()
assert len(spans) == 1
@@ -1570,7 +1570,7 @@ async def test_chat_client_streaming_observability_exception(mock_chat_client, s
span_exporter.clear()
with pytest.raises(ValueError, match="Streaming error"):
async for _ in client.get_response(messages=messages, stream=True, model_id="Test"):
async for _ in client.get_response(messages=messages, stream=True, options={"model_id": "Test"}):
pass
spans = span_exporter.get_finished_spans()
@@ -2075,7 +2075,7 @@ async def test_capture_messages_with_finish_reason(mock_chat_client, span_export
messages = [Message(role="user", text="Test")]
span_exporter.clear()
response = await client.get_response(messages=messages, model_id="Test")
response = await client.get_response(messages=messages, options={"model_id": "Test"})
assert response is not None
assert response.finish_reason == "stop"
@@ -2165,7 +2165,7 @@ async def test_chat_client_when_disabled(mock_chat_client, span_exporter: InMemo
messages = [Message(role="user", text="Test")]
span_exporter.clear()
response = await client.get_response(messages=messages, model_id="Test")
response = await client.get_response(messages=messages, options={"model_id": "Test"})
assert response is not None
spans = span_exporter.get_finished_spans()
@@ -2181,7 +2181,7 @@ async def test_chat_client_streaming_when_disabled(mock_chat_client, span_export
span_exporter.clear()
updates = []
async for update in client.get_response(messages=messages, stream=True, model_id="Test"):
async for update in client.get_response(messages=messages, stream=True, options={"model_id": "Test"}):
updates.append(update)
assert len(updates) == 2 # Still works functionally
@@ -2661,7 +2661,7 @@ async def test_capture_messages_preserves_non_ascii_characters(mock_chat_client,
messages = [Message(role="user", text=japanese_text)]
span_exporter.clear()
response = await client.get_response(messages=messages, model_id="Test")
response = await client.get_response(messages=messages, options={"model_id": "Test"})
assert response is not None
spans = span_exporter.get_finished_spans()
+15 -20
View File
@@ -594,8 +594,8 @@ async def test_tool_invoke_telemetry_sensitive_disabled(span_exporter: InMemoryS
assert attributes[OtelAttr.TOOL_CALL_ID] == "test_call_id"
async def test_tool_invoke_ignores_additional_kwargs() -> None:
"""Ensure tools drop unknown kwargs when invoked with validated arguments."""
async def test_tool_invoke_rejects_unexpected_runtime_kwargs() -> None:
"""Ensure invoke() requires runtime data to flow through FunctionInvocationContext."""
@tool
async def simple_tool(message: str) -> str:
@@ -604,15 +604,12 @@ async def test_tool_invoke_ignores_additional_kwargs() -> None:
args = simple_tool.input_model(message="hello world")
# These kwargs simulate runtime context passed through function invocation.
result = await simple_tool.invoke(
arguments=args,
api_token="secret-token",
options={"model_id": "dummy"},
)
assert isinstance(result, list)
assert result[0].text == "HELLO WORLD"
with pytest.raises(TypeError, match="Unexpected keyword argument"):
await simple_tool.invoke(
arguments=args,
api_token="secret-token",
options={"model_id": "dummy"},
)
async def test_tool_invoke_telemetry_with_pydantic_args(span_exporter: InMemorySpanExporter):
@@ -917,8 +914,8 @@ def test_parse_inputs_unsupported_type():
# endregion
async def test_ai_function_with_kwargs_injection():
"""Test that ai_function correctly handles kwargs injection and hides them from schema."""
async def test_ai_function_with_kwargs_rejects_runtime_invoke_kwargs():
"""Test that runtime kwargs must be passed through FunctionInvocationContext."""
@tool
def tool_with_kwargs(x: int, **kwargs: Any) -> str:
@@ -937,13 +934,11 @@ async def test_ai_function_with_kwargs_injection():
# Verify direct invocation works
assert tool_with_kwargs(1, user_id="user1") == "x=1, user=user1"
# Verify invoke works with injected args
result = await tool_with_kwargs.invoke(
arguments=tool_with_kwargs.input_model(x=5),
user_id="user2",
)
assert isinstance(result, list)
assert result[0].text == "x=5, user=user2"
with pytest.raises(TypeError, match="Unexpected keyword argument"):
await tool_with_kwargs.invoke(
arguments=tool_with_kwargs.input_model(x=5),
user_id="user2",
)
# Verify invoke works without injected args (uses default)
result_default = await tool_with_kwargs.invoke(