Python: Changes tools parameter type hints from MutableMapping to Mapping for broader type compatibility.#3867
Open
giles17 wants to merge 1 commit intomicrosoft:mainfrom
Open
Python: Changes tools parameter type hints from MutableMapping to Mapping for broader type compatibility.#3867giles17 wants to merge 1 commit intomicrosoft:mainfrom
MutableMapping to Mapping for broader type compatibility.#3867giles17 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
MutableMapping to Mapping for broader type compatibility.MutableMapping to Mapping for broader type compatibility.
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the Python agent API’s tools type hints to use Mapping instead of MutableMapping, reflecting that tool definitions are read-only and improving compatibility with more mapping types.
Changes:
- Switched
toolsparameter annotations fromMutableMapping[str, Any]toMapping[str, Any]inRawAgent/Agentconstructors andrun()overloads. - Updated internal casts and normalized tool list annotations to match the new mapping type.
Comments suppressed due to low confidence (1)
python/packages/core/agent_framework/_agents.py:1016
- After widening the accepted tool-definition type to
Mapping[str, Any],_prepare_run_contextstill typesfinal_toolsas... | dict[str, Any] .... If callers pass a non-dictmapping (e.g.,ChainMap/MappingProxyType), this becomes a type mismatch even though runtime behavior is fine. Consider wideningfinal_tools(and any downstream tool list types) toMapping[str, Any]for consistency with the new contract.
normalized_tools: list[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any] = (
[] if tools_ is None else tools_ if isinstance(tools_, list) else [tools_]
)
agent_name = self._get_agent_name()
# Resolve final tool list (runtime provided tools + local MCP server tools)
final_tools: list[FunctionTool | Callable[..., Any] | dict[str, Any] | Any] = []
for tool in normalized_tools:
if isinstance(tool, MCPTool):
| | Mapping[str, Any] | ||
| | Any | ||
| | Sequence[FunctionTool | Callable[..., Any] | MutableMapping[str, Any] | Any] | ||
| | Sequence[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any] |
There was a problem hiding this comment.
The tools parameter is typed as a Sequence[...], but normalization only treats list as a collection (isinstance(tools_, list)); passing a tuple/other sequence will be wrapped as a single item and the contained tools will be ignored downstream. Either restrict the type hint to list[...] (to match behavior) or update normalization/casts to accept any non-string Sequence and iterate over it.
Suggested change
| | Sequence[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any] | |
| | list[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
The tools parameter accepts dict-based tool definitions that are only read, never mutated.
Resolves #3577.
Description
Contribution Checklist