Fix Pydantic field alias consistency in structured output#1099
Merged
ihrpr merged 2 commits intomodelcontextprotocol:mainfrom Jul 7, 2025
Merged
Fix Pydantic field alias consistency in structured output#1099ihrpr merged 2 commits intomodelcontextprotocol:mainfrom
ihrpr merged 2 commits intomodelcontextprotocol:mainfrom
Conversation
Resolve inconsistency between schema and actual output when using Pydantic models with field aliases as tool return types: - Add by_alias=True parameter to model_dump() call in func_metadata.py - Add comprehensive test case to verify alias consistency - Ensure schema generation and structured output use same aliased field names Before: Schema shows "first", "second" but output uses "field_first", "field_second" After: Both schema and output consistently use aliased field names
ihrpr
requested changes
Jul 7, 2025
Contributor
ihrpr
left a comment
There was a problem hiding this comment.
Thank you for working on this!
QQ:
Added comprehensive test case test_structured_output_aliases() that verifies both schema generation and output use consistent alias names
I don't see any tests added, please can this be checked?
Add comprehensive test_structured_output_aliases() that verifies: - Schema generation uses aliased field names - Structured output uses same aliased field names - Both explicit values and default values work correctly - Ensures consistency between schema and actual tool output
Contributor
Author
|
I’m wondering why I can’t get some tests to pass on Windows.
|
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.
Fix Pydantic field alias consistency in structured output
Summary
Fixes issue #1073. Fix inconsistency between schema and structured output when using Pydantic models with field aliases in tool return types. The schema would display aliased field names but the actual output would use original field names.
Motivation and Context
When defining Pydantic models with field aliases for tool return types, there was an inconsistency where:
This inconsistency made it difficult for clients to reliably consume structured tool outputs, as the field names in the schema didn't match the actual response data.
How Has This Been Tested?
test_structured_output_aliases()that verifies both schema generation and output use consistent alias namesBreaking Changes
No breaking changes. This is a bug fix that makes the behavior consistent with what the schema advertises. Users who were working around this inconsistency may need to update their code to use the aliased field names consistently.
Types of changes
Checklist
Additional context
The fix was a simple one-line change adding
by_alias=Trueto themodel_dump()call infunc_metadata.py:114. This ensures that when Pydantic models are serialized for structured output, they use the same field names that appear in the generated JSON schema.The test case covers both the schema generation and the actual output conversion to ensure they remain consistent. This addresses the core issue described in the original bug report where the schema and output used different field naming conventions.
Technical Details
Before the fix:
"first"and"second""field_first"and"field_second"After the fix:
"first"and"second"Code change: