Repository navigation
Conversation
…gnatures A tool whose parameter and return schemas each define a different `User` `$def` rendered two `class sync_user_User` definitions (the second silently shadowing the first), and a type referencing a conflicting type from its fields rendered a plain `User` that no emitted definition provided. Resolve conflicting types per object instead of by name: each renders under its owning signature's name as prefix, with a numeric suffix when one signature owns several same-named conflicting types, and both signature lines and type definitions resolve references through the same per-object overrides. A type shared by several signatures keeps its first owner's prefix, as before.
|
Thanks for the contribution! To make sure changes are discussed before code is written, we ask that every PR references an existing issue with a closing keyword (e.g. If there's no issue for this yet, please open one first (e.g. a bug report with a reproducible example), wait for a maintainer to confirm it, then update this PR's description to reference it and reopen the PR. Documentation-only fixes are exempt and don't need an issue. Feel free to ping our team if you think this was closed in error. |
| """ | ||
| if owner_name and conflicting_type_names: | ||
| overrides = {n: f'{owner_name}_{n}' for n in conflicting_type_names} | ||
| if owner_name and self.name in conflicting_type_names: |
There was a problem hiding this comment.
🟠 High pydantic_ai/function_signature.py:172
render_definition() emits unresolved field annotations for conflicting nested types: rendering a Wrapper with conflicting_type_names={'Wrapper', 'User'} produces class tool_a_Wrapper with user: User, while the separately rendered definition is class tool_a_User. The new id(self) override only renames the outer definition and is not applied to field references; propagate the conflict mapping to referenced types when rendering individually.
🤖 Copy this AI Prompt to have your agent fix this:
In file @pydantic_ai_slim/pydantic_ai/function_signature.py around line 172:
`render_definition()` emits unresolved field annotations for conflicting nested types: rendering a `Wrapper` with `conflicting_type_names={'Wrapper', 'User'}` produces `class tool_a_Wrapper` with `user: User`, while the separately rendered definition is `class tool_a_User`. The new `id(self)` override only renames the outer definition and is not applied to field references; propagate the conflict mapping to referenced types when rendering individually.
ApprovabilityVerdict: Would Approve Macroscope's review found this PR approvable — This is a localized bug fix that makes conflicting rendered type names unique and resolvable, with targeted regression coverage and no schema, deployment, security, billing, or authentication impact. An unresolved High Macroscope finding still flags a direct Not approved because:
Adjust the Minimum Blocking Severity for this repo — including turning it Off — in Settings. You can add or adjust custom eligibility rules. Learn more. |
Merging this PR will not alter performance
Comparing Footnotes
|
Summary
FunctionSignature.render_type_definitionsresolved type-name conflicts by type name, so catalogs built from its output (e.g. the code-mode toolset inpydantic-ai-harness) could contain broken Python with no error anywhere:User$defrendered twoclass sync_user_Userdefinitions — the second silently shadowing the first, rebinding theuserparameter to the return type's shape;Wrapper.user: UserwhereUserconflicts across tools) rendered a plainUserthat no emitted definition provides.Why
_type_name_overrides(pydantic_ai_slim/pydantic_ai/function_signature.py:31onmain) mapped type names to prefixed names, and each renderer installed its own name-keyed map:FunctionSignature.render()prefixed every conflicting name with the tool name, so two same-named$defs owned by one signature both mapped to<tool>_User;render_type_definitions()passedowner_name=Nonefor non-conflicting types, so their definitions rendered with no overrides at all and their fields used the unprefixed name.Changes
_type_name_overridesis keyed by type object (id) instead of name, so each conflictingTypeSignatureresolves to its own prefixed name._prefixed_type_names()assigns those names, adding a numeric suffix when one signature owns several same-named conflicting types (sync_user_User,sync_user_User_2).render_type_definitions()installs the combined overrides for the whole render, so signature lines and every definition's fields resolve through the same per-object map. A type shared by several signatures (unified to one object byget_conflicting_type_names) keeps its first owner's prefix, as before.No public signatures change; rendered output only changes where it was previously invalid.
One pre-existing gap is unchanged and out of scope: when two tools' parameters directly reference the same unified conflicting type, each signature's
render()still emits its own tool prefix while the single shared definition takes the first owner's — the same output as onmaintoday.Testing
No issue exists for this yet; the reproduction is the three new regression tests, each failing on
main:test_render_type_definitions_param_return_name_collision— duplicateclass sync_user_Userheaderstest_render_type_definitions_non_conflicting_type_references_conflict—Wrapper.user: Userunresolvedtest_render_type_definitions_shared_type_references_conflict— same, with the conflictingUsershared across toolsChecklist
Fixes #8816