Repository navigation
Closed
Description
Initial Checks
- I'm using the latest version of Pydantic AI
- I've searched for my issue in the issue tracker before opening this issue
Description
FunctionSignature.render_type_definitions resolves type-name conflicts by type name, so its output can be Python that does not behave as intended, with no error raised anywhere in pydantic-ai:
- A tool whose parameter and return schemas each define their own
User$defrenders twoclass sync_user_User(TypedDict):definitions. Concatenated into one module (as catalogs built from this output do), the second definition silently rebinds the name, so theuserparameter ends up with the return type's shape. On top of that,RetWrap.userrenders as plainUser, which no emitted definition provides. - A non-conflicting type whose fields reference a conflicting type renders the plain name — e.g.
Wrapper.user: User— while the only emittedUserdefinitions are the prefixedtool_a_User/tool_b_User, so the reference resolves to nothing.
Example
from pydantic_ai.function_signature import FunctionSignature
# Case 1: one tool whose parameter and return schemas each define their own `User`.
sig = FunctionSignature.from_schema(
name='sync_user',
parameters_schema={
'type': 'object',
'properties': {'user': {'$ref': '#/$defs/User'}},
'required': ['user'],
'$defs': {
'User': {
'type': 'object',
'properties': {'name': {'type': 'string'}},
'required': ['name'],
},
},
},
return_schema={
'$ref': '#/$defs/RetWrap',
'$defs': {
'User': {
'type': 'object',
'properties': {'id': {'type': 'integer'}, 'email': {'type': 'string'}},
'required': ['id', 'email'],
},
'RetWrap': {
'type': 'object',
'properties': {'user': {'$ref': '#/$defs/User'}},
'required': ['user'],
},
},
},
)
conflicting = FunctionSignature.get_conflicting_type_names([sig])
print('conflicting:', sorted(conflicting))
definitions = FunctionSignature.render_type_definitions([sig], conflicting)
print('--- render_type_definitions ---')
for d in definitions:
print(d)
print('--- render ---')
print(sig.render('...', name='sync_user', conflicting_type_names=conflicting))
# Case 2: a non-conflicting type whose fields reference a conflicting type.
sig_a = FunctionSignature.from_schema(
name='tool_a',
parameters_schema={
'type': 'object',
'properties': {'wrap': {'$ref': '#/$defs/Wrapper'}},
'required': ['wrap'],
'$defs': {
'Wrapper': {
'type': 'object',
'properties': {'user': {'$ref': '#/$defs/User'}},
'required': ['user'],
},
'User': {
'type': 'object',
'properties': {'name': {'type': 'string'}},
'required': ['name'],
},
},
},
)
sig_b = FunctionSignature.from_schema(
name='tool_b',
parameters_schema={
'type': 'object',
'properties': {'user': {'$ref': '#/$defs/User'}},
'required': ['user'],
'$defs': {
'User': {
'type': 'object',
'properties': {'id': {'type': 'integer'}},
'required': ['id'],
},
},
},
)
conflicting = FunctionSignature.get_conflicting_type_names([sig_a, sig_b])
print()
print('conflicting:', sorted(conflicting))
definitions = FunctionSignature.render_type_definitions([sig_a, sig_b], conflicting)
print('--- render_type_definitions ---')
for d in definitions:
print(d)
print('--- render ---')
print(sig_a.render('...', name='tool_a', conflicting_type_names=conflicting))
print(sig_b.render('...', name='tool_b', conflicting_type_names=conflicting))Actual output on current main (5badf40):
conflicting: ['User']
--- render_type_definitions ---
class sync_user_User(TypedDict):
name: str
class sync_user_User(TypedDict):
id: int
email: str
class RetWrap(TypedDict):
user: User
--- render ---
def sync_user(*, user: sync_user_User) -> RetWrap:
...
conflicting: ['User']
--- render_type_definitions ---
class Wrapper(TypedDict):
user: User
class tool_a_User(TypedDict):
name: str
class tool_b_User(TypedDict):
id: int
--- render ---
def tool_a(*, wrap: Wrapper) -> Any:
...
def tool_b(*, user: tool_b_User) -> Any:
...
Expected behaviour
- Every emitted class name is unique. A duplicate
classheader means the second definition silently shadows the first when the output is assembled into one module. - Every annotation resolves to an emitted definition (or a JSON-type builtin the renderer emits). Keeping conflicting names both unique and resolvable is the stated purpose of the tool-name prefix in
render_type_definitions("each definition is rendered with a tool-name prefix (e.g.get_user_Address)").
For case 1 that means two distinct names (e.g. sync_user_User and sync_user_User_2) with RetWrap.user pointing at the name the return-side definition actually renders under; for case 2, Wrapper.user should render as the prefixed name of the User it references (tool_a_User).
Root cause
pydantic_ai_slim/pydantic_ai/function_signature.py:
_type_name_overrides(line 31) maps type names to prefixed names (dict[str, str]), andTypeSignature.display_name(line 153) resolves by name — so two same-named$defs owned by one signature both map to the same<tool>_<name>, producing the duplicate class definitions of case 1.render_type_definitions(lines 440–448) renders each definition with overrides installed only for that definition itself; a non-conflicting type is rendered withowner_name=None, so its fields render the unprefixedUserof case 2.
Logfire Trace
Not applicable — the rendering path involves no model call.
Python, Pydantic AI & LLM client version
- Python: 3.12.14
- Pydantic AI:
main@ 5badf40;function_signature.pyis identical in v2.51.0 (the latest release), so both are affected - LLM provider SDK: N/A
Happy to reopen the PR with this fix once this issue is linked, or to be assigned if you prefer to take it.
Metadata
Metadata
Assignees
Labels
No labels
Activity
pydanty commentedon Sep 26, 2026
pydanty runtime failure
Run
df_run_ec8f8f4984bf4ec9a9e8started from the GitHub webhook, but the Dogfooding runtimefailed before pydanty completed the workflow. The pydanty status comment above
(if any) is from a completed run and still stands.
Next step: maintainer attention needed; rerun after fixing the runtime failure.
Failure detail (tail)
Show AI SDK tools to the model as typed Python, and prove the example…
fix: narrow to one name and one definition per type