Skip to content

fix(function_schema): raise UserError for Pydantic protected-namespace param names#3550

Open
devteamaegis wants to merge 1 commit into
openai:mainfrom
devteamaegis:fix/valueerror-from-pydantic-s-protected-namespace-check-in-create-model
Open

fix(function_schema): raise UserError for Pydantic protected-namespace param names#3550
devteamaegis wants to merge 1 commit into
openai:mainfrom
devteamaegis:fix/valueerror-from-pydantic-s-protected-namespace-check-in-create-model

Conversation

@devteamaegis
Copy link
Copy Markdown

What's broken

Calling function_schema() (or @function_tool) on a function whose parameter is named model_dump, model_dump_json, model_validate, model_validate_json, or model_validate_strings raises an unhandled ValueError deep inside Pydantic's create_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 BaseModel methods as belonging to a protected namespace and raises ValueError inside create_model(). These five names are distinct from model_config / model_fields / model_computed_fields covered by PR #3548.

Fix

A guard clause before create_model() in function_schema.py detects any field name in the protected set and raises a UserError with an actionable message listing the offending parameter names, before Pydantic ever sees them.

Test

Added test_pydantic_protected_namespace_param_raises_user_error in test_function_schema.py, which asserts that a function with a model_validate parameter raises UserError (not ValueError) and that the error message names the conflicting parameter.

Fixes #3549

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

function_schema() raises opaque ValueError for params named model_dump / model_validate / etc.

2 participants