fix: handle field names with leading underscores in Pydantic model creation#219
fix: handle field names with leading underscores in Pydantic model creation#219clawtom wants to merge 2 commits intoraw-labs:mainfrom
Conversation
…eation Pydantic v2 rejects field names starting with `_`, treating them as private attributes. When a YAML property name starts with `_`, generate a safe Python identifier (`field_` + stripped name) and record the original via `Field(alias=...)`. Add `populate_by_name=True` to ConfigDict so the model accepts both the alias and the Python name during validation. Fixes raw-labs#184
Using lstrip("_") strips ALL leading underscores, so _url and __url
both map to "field_url" causing a silent overwrite. Use [1:] to strip
exactly one underscore, preserving the distinction.
|
Good catch from @cursor (bot). Fixed in the latest commit. The issue: Fix: use
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| if prop_name.startswith("_"): | ||
| python_name = "field_" + prop_name[1:] # strip exactly one underscore to avoid name collisions | ||
| field_kwargs["alias"] = prop_name | ||
| has_aliased_fields = True |
There was a problem hiding this comment.
Silent field collision with field_ prefix mapping
Low Severity
When an underscore-prefixed property like _name is renamed to field_name, it can silently collide with an existing non-underscore property also called field_name in the same schema. Both would map to the same key in model_fields, causing one to silently overwrite the other and be dropped from the generated model. No collision detection or error is raised.
|
The second Bugbot finding (field collision between If it becomes a real concern, the fix would be to add collision detection before inserting into |


Summary
Fixes #184.
Pydantic v2 treats field names starting with
_as private attributes and raisesPydanticUserError("Fields must not use names with leading underscores") when they appear in acreate_model()call. This blocked users from declaring YAML return types with underscore-prefixed field names such as__urlor__type.Changes
In
_create_pydantic_model_from_schema, when iterating over object properties:prop_namestarts with_, generate a safe Python identifier ("field_" + prop_name.lstrip("_")) and setField(alias=prop_name)so Pydantic maps the alias back to the original name during validation.populate_by_name=TruetoConfigDictwhen aliased fields are present, allowing the model to accept both the alias (original YAML name) and the Python identifier.No change to non-underscore field names — existing behavior is unaffected.
Example
Before this fix, the following YAML return type definition would raise a
PydanticUserError:After this fix, validation works correctly:
__urlis accepted as an alias for the generated Python fieldfield_url.Note
Medium Risk
Touches dynamic Pydantic model generation used for endpoint parameter/return validation; mistakes could break validation or change accepted/returned field names for object schemas, though the change is narrowly scoped to underscore-prefixed properties.
Overview
Fixes generated Pydantic v2 models for JSON Schema
objectproperties that start with_by renaming them to safe Python identifiers (e.g.,field_x) and preserving the original key viaField(alias=...).When any such aliases are present, the generated model now enables
populate_by_nameso both the alias (original schema/YAML field) and the Python field name can be used during validation.Written by Cursor Bugbot for commit 7bfb987. This will update automatically on new commits. Configure here.