fix(function_schema): raise UserError for Pydantic protected-namespace param names#3550
Open
devteamaegis wants to merge 1 commit into
Conversation
…e param names Parameters named model_dump, model_dump_json, model_validate, model_validate_json, or model_validate_strings caused an opaque ValueError from inside Pydantic's create_model() instead of a clear SDK error. Guard against these names before calling create_model() and surface a UserError with an actionable message. Fixes openai#3549
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.
What's broken
Calling
function_schema()(or@function_tool) on a function whose parameter is namedmodel_dump,model_dump_json,model_validate,model_validate_json, ormodel_validate_stringsraises an unhandledValueErrordeep inside Pydantic'screate_model(). The traceback points into Pydantic internals and gives no hint that the parameter name is the problem or how to fix it.Why it happens
Pydantic treats field names that match built-in
BaseModelmethods as belonging to a protected namespace and raisesValueErrorinsidecreate_model(). These five names are distinct frommodel_config/model_fields/model_computed_fieldscovered by PR #3548.Fix
A guard clause before
create_model()infunction_schema.pydetects any field name in the protected set and raises aUserErrorwith an actionable message listing the offending parameter names, before Pydantic ever sees them.Test
Added
test_pydantic_protected_namespace_param_raises_user_errorintest_function_schema.py, which asserts that a function with amodel_validateparameter raisesUserError(notValueError) and that the error message names the conflicting parameter.Fixes #3549