LimitInputLength
Rejects prompts longer than a character limit, a token limit, or both.
Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
max_chars |
int | None |
None |
Maximum characters |
max_tokens |
int | None |
None |
Maximum tokens |
tokenizer |
str |
'cl100k_base' |
tiktoken encoding name |
on_fail |
'raise' | 'warn' |
'raise' |
Raise InputBlocked or log and continue |
Set at least one of max_chars or max_tokens, or construction raises ValueError.
Token counts are exact when tiktoken is installed (pip install 'pydantic-ai-guardrails[tokens]'). Without it, the guardrail estimates tokens from the word count.
Examples
Section titled “Examples”from pydantic_ai import Agentfrom pydantic_ai_guardrails import InputBlocked, LimitInputLength
agent = Agent('openai:gpt-4o', capabilities=[LimitInputLength(max_chars=2000, max_tokens=500)])
try: agent.run_sync('word ' * 5000)except InputBlocked as e: print(e.reason) # exceeds 2000 char limit by ...Failure behaviour
Section titled “Failure behaviour”on_fail='raise': raisesInputBlocked;.reasonsays which limit was exceeded and by how much.on_fail='warn': logs and continues.