Skip to content

Automatic tool output name generation fails with Generic types containing brackets #2929

@lionpeloux

Description

@lionpeloux

Initial Checks

Description

When using Generic types directly in output_type without wrapping them in ToolOutput, the automatic tool output name generation fails because the generated name contains brackets [], which violates Google's function naming requirements.

see : https://pydanticlogfire.slack.com/archives/C083V7PMHHA/p1758034303546309

Current Behavior

Using Generic types directly in output_type fails:

scan_agent_v2 = BaseAgent(
    name="scan-agent",
    model_ref=ModelRef.GEMINI_2_5_PRO,
    system_prompt=SYSTEM_PROMPT,
    output_type=[
        ScanSuccess[ElectricStorageWaterHeaterScanResult],  # ❌ Fails
        ScanFailure,
    ],
)

Error:

ModelHTTPError: status_code: 400
"Invalid function name. Must start with a letter or an underscore. Must be a-z, A-Z, 0-9, or contain underscores, dots and dashes, with a maximum length of 64."

Expected Behavior

Generic types should work seamlessly without requiring explicit ToolOutput wrapping.

Current Workaround

Explicitly wrap Generic types in ToolOutput with a custom name:

class ScanSuccess[T](ScanResponse):
    """Extraction réussie des données du scan."""
    result: T

class ScanFailure(ScanResponse):
    """Échec de l'extraction des données du scan."""
    message: str

scan_agent_v2 = BaseAgent(
    name="scan-agent",
    model_ref=ModelRef.GEMINI_2_5_PRO,
    system_prompt=SYSTEM_PROMPT,
    output_type=[
        ToolOutput(
            ScanSuccess[ElectricStorageWaterHeaterScanResult],
            name="scan_success",  # ✅ Works with explicit name
        ),
        ToolOutput(ScanFailure, name="scan_failure"),
    ],
)

Root Cause

The automatic name generation appears to include the Generic type parameters (e.g., ScanSuccess[ElectricStorageWaterHeaterScanResult]), resulting in function names with brackets that violate Google's function naming constraints.

Proposed Solution

The automatic name generation should sanitize Generic type names by:

  1. Removing brackets from the class name (but keep type parameter)
  2. Converting to a valid function name format (snake_case)
  3. Ensuring compliance with provider naming requirements

Python, Pydantic AI & LLM client version

python 3.143
pydantic-ai >= 1.0.2
google/gemini-2.5-pro` via OpenRouter

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions