-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue in https://github.com/pydantic/pydantic-ai/issues before opening this issue
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:
- Removing brackets from the class name (but keep type parameter)
- Converting to a valid function name format (snake_case)
- 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