Context
Defining tool parameters.
Problem
Free-form string inputs lead to invalid values and wasted tokens on error handling.
Solution
Constrain inputs explicitly:
- Enums: Use defined values, not strings
- Ranges: Specify min/max for numbers
- Patterns: Validate format (email, UUID)
- Required vs optional: Be explicit
- Enums: Use defined values, not strings
- Ranges: Specify min/max for numbers
- Patterns: Validate format (email, UUID)
- Required vs optional: Be explicit
Examples
Python
class Priority(Enum):
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
URGENT = "urgent"
@tool
def create_ticket(
title: str,
priority: Priority = Priority.MEDIUM, # Enum, not string
due_days: int = Field(ge=1, le=365) # Range constraint
) -> Ticket: TypeScript
const createTicket = tool({
parameters: z.object({
title: z.string().min(1).max(200),
priority: z.enum(["low", "medium", "high", "urgent"]),
dueDays: z.number().int().min(1).max(365)
})
}); Considerations
- Enums are self-documenting for the LLM
- Include enum values in the description
- Do not assume the LLM will follow the constraints, even for Enums and known patterns, it can make mistakes and hallucinate unsupported values. Always validate early and fail fast with clear error messages.