Skip to content

Keep conflicting type names unique and resolvable in rendered function signatures - #8789

Closed
BlueX888 wants to merge 1 commit into
pydantic:mainfrom
BlueX888:fix/prep-function-signature-conflict-prefix-collision
Closed

BlueX888 wants to merge 1 commit into
pydantic:mainfrom
BlueX888:fix/prep-function-signature-conflict-prefix-collision

Conversation

@BlueX888

@BlueX888 BlueX888 commented Sep 25, 2026 •

Copy link
Copy Markdown

Summary

FunctionSignature.render_type_definitions resolved type-name conflicts by type name, so catalogs built from its output (e.g. the code-mode toolset in pydantic-ai-harness) could contain broken Python with no error anywhere:

  • 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, rebinding the user parameter to the return type's shape;
  • a type whose fields reference a conflicting type (e.g. Wrapper.user: User where User conflicts across tools) rendered a plain User that no emitted definition provides.

Why

_type_name_overrides (pydantic_ai_slim/pydantic_ai/function_signature.py:31 on main) 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() passed owner_name=None for non-conflicting types, so their definitions rendered with no overrides at all and their fields used the unprefixed name.

Changes

  • _type_name_overrides is keyed by type object (id) instead of name, so each conflicting TypeSignature resolves to its own prefixed name.
  • New _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 by get_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 on main today.

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 — duplicate class sync_user_User headers
  • test_render_type_definitions_non_conflicting_type_references_conflict — Wrapper.user: User unresolved
  • test_render_type_definitions_shared_type_references_conflict — same, with the conflicting User shared across tools
uv run pytest tests/test_function_signature.py::test_render_type_definitions_param_return_name_collision \
              tests/test_function_signature.py::test_render_type_definitions_non_conflicting_type_references_conflict \
              tests/test_function_signature.py::test_render_type_definitions_shared_type_references_conflict
# main: 3 failed / this branch: 3 passed

uv run pytest tests/test_function_signature.py tests/test_toolsets.py tests/test_public_interface_contracts.py tests/test_tools.py
# 443 passed

uv run ruff format --check && uv run ruff check
# 814 files already formatted / All checks passed!

PYRIGHT_PYTHON_IGNORE_WARNINGS=1 uv run pyright pydantic_ai_slim/pydantic_ai/function_signature.py tests/test_function_signature.py
# 0 errors, 0 warnings, 0 informations

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

Review in cubic

Fixes #8816

…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.
@github-actions github-actions Bot added size: M Medium PR (101-500 weighted lines) bug Report that something isn't working, or PR implementing a fix labels Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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. Fixes #1234) in its description, so this PR has been closed automatically.

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.

@github-actions github-actions Bot closed this Sep 25, 2026
"""
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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 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.

@macroscopeapp

macroscopeapp Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: 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 render_definition() nested-reference risk for the correctness process.

Not approved because:

  • 1 blocking correctness issue found at or above your repo's Minimum Blocking Severity

Adjust the Minimum Blocking Severity for this repo — including turning it Off — in Settings. You can add or adjust custom eligibility rules. Learn more.

@codspeed

codspeed Bot commented Sep 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 3 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing BlueX888:fix/prep-function-signature-conflict-prefix-collision (1b5b6c5) with main (84ebeb0)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Report that something isn't working, or PR implementing a fix size: M Medium PR (101-500 weighted lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

render_type_definitions can emit duplicate TypedDict classes and unresolved type references when type names conflict

1 participant