Fix BaseModel method name conflicts in func_metadata #1123
Merged
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.
Fix BaseModel method name conflicts in func_metadata when function parameters have names that conflict with BaseModel methods.
Motivation and Context
When function parameters have names that conflict with BaseModel methods (e.g., 'model_dump', 'dict', 'json', 'validate'), Pydantic v2 raises warnings about shadowing parent attributes. This prevents users from creating MCP tools with parameter names that match BaseModel method names.
How Has This Been Tested?
test_func_metadata.py
:test_basemodel_reserved_names
: Verifies schema generation with conflicting parameter namestest_basemodel_reserved_names_validation
: Tests validation and function calling with reserved namestest_basemodel_reserved_names_with_json_preparsing
: Tests JSON pre-parsing with reserved namesBreaking Changes
None. This change is fully backward compatible. The aliasing is transparent to users - they continue to use the original parameter names in their function signatures and when calling the functions.
Types of changes
Checklist
Additional context
The solution uses Pydantic's alias system to map conflicting parameter names to prefixed internal fields (
field_
prefix) while preserving the original names in the external API. The implementation:hasattr
andcallable
checksmodel_dump_one_level
andpre_parse_json
to handle aliases correctlyby_alias=True
in schema generation to ensure correct parameter names in the API