Fix Pydantic field alias consistency in structured output #1099
+46
−1
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=True
to 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: