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, or the one rendering context calls for when some names have to change.

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()

The set returned by a get_conflicting_type_names call made just before rendering. Only effective when owner_name is also provided.

structurally_equal
def structurally_equal(other: TypeSignature) -> bool

Compare two TypeSignatures structurally, ignoring descriptions.

Nested types compare by name only, so this can call two types equal that FunctionSignature.get_conflicting_type_names keeps apart.

Returns

bool

FunctionParam

A single parameter in a function signature.

Attributes

name

The Python identifier the parameter renders as.

This differs from its key in FunctionSignature.params, the JSON schema property name, when that name is a Python keyword or not an identifier: from renders as from_ and user-id as user_id.

Type: str

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 keyed by JSON schema property name, 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()

The set returned by a get_conflicting_type_names call made just before rendering. Types named by the last such call use those names whenever any non-empty set is passed.

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, and name the types involved.

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

A type keeps its name unless a different type has it too. Then it takes the name of the first tool using it as a prefix (e.g. get_user_Address), and a number if that is taken.

The new names are stored on the types, and apply when this set is passed on to FunctionSignature.render(conflicting_type_names=...). Render straight after this call, as resolving a signature again, among other signatures, replaces them.

Returns the set of type names that had to change.

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.

A type whose name had to change uses the one get_conflicting_type_names stored on it: a tool-name prefix (e.g. get_user_Address), and a number if that is taken too.

Returns

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

Parameters

signatures : list[FunctionSignature]

The function signatures, straight 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'