Skip to content

pydantic_ai.function_signature

Generate function signatures from functions and JSON schemas.

This module provides utilities to represent tool definitions as human-readable function signatures, which LLMs can understand more easily than raw JSON schemas. Used by code mode to present tools as callable functions.

SimpleTypeExpr

A simple named type like str, int, Any, None.

LiteralTypeExpr

A Literal type expression like Literal['a', 'b'] or Literal[42].

GenericTypeExpr

A generic type expression like list[User], dict[str, User], tuple[int, str].

UnionTypeExpr

A union type expression like User | None, str | int.

TypeFieldSignature

A single field in a TypedDict-style type definition.

Methods

__str__
def __str__() -> str

Render this field as a line in a TypedDict class body.

Returns

str

TypeSignature

A TypedDict-style class definition with named fields.

Attributes

display_name

The type name, with tool-name prefix applied if rendering context is set.

Type: str

Methods

__str__
def __str__() -> str

Return the type name (for use in type expressions like def foo(x: User)).

Returns

str

render_definition
def render_definition(
    owner_name: str | None = None,
    conflicting_type_names: frozenset[str] = frozenset(),
) -> str

Render the full TypedDict class definition.

Returns

str

Parameters

owner_name : str | None Default: None

The owning tool name, used to build prefixed type names for conflicting types (e.g. get_user_Address).

conflicting_type_names : frozenset[str] Default: frozenset()

Set of type names that need tool-name prefixes (from get_conflicting_type_names). Only effective when owner_name is also provided.

structurally_equal
def structurally_equal(other: TypeSignature) -> bool

Compare two TypeSignatures structurally, ignoring descriptions.

Returns

bool

FunctionParam

A single parameter in a function signature.

Methods

__str__
def __str__() -> str

Render this parameter as a function parameter string.

Returns

str

FunctionSignature

Function signature shape with referenced type definitions.

This class holds the structural data (params, return type, referenced types) needed to render a function signature. Name and description can be overridden at render time (e.g. from a ToolDefinition).

Attributes

params

Function parameters, all rendered as keyword-only (JSON schema doesn’t distinguish positional/keyword).

Type: dict[str, FunctionParam] Default: field(default_factory=(dict[str, FunctionParam]))

return_type

The return type expression.

Type: TypeExpr

referenced_types

TypedDict class definitions needed by the signature.

Type: list[TypeSignature] Default: field(default_factory=(list[TypeSignature]))

is_async

Whether the underlying function is async.

Type: bool Default: False

Methods

render
def render(
    body: str,
    name: str | None = None,
    description: str | None = None,
    is_async: bool | None = None,
    conflicting_type_names: frozenset[str] = frozenset(),
) -> str

Render the signature with a specific body.

Sets _type_name_overrides so that dedup-prefixed types resolve correctly during rendering.

Returns

str

Parameters

body : str

The function body (e.g. '...' or 'return await tool()').

name : str | None Default: None

The function name (also used for dedup prefix resolution). Falls back to self.name.

description : str | None Default: None

Optional docstring to include. Falls back to self.description.

is_async : bool | None Default: None

Override async rendering. If None, uses self.is_async.

conflicting_type_names : frozenset[str] Default: frozenset()

Set of type names that need tool-name prefixes (from get_conflicting_type_names).

from_schema

@classmethod

def from_schema(
    cls,
    name: str,
    parameters_schema: dict[str, Any],
    return_schema: dict[str, Any] | None = None,
) -> FunctionSignature

Build a FunctionSignature from JSON schemas.

name is stored on the resulting signature and also used for generating fallback type names (e.g. GetUserAddress) when the schema has no title.

Parameter and return schemas are processed independently — each resolves $refs against its own $defs. Name collisions between parameter and return types (e.g. both define a User $def with different structures) are handled by get_conflicting_type_names at a later stage.

Returns

FunctionSignature

get_conflicting_type_names

@staticmethod

def get_conflicting_type_names(signatures: list[FunctionSignature]) -> frozenset[str]

Identify TypedDict name conflicts across multiple tool signatures.

Each signature keeps all its referenced types (so it remains self-contained), but identical types (same name and structure) are unified to the same object instance.

Returns the set of type names that have conflicts (same name, different structure) and need tool-name prefixes at render time. Pass this set to FunctionSignature.render(conflicting_type_names=...).

Use collect_unique_referenced_types() when rendering to emit each definition once.

Returns

frozenset[str]

collect_unique_referenced_types

@staticmethod

def collect_unique_referenced_types(
    signatures: list[FunctionSignature],
) -> list[TypeSignature]

Collect unique TypeSignature objects from signatures, deduplicating by identity.

Returns

list[TypeSignature]

render_type_definitions

@staticmethod

def render_type_definitions(
    signatures: list[FunctionSignature],
    conflicting_type_names: frozenset[str],
) -> list[str]

Render unique TypedDict definitions for a set of function signatures.

For types whose names conflict across signatures (as identified by get_conflicting_type_names), each definition is rendered with a tool-name prefix (e.g. get_user_Address).

Returns

list[str] — A list of rendered TypedDict class definitions as strings.

Parameters

signatures : list[FunctionSignature]

The function signatures (after get_conflicting_type_names).

conflicting_type_names : frozenset[str]

The set returned by get_conflicting_type_names.

TypeExpr

A type expression node in the signature’s type tree.

Type: TypeAlias Default: 'TypeSignature | SimpleTypeExpr | LiteralTypeExpr | GenericTypeExpr | UnionTypeExpr'